Loop just goes on..

Hi

Im new to c++ and tries to solve this:

Create a program that finds the average min.temp,
max.temp, rainfall and total rainfall a given month. The program will work as follows:
First, the computer asks for the number of days in the month. This should be a number between 28 and 31
Then goes through as many days (as the user typed), and for each of them
queried for the given current min.temp, max.temp and mm rainfall.
Min.temp. should be in the range of -70 to 70, Max.temp should be in the range min.temp to 70
Rainfall should be in the range of 0 to 200
When all data fully scanned, the following shall be printed on the current month:
 Average min.temp
 Average max.temp
 Average rainfall
 Total rainfall
Finally, ask the user if he / she will read the data for another month.
If so, start the program at the point where it is asked about the number of days in the month.

Otherwise, exit the program with a positive / pleasant message.
NB1: All figures scanned may well be integers, while the averages printed
should be float.
NB2: Use const as much as possible


Im a bit stuck in the loop here...

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

using namespace std;

int main ()	{

int day;
int day_var = 0;

float min_temp;
float max_temp;
float low_temp;
float high_temp;
float rain;
float avg_tempMax;
float avg_tempLow;
float avg_rain;

char ch;

const int LOW_TEMP = -71;
const int HIGH_TEMP = 71;
const int HIGH_RAIN = 201;
const int LOW_RAIN = -1;


do	{
	do {
		cout << "How many days in the month?: ";
		cin >> day;
		
		} while ( day != 28 && day != 29 && day != 30 && day != 31);

		
	do {
		cout << "Todays lowest temp, ";
		cout <<	"higest temp. and rainfall: ";
		cin >> low_temp >> high_temp >> rain;

			} while (low_temp > LOW_TEMP || low_temp < HIGH_TEMP || 
				  high_temp > LOW_TEMP || high_temp < HIGH_TEMP               || rain > LOW_RAIN || rain < HIGH_RAIN );

		for (day_var = 0; day_var >= day; day_var++);


	avg_tempLow = low_temp / day;
	avg_tempMax = high_temp / day;
	avg_rain = rain / day; 

	cout << setw(24) << "Avrage min.temp: " << 
		    setw(6) << avg_tempLow << "\n";
	cout << setw(24) << "Avrage max.temp: " << 
			setw(6) << avg_tempMax << "\n";
	cout << setw(24) << "Average rainfall: " << 
			setw(6) << avg_rain << "\n";
	cout << setw(24) << "Total rainfall: " << setw(6) << rain << "\n";
	cout << "\n\nRun again? (Y/n)";
	rain = 0;
	avg_tempLow = 0;
	avg_tempMax = 0;
	avg_rain = 0;
	cin >> ch;
} while ( ch != 'n' );

cout << "\Have a nice day!! :)";
What exactly is your problem?

Edit: woops forgot what the title was by the time I read the program.
Last edited on
the loop never ends. it just asks for "Todays lowest temp., highest temp. and rainfall
1
2
3
while (low_temp > LOW_TEMP || low_temp < HIGH_TEMP || 
				  high_temp > LOW_TEMP || high_temp < HIGH_TEMP               || rain > LOW_RAIN || rain < HIGH_RAIN );


Pretty sure that your problem is in there. You need to think about what each of those comparisons is doing and when they would be true. Remember that if even one of them is true then the whole loop while run again and ask for the user's input.

For instance, low_temp > LOW_TEMP will be true if the user inputs a temperature of -70 or above, so it will make the loop run again. This is not what you want. You want it to run again if the temperature entered is less than -70, not more. Rethink each of the comparisons in this while loop. Fixing them should solve the problem.
Hi

Thanks for the help, but when i change it to && (so all of them has to be true, isn't that right?) it only breaks out of the code when I give a answear thats higher then 70.

1
2
3
} while (low_temp > LOW_TEMP && low_temp < HIGH_TEMP && 
				  high_temp > LOW_TEMP && high_temp < HIGH_TEMP &&
				  rain > LOW_RAIN && rain < HIGH_RAIN );
You're doing the comparisons opposite of how you need to. You want this while loop to be true when the user inputs data incorrectly, yes? So you need to test if their input is outside the bounds set by LOW_TEMP, HIGH_TEMP, etc. Take this part of it, for example:

 
low_temp > LOW_TEMP && low_temp < HIGH_TEMP


low_temp is the users' inputted temperature, which we want to be between -70 and 70. What you're doing with low_temp > LOW_TEMP is testing if it is GREATER than -71, and with low_temp < HIGH_TEMP you are testing if it is LESS than 71. What you want to do is test for if it is LESS than -70, or GREATER than 70.

To start, you should assign LOW_TEMP and HIGH_TEMP the exact values given in the problem (-70 and 70, respectively), then reverse the > or < sign in the comparisons (i.e., low_temp > LOW_TEMP should instead be low_temp < LOW_temp).

Then, continue this logic for the other comparisons.
Last edited on
I got that one now :) thanks.

but the program only give me the avrage of one day instead of asking me as many times as the imput "day"
You need to include a for loop that runs for the # of times that they inputted.
Topic archived. No new replies allowed.