Basic File I/O Question

Hello - For some reason when I am trying to assign the text file data to the appropriate variables, I am having some problems with it skipping over the town names and date. As you can see from the output below, the town is being assigned to Paul, which is at the end of the first line. Shouldn't town be assigned BANGOR?
*As a note this is nowhere near complete, I am just doing some initial checks to see why the correct data isn't being assigned to the correct variables.

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
BANGOR         02/10/12       2.5       W         2.5 C  Paul Smith
OLD_TOWN       02/10/12       4.1       N         28.9 F Susan Spellman
BANGOR         02/10/12       3.9       W         1.0 C  Tom Houson
CALAIS         02/10/12       2.9       N         34.5 F Mike Conway
CALAIS         02/10/12       2.2       N         34.1 F Penny Thomas
BAR_HARBOR     02/10/12       2.6       W         4.7 C  Laurel Penn
BANGOR         02/10/12       2.9       N         32.0 F Xang Xao
PORTLAND       02/10/12       2.2       N         4.4 C  Jared Washington
PORTLAND       02/10/12       2.9       N         3.8 C  Opal Tines
OLD_TOWN       02/10/12       3.6       N         29.7 F Kaitlin Wallis
[\code]

[code]
  int main()
{
	string town, date, name;
	double speed, celciusTemptemp , farenheitTemp, temp;
	char direction;
	
	ifstream fin;
	fin.open("weather.txt");	
	while (!fin.fail()) {			//while not end of report, do the following:
		fin >> town >> date >> speed >> direction >> temp >> name;	
	}
	cout << "town" << town << "date" << date << "speed" << speed << "direction" << direction << "temp" << temp << "name" << name; //
	fin.close(); //closes the text file

	ofstream fout; //opens a new text file
	fout.open("finishedDoc.txt"); //names the text file finishedDoc.txt
	fout.setf(ios::left);
	fout << setprecision(2);
	fout << "Wind Measurement for Month " << date << endl << endl;
	fout << "Wind Speed (m/sec) << Direction << Temp (F) << Temp(C) << Wind Chill (F) << Wind Chill (C)" << endl;
	fout << "---------------------------------------------------------------------------------------------------" << endl;
	fout << setw(45) << town << date << speed << direction;

	fout.close();
	return 0;
}
Last edited on
Remember that when using the extraction operator>> extraction stops when it encounters a whitespace character. Also you seem to be skipping the extraction of the temperature scale (C or F).

Your printout is outside the loop so it will try to print the last items processed before the stream errors out. Ie, "Paul" for the city, "Smith" for the date, 2 for the speed and stream error when trying to read the direction since the a '/' character is not a valid numeric input.

Perhaps you should start with the printout inside the loop so you can see what is being retrieved. And note you may require some kind of data structure to hold the data.

Topic archived. No new replies allowed.