How to get data file name from other function?

Hello and good day. How do i set myfile from searchstat() to get the file from loaddata() ?


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
void loaddata()
{
ifstream myfile;
	while(!myfile.is_open()) 
	{
		cout << "   Input data file: ";
		cin >> file;
		myfile.open(file.c_str(),ios::in | ios::binary);
		
		if ((file != "weather.txt") && (file != "weather2.txt"))
	        {
		cout <<"\n   File not found. " << endl;
		endprogram();
		}
	}
}
void SearchStat()
	{
	string line, file, type, thedate;
	string date;
	double lowTemp, highTemp, rainfall;
	
		ifstream myfile;
		getline(myfile, line); 
		{
			cout <<"\n\n     Format (year-month-date)";
			cout <<"\n     Enter the date you desired :  " ;
			cin >> date;
		
			bool found = false;
			while(getline( myfile, line ) && !found)
			{
				if(line.find(date) != string::npos)
				{ 									
				cout <<"\n     "<<line << endl;
					found = true;
					// Break not necessary although its correct
				}
			}

			if (!found){
				Sleep(2000);
				cout << "\n\n   NOT FOUND" << endl; }
				endprogram();
		
		}
		
	}
Last edited on
You can create the file in SearchStat and pass it by reference -
http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/
How do i pass string by reference?
Same was as with integer.

void pass(string& fileName)
in my main function.. i use switch and what about this?

1
2
case 1 : WeatherReports.LoadData(file);
			         break;


its an error if i put file or &file.
Well, I don't know what your new code is. The function you showed earlier is called loaddata and not LoadData.

What errors are you getting, copy paste the full messages.
If i put ...LoadData(file); it gives 'file' not declared. If i put string, "error: expected primary-expression before '&' token. i'm not sure how to copy and paste from command prompt
www.cpp: In function 'int main()':
www.cpp:235:37: error: no matching function for call to 'Weather::LoadData()'
case 1 : WeatherReports.LoadData();
^
www.cpp:235:37: note: candidate is:
www.cpp:38:9: note: void Weather::LoadData(std::string&)
void LoadData(string &filename)
^
www.cpp:38:9: note: candidate expects 1 argument, 0 provided

D:\>g++ www.cpp
www.cpp: In function 'int main()':
www.cpp:236:37: error: no matching function for call to 'Weather::LoadData()'
case 1 : WeatherReports.LoadData();
^
www.cpp:236:37: note: candidate is:
www.cpp:38:9: note: void Weather::LoadData(std::string&)
void LoadData(string &filename)
^
www.cpp:38:9: note: candidate expects 1 argument, 0 provided

D:\>g++ www.cpp
www.cpp: In function 'int main()':
www.cpp:236:37: error: 'file' was not declared in this scope
case 1 : WeatherReports.LoadData(file);
^

D:\>g++ www.cpp
www.cpp: In function 'int main()':
www.cpp:236:44: error: expected primary-expression before '&' token
case 1 : WeatherReports.LoadData(string &file);
^
www.cpp:236:45: error: 'file' was not declared in this scope
case 1 : WeatherReports.LoadData(string &file);
^

D:\>g++ www.cpp
www.cpp: In function 'int main()':
www.cpp:237:43: error: no matching function for call to 'Weather::LoadData(std::string*)'
case 1 : WeatherReports.LoadData( &file);
^
www.cpp:237:43: note: candidate is:
www.cpp:38:9: note: void Weather::LoadData(std::string&)
void LoadData(string &filename)
^
www.cpp:38:9: note: no known conversion for argument 1 from 'std::string* {aka std::basic_string<char>*}' to 'std::string& {aka std::basic_string<char>&}'

D:\>g++ www.cpp
www.cpp: In function 'int main()':
www.cpp:237:44: error: expected primary-expression before '&' token
case 1 : WeatherReports.LoadData(string &file);
^

D:\>g++ www.cpp
www.cpp: In function 'int main()':
www.cpp:237:37: error: 'file' was not declared in this scope
case 1 : WeatherReports.LoadData(file);
^

D:\>g++ www.cpp
www.cpp: In function 'int main()':
www.cpp:237:37: error: 'file' was not declared in this scope
case 1 : WeatherReports.LoadData(file);
^

