passing two different strut member variables as const reference to a function

so far i think i'm on the right track, but i'm having trouble outputting one of the other struct member variables, i'm also not sure if i made the other structure as constant.
I'll post the whole code a bit later in this post, but for now i want to show where i'm having problems.

The struct WeatherMan is being returned back to main, and when i make it's variables constant, it doesn't display.
here's the function that i'm attempting to make WeatherMan variables as constant references
1
2
3
4
5
6
7
void displayAllData(const DayOfWeek day[], const WeatherMan &getting)
{
	cout << "the name of weatherman is: " << getting.name;
	for (int i = 0; i < 7; i++)
	{
		cout << "The amount of rain for " << day[i].dayName << " is: " << day[i].rainAmount << endl;
	}


and here is the function where i obtain the name of a weatherman and its channel name
1
2
3
4
5
6
7
8
9
10
WeatherMan getWeatherdata()
{
	WeatherMan data;
	cout << "Please type in your name: "; 
	getline(cin, data.name);
	cout << endl << "Please type in channel name: ";
	getline(cin, data.newChannel);
	return data;
	
}


I also want to know if i passed the DayOfWeek variables correctly, it displays what i want it to display correctly, but the format looks a bit off. i'm not sure if i passed it correctly as a constant reference.


here is the entire code for reference.

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
  #include <iostream>
#include <string>
#include <iomanip>


using namespace std;


/*struct declarations*/

struct WeatherMan
{

	string name, newChannel;
};

struct DayOfWeek
	{
		string dayName;
		double rainAmount;
		
	};
const int NUM_DAY_OF_WEEK = 7;
DayOfWeek day[NUM_DAY_OF_WEEK];











/*PROTOTYPES*/
WeatherMan getWeatherdata();
void getRainAmountdata(DayOfWeek [], const int NUM_DAY_OF_WEEK);
void displayAllData(const DayOfWeek [] , const WeatherMan &);







int main()
{
	WeatherMan obtainingWeather, example;
	DayOfWeek obtainingRain;

	day[0].dayName = "Monday";
	day[1].dayName = "Tuesday";
	day[2].dayName = "Wednesday";
	day[3].dayName = "Thursday";
	day[4].dayName = "Friday";
	day[5].dayName = "Saturday";
	day[6].dayName = "Sunday";


	obtainingWeather=getWeatherdata();
	getRainAmountdata(day, NUM_DAY_OF_WEEK);
	displayAllData(day, example);
	
	
	
	




	system("pause");
	return 0;
}



WeatherMan getWeatherdata()
{
	WeatherMan data;
	cout << "Please type in your name: "; 
	getline(cin, data.name);
	cout << endl << "Please type in channel name: ";
	getline(cin, data.newChannel);
	return data;
	
}

void getRainAmountdata(DayOfWeek day[], const int NUM_DAY_OF_WEEK)
{
	for (int i = 0; i < NUM_DAY_OF_WEEK; i++)
	{
		cout << "Enter the rainfall(in inches) for " << day[i].dayName << ": ";
		cin >> day[i].rainAmount;
	}
}

void displayAllData(const DayOfWeek day[], const WeatherMan &getting)
{
	cout << "the name of weatherman is: " << getting.name;
	for (int i = 0; i < 7; i++)
	{
		cout << "The amount of rain for " << day[i].dayName << " is: " << day[i].rainAmount << endl;
	}
}


sorry for some of the obscure variables names, i was just testing the waters to see if the code even worked
Last edited on
Look at this snippet:
1
2
3
4
5
6

	WeatherMan obtainingWeather, example;
...
	obtainingWeather=getWeatherdata();
...
	displayAllData(day, example);


You assign information to the obtainingWeather instance but then you try to display example?

Where have you assigned values to the example instance?
i used example for the displayAllData because I wasn't sure if i could use obtainingweather. but now that you pointed that out, I used obtainingWeather to display. my code worked! thank you!

also, am i passing the DayOfWeek variable members correctly to the function displayAllData?
Last edited on
am i passing the DayOfWeek variables members correctly to the function displayAllData?

It looks okay. You're passing the array of your day structure which seems to be what you want.

awesome, thanks for your help!
Topic archived. No new replies allowed.