Read numeric datafile one line at a time

Hello All,
I am trying to write a program about weather report.The program will read numeric data from a file and store it in 1 array of 4 objects. Each line belong to an object so object1 get numeric values on line 1; object 2 ==> line2. I need to find a way to read only one line at a time and read the next line each time. Any suggestions

P.S. Later on, I am going to use the input values to make a weather summary report.

Thanks for your help


This is the data contained in weatherData.txt file:
12 1234 8524 85.55 25.55
22 2233 234 28.5 27.6
28 84 456 45 43.5
25 7 36 13 35 34


This is my C++ code

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

#include <iostream>
#include <fstream>
#include <cstdlib>	// exit prototype
#include <iomanip>

using namespace std;

class Weather
{
private:
	int m_dayOfWeek;
	int m_highTemp;
	int m_lowTemp;
	double m_amountRain;
	double m_amountSnow;
public:
    	Weather(int, int, int, double, double);			
		Weather();
		void setWeather(int, int, int, double, double);
		void getWeather();
		void displayWeather();
		void summaryWeather();
		void displaySummary();
		~Weather();
};

	//Initialize constructor 
Weather::Weather(int _day, int _high, int _low, double _rain, double _snow)
{
	setWeather( _day,  _high,  _low,  _rain,  _snow);
}	// End Weather constructor

Weather::Weather()
{
	
}

void Weather::setWeather(int _day, int _high, int _low, double _rain, double _snow)
{
	m_dayOfWeek = _day;
	m_highTemp = _high;
	m_lowTemp = _low;
	m_amountRain = _rain;
	m_amountSnow = _snow;
}

//Initialize destructor 
Weather::~Weather()
{
	cout<<"\n*******Constructor destroyed*********"<<endl;
}	// End Weather destructor


//Method to read data from grades.txt file and write it to OUTgrades.txt 
void Weather::getWeather()
{
	ifstream in;
	ofstream out;

	in.open("weatherData.txt");			

	// exit program if ifstream could not find file 
	if (!in) {
		cout << "File could not be  found!" << endl;
	}

	// exit program if ofstream could not find file 
	if (!out) {
		cout << "File could not be  found!" << endl;
	}
	
	// exit program if ifstream could not open file 
	if (!in.is_open()){
	cout << "File could not be  opened!" << endl;
	}

	// Open file and append data
	out.open("weatherOutput.txt", ios::app);

	cout << left << setw(13)<< "Day #" 
		<< setw(13) << "High Temp" << setw(13) << "Low Temp"<< setw(13)
		<< "Rain"<< setw(13) << "Snow" << endl 
		<< fixed << showpoint;

		out << left << setw(13)<< "Day #" 
		<< setw(13) << "High Temp" << setw(13) << "Low Temp"<< setw(13)
		<< "Rain"<< setw(13) << "Snow" << endl 
		<< fixed << showpoint;
	
		while(in >> m_dayOfWeek >> m_highTemp  >> m_lowTemp >>  m_amountRain >> m_amountSnow)
	{
		cout << left << setw(13) << m_dayOfWeek << setw(13) << m_highTemp
			<< setw(13) << m_lowTemp << setw(13) << setprecision (2)
			<< m_amountRain << setw(13)  << setprecision (2) 
			<< m_amountSnow << setw(13) << endl; 

	out << left << setw(13) << m_dayOfWeek << setw(13) << m_highTemp 
		<< setw(13) << m_lowTemp << setw(13) << setprecision (2)
		<< m_amountRain << setw(13)  << setprecision (2) 
		<< m_amountSnow << setw(13) << endl; 
	}
	in.close();
	out.close();
}

void Weather::displayWeather()
{
	cout << m_dayOfWeek << endl;
	cout << m_highTemp << endl;
	cout << m_lowTemp << endl;
	cout << m_amountRain << endl;
	cout << m_amountSnow << endl;
}


void main ()
{
	Weather aWeather( 99, 9999, 9999, 0, 0);
	aWeather.displayWeather();
	
	
	Weather fourDaysWeather[4];
	
	for(int x = 0; x < 4; x++)
	{
		
		fourDaysWeather[x].getWeather();
	}


	/* Scaffolding code for testing purposes */ 
	cin.ignore(256, '\n');
	cout << "Press ENTER to continue..." << endl;
	cin.get();
	/* End Scaffolding */ 

}
Topic archived. No new replies allowed.