D:\>g++ www.cpp
www.cpp: In function 'int main()':
www.cpp:237:44: error: expected primary-expression before '&' token
case 1 : WeatherReports.LoadData(string &file);
^

Your errors are pretty obvious -
error: no matching function for call to 'Weather::LoadData()'

You're calling a function that doesnt exist.

error: 'file' was not declared in this scope

"file" most likely dosent exist where you're trying to use it, the variable is non-existant.

Hard to comment when you're errors are in a program I cant see.
Last edited on
Here's my full 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>
#include <windows.h>
using namespace std;


void endprogram();
class Weather
{
	// reports of the same year

	struct entry
	{
		int year;
		double avg_min_tmp;
		double avg_max_tmp;
		double rain;

		vector<string> description;
		vector<int> count; // counter


	};

	vector<entry> Reports; // reports from different years

public:

   Weather(){
		// create fresh instance of the vector
	   Reports = vector<entry>();
   }

	
   void LoadData(string &filename)
   {
	   string file;
	   int number_of_lines = 0;
	   
	ifstream myfile;
	while(!myfile.is_open()) 
	{
		cout << "   Input data file: ";
		cin >> file;
		myfile.open(file.c_str(),ios::in | ios::binary);
		
		if ((file != "weather.txt") && (file != "weather2.txt"))
	    {
		cout <<"\n   File not found. " << endl;
		endprogram();
		}
	}
	
		while (!myfile.eof ())
		{
			getline(myfile, file);
			if (file=="") //error checking
			{
				cout << endl;
				number_of_lines=0; //if not true
			}
			else 
			{
				++number_of_lines; //count lines in weather.txt
			}
			
		}	
		cout <<"   Loading. . . . ." << endl;
		Sleep(2000);
		
		
	   while(true)
		{

		   int year, rainfall;
		   string type;
		   double lowtemp, hightemp;

		   myfile >> year >> type >> lowtemp >> hightemp >> rainfall;

		   if(myfile.eof())
			   break;
		   cout<<" Read "<<year<<" "<<type<<" "<<lowtemp<<" "<<hightemp<<" "<<rainfall<<endl;


		   int i, j;
		   for (i=0; i<Reports.size(); i++)
		   {
			   if(Reports[i].year !=year)
				   continue;


			   int n = Reports[i].description.size();

			   Reports[i].rain += rainfall;
			   Reports[i].avg_min_tmp = (Reports[i].avg_min_tmp*(n) + lowtemp)/(n+1);
			   Reports[i].avg_max_tmp = (Reports[i].avg_max_tmp*(n) + hightemp)/(n+1);


			   vector<string> descrip = Reports[i].description;
			   for(j=0; j<descrip.size(); j++)
			   {
				   if(descrip[j].compare(type) != 0)
					   continue;

				   Reports[i].count[j]++;
				   break;

			   }

			   if(j ==descrip.size()){
				   Reports[i].description.push_back(type);
				   Reports[i].count.push_back(1);

			   }
			   break;
		   }

		   if (i == Reports.size()){

			   struct entry NewEntry;
			   NewEntry.year = year;
			   NewEntry.avg_min_tmp = lowtemp;
			   NewEntry.avg_max_tmp = hightemp;
			   NewEntry.rain = rainfall;
			   NewEntry.description.push_back(type);
			   NewEntry.count.push_back(1);

			   Reports.push_back(NewEntry);
		   }

		  // number_of_lines++;

		}
		cout <<"   Successfully loaded "<< number_of_lines<< " entries!" << endl; 
		endprogram();
	}	
	 
	
	
   

