How to ask for temperature for x number of days?

I need to make a program that asks for how many days there are in a particular month, and then ask for the minimum and maximum temperature for each of the days.
So now i'm wondering how i'm supposed to make it ask for the temperature of exactly 30 days, and not 28 or 29 or 31, if it's 30 that i've entered.

I don't know how clear i made myself there, but i hope someone can help me out :)
(i'm not asking anyone to write everything for me, but to just point me in the right direction)
Thanks!
have you got any code so far? post what you have, and we can try to help.

Also, do you really want to specify how many days are in the month, or would you like to just specify which month and have it automatically know number of days (with perhaps exception of having to ask if it's leapyear or not for feburary)?

You can hard code an array to contain that information.
Thanks for replying.

I don't really have any code yet. But what i was thinking is that there's gotta be an easier way to fill in the temperatures than what i've thought of, which is to make an int for every temperature (if that would work at all).

int mintemp1, mintemp2, mintemp3, mintemp4, mintemp5, mintemp6, mintemp7;
int mintemp8, mintemp9, mintemp10, mintemp11, mintemp12, mintemp13, mintemp14;
int mintemp15, mintemp16, mintemp17, mintemp18, mintemp19, mintemp20;
int mintemp21, mintemp22, mintemp23, mintemp24, mintemp25, mintemp26;
int mintemp27, mintemp28, mintemp29, mintemp30, mintemp31;

And the program should then ask me to fill in the temp. for the number of days that i type in, and forget about the rest of the ints/days.

This is not how you would do it, right?
And yes, i have to specify the number of days, not just bringing up some pre-made calendar.
int mintemp1, mintemp2, mintemp3, mintemp4, mintemp5, mintemp6, mintemp7;
int mintemp8, mintemp9, mintemp10, mintemp11, mintemp12, mintemp13, mintemp14;
int mintemp15, mintemp16, mintemp17, mintemp18, mintemp19, mintemp20;
int mintemp21, mintemp22, mintemp23, mintemp24, mintemp25, mintemp26;
int mintemp27, mintemp28, mintemp29, mintemp30, mintemp31;
When you have this, it means it's time to use an array.
int mintemp[31];
See http://www.cplusplus.com/doc/tutorial/arrays/ for more info.
Ok, i've discovered that i don't really need to store every temperature that i enter. It's enough that i just add them all together and divide on the number of days (i need to find the average temperature). So i won't need all those ints.

What i need to find out now is how to make it ask for the temperature for only the entered number of days. You know, if i enter 28, then i will have to enter 28 temps.

I'm guessing that a for-loop is needed here?

I tried putting something together (this actually seems to work as intended):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int days, mintemp, maxtemp, j, sumtemp;

int main()
{
	cin >> days;
	for (j = 1; j < days+1; j++)
	{
		cin >> maxtemp;
		sumtemp += maxtemp;
	}	
	cout << sumtemp / days;


return 0;
}


Is there something better to replace (j = 1; j < days+1; j++) with? Seems a little unprofessional.
And what does that "j++" thingy do, really?
Yes, replace it with:
for (j = 0; j < days; j++)

j++ is the same as j = j + 1, so it just adds 1 to integer j.
Nice, thanks.
So does that mean that the j++ makes it count one and one from 0 to "days"?
By the way, when you need to do something n+1 times, check if <=n, not <n+1.
Here's a part of my program now. Is this very bad code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int days, mintemp, maxtemp,
j, daymin, daymax, mm, daymm;
float sumtemp1, sumtemp2, summm;

daymin = 1;
daymaks = 1;
daymm = 1;

cin >> days;
	}
	for (j = 0; j < days; j++)
	{
		cout << "\nLowest temp for day: "
			 << daymin++ << '\n';
		cin >> mintemp;
		while (mintemp < -70 || mintemp > 70)
		{
			cout << "\nLowest temp for day: "
				 << daymin-1 << " (From -70\370C to 70\370C)\n";
			cin >> mintemp;
		}
		sumtemp1 += mintemp;                                                            


Also, i'm supposed to use consts for the max and min temperatures, instead of hard coding them everywhere like i've done here. But i don't understand why and how i'm supposed to use it, since they are a range from -70 to 70, and not a single number. Any ideas?

Edit: nvm, figured it out
Last edited on
Topic archived. No new replies allowed.