Taking in input from a textfile and printing it out

My computer science teacher wants us to take in input from a textfile that displays rain from a city and a city name, then add together the rain from each city and output it. The text file looks like this 15.1,Chico,17.4 34.4,Seattle,30.5
22.9,Portland,26.1
12.3,New York,9.4
.5,Death Valley,1.1
3,Las Vegas,13.8
60,Alajuela,54.3
17.3,San Francisco,20.3
29.5,Dublin,30.8

except all these cities and names are on one line. Anyways I kind of get how to input the text but not fully.

Right now this is what I have at the moment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
string line;
string rain;
ifstream myfile;
myfile.open("inputfile.txt");





while (getline(myfile, rain, ',').eof());
{
getline(myfile, rain, ',');
cout << rain;
}


all I'm trying to do is first printout the first line correctly. So add the two rainfall averages for the first and second year of the line that says Chico, then print out both of them added together along with the name to the left of it. Kind of like this

Chico 32.5

I'm having trouble though figuring out how to get the first number in this line and then skip over the "chico" and then continue to reading the second number given.
Try doing this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
string rain1;
string city;
string rain2;
string avg;
ifstream myfile;
myfile.open("inputfile.txt");

while(myfile.is_open());
{
     if(myfile.good())
     {
         getline(myfile,rain1,',');
         getline(myfile,city,',');
         getline(myfile,rain2,',');
         avg=(rain1+rain2)/2;
         cout<<city<<"/t"<<avg<<;
      }

}


I think should work for what u are trying to do.
so this happened in the output

Chico 15.117.4
34.430.5
22.9 SeattlePortlandNew York 26.1
12.39.4
.51.1
3 Death ValleyLas VegasAlajuela 13.8
6054.3
17.320.3
29.5 San FranciscoDublin20.3


and heres my code, just changed one thing and that was the avg because I meant to say just the sum of the two rain variables not the average.
1
2
3
4
5
6
7
8
9
10
11
while (myfile.is_open())
{
if (myfile.good())
{
	getline(myfile, rain1, ',');
	getline (myfile, city, ',');
	getline(myfile, rain2, ',');
	avg = (rain1 + rain2);
	cout << city << "\t" << avg;
	}
}
I thought maybe I could change the string avg and string rain1, rain2 into a float but that gave me errors no matter what I did to fix them.
Because , Getline()'s Second arguement takes a String.

oh okay, so how can I fix this problem I'm having?
Topic archived. No new replies allowed.