#include <iostream>
#include <fstream>
#include <sstream>
int main()
{
std::ifstream inputFile("input.txt");
std::string line;
int num = 0, sum = 0; //always initialize built in types in local/block scope
if(inputFile)
{
//go line by line
while(std::getline(inputFile, line))
{
std::istringstream ss(line);
//go through individual numbers
while(ss >> num)
{
sum += num;
}
}
}
else
{
std::cout<<"Input file cannot be read"<<std::endl;
}
inputFile.close();
std::cout << "The sum of all the intergers from the file is: "<<sum<<std::endl;
return 0;
}
1
2
3 4 5
6