Program does not work

Jan 9, 2014 at 3:30pm
Review4.txt
Objectville
23094
10239
–1
Bjarne City
4562
328
125
–1

My program cannot read the 5th line which is the cityname and the num blew it.
I want to output like this:
Objectville: 33333
Bjarne City: 5015

but mine only do the Objectville one
plz help me display the Bjarne City one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  #include<iostream>
#include<fstream>
#include<vector>
using namespace std;

int main()
{
    fstream InFile("Review4.txt",ios::in);
    if(InFile.fail()){
        cout<<"File could not be opened";
        return 0;
    }
    else{
        int num;
        vector<int>sum(2,0);
        int counter=0;
        string cityname;
        vector<string>city(2,"");

        while(InFile>>city[0]){
            while(InFile>>num){
                if(num!=-1)
                    sum[0]+=num;
                else
                    break;
            }
        }
        while(InFile>>city[1]){
            while(InFile>>num){
                if(num!=-1)
                    sum[1]+=num;
                else
                    break;
            }
        }
        for(int i=0;i<=1;i++)
            cout<<city[i]<<": "<<sum[i]<<endl;

        return 0;
    }
}
Jan 9, 2014 at 4:25pm
If you don't plan on doing anything with the numbers and names you read, you can do it this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <limits>
using namespace 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 ...)
Last edited on Jan 10, 2014 at 4:47pm
Jan 10, 2014 at 5:05am
I fixed the coding like what u said
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
    fstream InFile("Review4.txt",ios::in);
    if(InFile.fail()){
        cout<<"File could not be opened";
        return 0;
    }
    else{
        for(string city; getline(InFile,city);){
            int sum=0,num;
            for(InFile>>num;num>0;InFile>>num)
                sum+=num;
            cout<<city<<": "<<sum<<endl;
        }
        return 0;
    }
}


but the 2nd line still did not display or maybe it does not work for Bjarne City one
It display like ":" for 2nd line
Jan 10, 2014 at 10:16am
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.
Jan 10, 2014 at 4:47pm
The code is updated now
Jan 13, 2014 at 3:10pm
thx, it works now
Topic archived. No new replies allowed.