Classes help

I think I have the general gist of classes. This code works, but what I want to change is what numbers are used. I want the user to be able to input actual data. How do I use actual input instead of preset data (as in this example)?

As you can see, I tried changing the constructor to get the input. The question now is, how do I get that data to show up for each station?

Thanks!

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
//David Scheip
//Created 4.19.13
//Page 426 Number 3
//Write a program that creates three Weather_station objects.
//At each weather station, the wind speed, temperature and humidity are recorded.
//Use a constructor to initialize the values.
//Print out the data.

#include <iostream>
using namespace std;


class Weather_station
{
	private:
		int wind_speed;
		double temp, humid;

	public:
		Weather_station(int, double, double);
		void set_data();
		void print_data(); 
};


Weather_station::Weather_station (int wind_speed_var, double temp_var, double humid_var)
				:wind_speed (wind_speed_var), temp (temp_var), humid (humid_var)
{
	cout << "enter wind speed, temp and humid";
	cin >> wind_speed_var >> temp_var >> humid_var;

};

void Weather_station::print_data()
{
	cout << "The Wind Speed is (mph) " << wind_speed << ", the Temperature is (°F) " << temp << endl <<"	and the Humidity is (%) "<< humid << endl << endl;
};

int main()
{
	int wind_speed_var;
	double temp_var, humid_var;

	Weather_station station1 (15, 32.2, 56.43), station2 (15, 32.2, 56.43), station3 (15, 32.2, 56.43);
	station1.print_data();
	station2.print_data();
	station3.print_data();

	cin.get();
};
You need to write a definition for your set data function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Weather_station::set_data()
{
       cout << "Enter a number for wind speed " ;
       cin >> wind_speed;
       cin.ignore();
       cout << endl;

       cout << "Enter a number for temp";
       cin >> temp;
       cin.ignore();
       cout << endl;

       cout << "Enter a number for humidity";
       cin >> humid;
       cin.ignore();
       cout << endl;
}


Please note this code probably has bugs in it because I wrote it in my post and didn't debug it, Its meant as an example.

You should always define a default constructor. What would happen if you tried to create an array of your Weather_station class. you would get an error because when the compiler tries to create the array it calls the default constructor but you don't have one

You don't need the ; on lin 32, 37 or 50


You should not be trying to enter values in the constructor you defined, and you are trying to save the input into the parameters for your constructor which isn't allowed.
Last edited on
1
2
3
4
5
6
7
8
int main()
{
	Weather_station station1 ();
	station1.print_data();
	

	cin.get();
}


Without my station1 having any data in the (), station1 comes back with "Error:expression must have a class type".

My question is: how to I input data and have it passed there?? Or am I looking at this from the wrong angle? My orignial program works - ie. it prompts the user to input the data, but then prints out the values in station1 ().

Thanks again!

David
Line 3 in your code Weather_station station1 (); is not declaring an object of the Weather_station class. The compiler actually thinks this is a function prototype for a function named station1 that returns a Weather_station object and has no parameters. To declare an object of the Weather_station with no aruguments you need to omit the () so it would look like this Weather_station station1; but because you don't have a default constructor this would not compile.

To pass data to a constructor you would use the parameters like any other function. It is possible to prompt the user for input in the constructror but it is bad programming. If you wan't to prompt the user for input it should be done in a seperate method. Like the one I posted above.

Your constructor definition should look like this
1
2
3
4
5
6
Weather_station::Weather_station (int wind_speed_var, double temp_var, double humid_var)
{
	wind_speed = wind_speed_var;
	temp = temp_var;
	humid = humid_var;
}




Here is an example of working 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
//David Scheip
//Created 4.19.13
//Page 426 Number 3
//Write a program that creates three Weather_station objects.
//At each weather station, the wind speed, temperature and humidity are recorded.
//Use a constructor to initialize the values.
//Print out the data.

#include <iostream>
using namespace std;


class Weather_station
{
	private:
		int wind_speed;
		double temp, humid;

	public:
		Weather_station();
		Weather_station(int, double, double);
		void set_data();
		void print_data(); 
};
Weather_station::Weather_station()
{
	
		wind_speed = 0;
		temp = 0.0;
		humid = 0.0;
}

Weather_station::Weather_station (int wind_speed_var, double temp_var, double humid_var)
{
	wind_speed = wind_speed_var;
		temp = temp_var;
		humid = humid_var;
}

void Weather_station::print_data()
{
	cout << "The Wind Speed is (mph) " << wind_speed << ", the Temperature is (" << char(248) << "F) " << temp << endl <<"	and the Humidity is (%) "<< humid << endl << endl;
}

void Weather_station::set_data()
{
       cout << "Enter a number for wind speed " ;
       cin >> wind_speed;
       cin.ignore();
	   cout << endl;

       cout << "Enter a number for temp ";
       cin >> temp;
       cin.ignore();
	   cout << endl;

       cout << "Enter a number for humidity ";
       cin >> humid;
       cin.ignore();
	   cout << endl;
}

int main()
{
	int wind_speed_var;
	double temp_var, humid_var;

	Weather_station station1 (15, 32.2, 56.43), station2 (15, 32.2, 56.43), station3 (15, 32.2, 56.43);
	
	station1.print_data();
	station2.print_data();
	station3.print_data();
	
	cout << "Enter data for station 1:" << endl;
	station1.set_data();
	cout << endl;

	cout << "Enter data for station 2:" << endl;
	station2.set_data();
	cout << endl;

	cout << "Enter data for station 3:" << endl;
	station3.set_data();
	cout << endl;
	
	station1.print_data();
	station2.print_data();
	station3.print_data();

	cin.ignore();
	return 0;
}
I greatly appreciate you help. I have one other question though.

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
int main()
{
	int wind_speed_var;
	double temp_var, humid_var;

	Weather_station station1 (), station2 (), station3 ();
	
	station1.print_data();
	station2.print_data();
	station3.print_data();
	
	cout << "Enter data for station 1:" << endl;
	station1.set_data();
	cout << endl;

	cout << "Enter data for station 2:" << endl;
	station2.set_data();
	cout << endl;

	cout << "Enter data for station 3:" << endl;
	station3.set_data();
	cout << endl;
	
	station1.print_data();
	station2.print_data();
	station3.print_data();

	cin.ignore();
	return 0;
}


This code gives an error with station1, station2 and station3: "Error:expression must have a class type". This error is not present when Weather_station station1 (), station2 (), station3 (); each of these have values in the () - an int, double, double to be exact. But having values in this has the program output those values (which do not need to be there). Any thoughts? I'll tinker around an play and see what I can come up with.

Again, thank you for helping - This class gave me some problems a few semesters ago and I am retaking it, and it's starting to make sense. I just need to play more and this site has been a great help (all of the guides and people like you who offer help).

David
Topic archived. No new replies allowed.