#include <iostream>
#include <fstream>
#include <limits>
usingnamespace std;
int main()
{
fstream in("Review4.txt",ios::in);
if(in.fail()){
cout<<"File could not be opened";
return 0;
}
for (string city; getline(in, city); )
{
int sum = 0, num;
for (in >> num; num > 0; in >> num)
sum += num;
cout << city << ": " << sum << '\n';
in.ignore(numeric_limits<streamsize>::max(), '\n');
}
return 0;
}
The reason your was not displaying the other city name is because you were using the >> operator to read in the names. The >> operator stops reading once it gets to whitespace (\n, \t, ,\r ...)
After the end of the inner for-loop, you need to remove the trailing newline after the last integer. InFile.ignore(1000, '\n'); // remove trailing newline
Also, using the input data posted above, Review4.txt
Objectville
23094
10239
–1
Bjarne City
4562
328
125
–1
there may be another error, because –1 is not the same as -1, the long dash character '–' is not a '-' minus sign.