I had an assignment that asked me to read input from a .txt file then instead of displaying them line by line, I was supposed to use a loop to add a line to the one before it and display the sum. So in essence the file has: 1 2 3 4 5 My output was supposed to be: 1 3 5 7 9. There was also a string "stop" which would cause the loop to terminate when encountered. I could get the addition part to work. Any suggestions?
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inFile;
int number;
int sum = 0;
string word;
stringstream sso;
inFile.open("lines.txt");
while (inFile >> word && word != "stop")
{
sso << word;
sso >> number;
if (word != "stop")
{
cout << (number + sum) << endl;
sum = number;
}
else
{
cout << "File reading stopped";
break;
}
}
inFile.close();
return 0;
}