array of structs declaration and output problem

My program needs to have a declared struct called DayOfWeek that holds the variables dayName(string) and rainAmount(double)
I've done that part and now the prompt tells me that i must create an array of structs that i presume is for the next step following that.

"Create another function to prompt the user to enter the rainfall amount for each day of the week. Pass your array of structs to a function. Note, that day of the week should not be hard-coded in cout << “Enter the rainfall (in inches) for Monday”, instead use a for loop and cout << “Enter the rainfall (in inches) for” << day[i].dayName;"


My problem is that i'm not sure how to create a function that would display the day name without putting dayName from the DayOfWeek struct into a simple array. here's what i have so far as the struct and declaration of the array of structs. i'm not sure if it's correct.
1
2
3
4
5
6
7
8
9
[...]
  struct DayOfWeek
	{
		string dayName;
		double rainAmount; 
	};
const int NUM_DAYS_OF_WEEK = 7;
DayOfWeek day[NUM_DAYS_OF_WEEK];
[...]


originally, before i declared an array of structs, this was how my program was before, and it ran fine

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
[...]
struct DayOfWeek
	{
		string dayName[NUM_DAYS_OF_WEEK]{ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
		double rainAmount[NUM_DAYS_OF_WEEK];
		
	};

//Prototype
void getRainAmountdata(DayOfWeek &);


int main()
{
DayOfWeek obtainingRain;
getRainAmountdata(obtainingRain);

return 0;
}


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


i just input 1-7 in numerical order for the amount of inches
Enter the rainfall(in inches) for Monday: 1
Enter the rainfall(in inches) for Tuesday: 2
Enter the rainfall(in inches) for Wednesday: 3
Enter the rainfall(in inches) for Thursday: 4
Enter the rainfall(in inches) for Friday: 5
Enter the rainfall(in inches) for Saturday: 6
Enter the rainfall(in inches) for Sunday: 7
Last edited on
I would create the function like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
void getRainAmountdata(DayOfWeek[] days)
{
   for (int i = 0; i < NUM_DAYS_OF_WEEK; i++)
   {
      // get your data here -> days[i].dayName
   }
}
int main()
{
   getRainAmountdata(day);
   // display your data
}
how should my prototype look like if i were to make a function like yours Thomas?
Like this: void getRainAmountdata(DayOfWeek[]);
sorry to ask again, but which struct set up should i use? the program is still giving me some errors
You struct should look like this:

1
2
3
4
5
6
7
8
struct DayOfWeek
	{
		string dayName;
		double rainAmount;
	};
// then create an array of DayOfWeek objects like this:
const int NUM_DAYS_OF_WEEK = 7;
DayOfWeek day[NUM_DAYS_OF_WEEK];


Now essentially you have 7 objects of type DayOfWeek. Each object has dayName field. You need to fill in the variable with a day of the week for each object.

1
2
3
4
5
day[0].dayName = "Monday";
day[1].dayName = "Tuesday";
....
....
....


Now each object has a day of week associated with it. Now you can use Thomas' function above to get the rain amount:

1
2
3
4
5
6
7
8
void getRainAmountdata(DayOfWeek[] days)
{
   for (int i = 0; i < NUM_DAYS_OF_WEEK; i++)
   {
       cout << "Enter the rainfall(in inches) for " << days[i].dayName << ": ";
	cin >> days[i].rainAmount;
   }
}
Last edited on
Okay, so while i was filling on the cariable with a day of the week in each object, it kept showing me syntax errors

1
2
 day[0].dayname="Monday";
day[1].dayName="Tuesday";


under the number 0, it told me that the size of the array must be bigger than zero, and under the (.) symbol, it told me that it expected a ';'
when i went and created the second object, under day, it told me that it this declaration has no storage class or type specifier.

i'm not sure if maybe i formatted the objects wrong or if maybe they are supposed to be declared in the structure.


i apologize for asking some many questions, we're just covering structs and i'm still a bit unsure why certain things aren't working
Can you post your entire code please?
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
#include <iostream>
#include <string>
#include <iomanip>
/**/

using namespace std;



struct WeatherMan
{

	string name, newChannel;
};

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

day[0].dayName = "Monday";
day[1].dayName = "Tuesday";









/*PROTOTYPES*/
WeatherMan getWeatherdata();
void getRainAmountdata(DayOfWeek &);
void displayAllData(const WeatherMan &, const DayOfWeek &);




int main()
{
	WeatherMan obtainingWeather;
	DayOfWeek obtainingRain;
	obtainingWeather=getWeatherdata();
	getRainAmountdata(obtainingRain);
	
	




	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 &get)
{
	for (int i = 0; i < NUM_DAY_OF_WEEK; i++)
	{
		cout << "Enter the rainfall(in inches) for " << get.dayName[i] << ": ";
		cin >> get.rainAmount[i];
	}
}


Last edited on
Hi,
1
2
day[0].dayName = "Monday";
day[1].dayName = "Tuesday";


Where are the five remaining days? Have they gone missing?
1
2
3
4
5
day[2].dayName = "Wednesday";
day[3].dayName = "Thursday";
day[4].dayName = "Friday";
day[5].dayName = "Saturday";
day[6].dayName = "Sunday";


Also, it is illegal to initialize them outside a function. At least put them in the function main().

==>
1
2
3
4
5
6
7
8
9
10
11
int main()
{
    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";

    // ... 
Does that help? :)
Well when i wrote the first statement, it gave me syntax errors so i didn't write in the rest of the remaining days. And oh i see! Thank you for the help!
sorry one more problem, i keep getting a syntax error in the arguments

1
2
3
4
5
6
7
8
void getRainAmountdata(DayOfWeek[] day)
{
	for (int i = 0; i < NUM_DAY_OF_WEEK; i++)
	{
		cout << "Enter the rainfall(in inches) for " << day[i].dayName << ": ";
		cin >> day[i].rainAmount;
	}
}

it says that expected a ')' next to day in the function arguments
Can you post your entire code again?
Your function getRainAmountData should be getting the array 'day' and its size. You are trying to access the array in the function but haven't passed it in.

1
2
3
4
5
6
7
8
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;
	}
}


Then in main you should call the function like this:

1
2
3
4
	WeatherMan obtainingWeather;
	DayOfWeek obtainingRain;
	obtainingWeather=getWeatherdata();
	getRainAmountdata(day, NUM_DAY_OF_WEEK);

thank you Arslan and everyone else! worked perfectly
Good to hear :)
Topic archived. No new replies allowed.