   void OverallStats()
   {
	   system("cls");
	   for(int i = 0; i<Reports.size(); i++)
	   {
		   cout<<"Year: "<<Reports[i].year<<endl;
		   cout<<"\tAvg minimum temperature: "<<Reports[i].avg_min_tmp<<" C"<<endl;
		   cout<<"\tAvg maximum temperature: "<<Reports[i].avg_max_tmp<<" C"<<endl;
		   cout<<"\tTotal rainfall: "<<Reports[i].rain<<" mm"<<endl;
		   cout<<"\tDays where reported weather is:"<<endl;

		   for(int j=0; j<Reports[i].description.size(); j++)
		   {
			   cout<<"\t\t"<<Reports[i].description[j];
			   cout<<"\t"<<Reports[i].count[j]<<endl;
		   }
		   cout<<endl;
	   }

	endprogram();
   }

   
    void SearchStat()
	{
	string line, file, type, thedate;
	string date;
	double lowTemp, highTemp, rainfall;
		
		ifstream myfile;
		LoadData(file);
		getline(myfile, line); 
		{
			cout <<"\n\n   Format (year-month-date)";
			cout <<"\n   Enter the date you desired :  " ;
			cin >> date;
		
			bool found = false;
			while(getline( myfile, line ) && !found)
			{
				if(line.find(date) != string::npos) // Search year followed by month and date respectively
				{ 									// ::npos = "until the end of the string".
					
					cout <<"\n   "<<line << endl;
					cout <<"\n ============================================" << endl;
					found = true;
					// Break not necessary although its correct
				}
			}

			if (!found){
				Sleep(2000);
				cout << "\n\n   NOT FOUND" << endl; }
				endprogram();
		
		}
		
	}


};

void Menu()
{
	cout <<" ============================================" << endl;
	cout <<" * Welcome to xyz weather stats generator * " << endl;
	cout <<" ============================================" << endl;
	cout <<"   Menu options :" << endl;
	cout <<"\n     1. Load data file";
	cout <<"\n     2. Overall stats";
	cout <<"\n     3. Search by date";
	cout <<"\n     4. Monthly stats";
	cout <<"\n     5. Quit";
	cout <<"\n ============================================" << endl;
	cout<<endl<<"   User Options: ";
	return;
}

int main(){

	Weather WeatherReports;
	system("cls");
	string file;
	int option;
	bool quit = false;
	while(!quit){

		Menu();
		cin>>option;
		switch(option){

			case 1 : WeatherReports.LoadData(string &file); //HERE! 
			         break;

			case 2 : WeatherReports.OverallStats();
			         break;

			case 3 : WeatherReports.SearchStat();
			         break;

			case 5:
				{
					cout <<"\n ============================================" << endl;
					cout<<"       You've chose to quit the program.";
					cout<<"\n          Thank you and Goodbye.";
					cout <<"\n ============================================" << endl;
					exit(1);
				}
			default:
				{
					system("cls");
					cout <<"\n ============================================" << endl;
					cout <<"\n     Invalid selection. Program ends.\n";
					cout <<"\n ============================================" << endl;
					exit(1);
					break;
				}
			}


	}
	return 0;



}

void endprogram() 
{
	string end;
	cout << endl;
	cout <<"\n   Please press any key to continue,";
	cout <<"\n   Press N or n to exit the program => ";
	cin >> end;
	
	
	if((end[0] == 'N')||(end[0] == 'n'))
	{
		cout <<"\n ============================================" << endl;
		cout<<"       You've chose to quit the program.";
		cout<<"\n          Thank you and Goodbye.";
		cout <<"\n ============================================" << endl;
		exit(1);
	}
	else
	{
		system("cls");
		return;
	}
	
}


/*cout<<"   Enter year: ";
		int year;
		cin>>year;
		int i;
		for(i=0; i<Reports.size(); i++)
		{

			if(Reports[i].year!=year)
				continue;

			cout<<"Year: "<<Reports[i].year<<endl;
			cout<<"\tAvg. minimum temperature: "<<Reports[i].avg_min_tmp<<" C"<<endl;
			cout<<"\tAvg. maximum temperature: "<<Reports[i].avg_max_tmp<<" C"<<endl;
			cout<<"\\tDays where reported weather is:"<<endl;

			for(int j=0; j<Reports[i].description.size(); j++)
			{
				cout<<"\t\t"<<Reports[i].description[j];
				cout<<"\t"<<Reports[i].count[j]<<endl;
			}
			cout<<endl;
			break;
		}

		if(i==Reports.size())
			cout<<"\n   No entry for year "<<year<<" was found"<<endl;
			endprogram();*/




1
2
3
4
5
6
7
8
case 1 : WeatherReports.LoadData(string &file); //HERE! 
			         break;

			case 2 : WeatherReports.OverallStats();
			         break;

			case 3 : WeatherReports.SearchStat();
			         break;

You're using a Weather Object to call these 3 functions. But that only works if the functions are part of the class, which they are not. So you can't call them this way, you should revisit classes and make it proparly -
http://www.cplusplus.com/doc/tutorial/classes/
Topic archived. No new replies allowed.