Help with homework

Hello guys! i need some help with a hw assignment and i am kinda stuck with this since i dont understand loops too well...

i need to capture the amount of days of rain
amount of inches per day and total it with all the rain days

note*i need to make a menu to choose either R or S for rain or snow and proceed accordingly*
but right now im just focusing with rain. Ignore the snow related variables :)

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

int main() {

    int days_of_rain = 0;
    int rain_per_day_inc = 0;
    int total_days_of_rain = 0;
    int days_of_snow = 0;
    int snow_per_day_inc = 0;
    int total_days_of_snow = 0;
    char choice;
    int yes = 1;
    int no = 2;

        std::cout << "To add to rain press 'r'";
        std::cin >> choice;
        if (choice == 'y') {
            std::cout << "Enter the rain amount in inches: ";
            std::cin >> rain_per_day_inc;
        }
    while (choice != no){
        if (choice == 'r') {
            std::cout << "Enter the rain amount in inches: ";
            std::cin >> rain_per_day_inc;
        }
        std::cout << "Enter y to enter another or n to print statistics: ";
        std::cin >> rain_per_day_inc;
    }

    return 0;
}


here is where i am stuck at, when i try to n or y to stop it loops for ever. how can i fix this problem?
Last edited on
Hello gjur99,

Look at the while condition. you are comparing "choice", a char, to (no), an "int". This should have been a compile error or at least a warning.

What would work better is: while (choice != 'n'). And on line 27 the input is to the wrong variable. Since you are using "choice" for two different uses you might want to consider changing the "choice" in the while condition to another name.

This is what I see for now. I will have to load up the program and test it further.

hope that helps,

Andy
right! in other to change the name in while i need to make another variable or do i just use one that i already have?

Hello gjur99,

To answer your question creating another variable would be best. Using the same variable for two different uses close together is a problem. Check the comments I put in your code below.

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

int main()
{

	int days_of_rain = 0;  // This works just as well "days_of_rain{}"
	int rain_per_day_inc = 0;
	int total_days_of_rain = 0;
	int days_of_snow = 0;
	int snow_per_day_inc = 0;
	int total_days_of_snow = 0;
	char choice;
	int yes = 1;  // <--- These two not really used or needed.
	int no = 2;

	std::cout << "To add to rain press 'r'";
	std::cin >> choice;

	if (choice == 'y')  // <--- "choice" is given the value of "r", but you are comparing it to "y".
		                // The condition will never be true.
	{
		std::cout << "Enter the rain amount in inches: ";
		std::cin >> rain_per_day_inc;
	}

	while (choice != no)  // <--- Comparing a "char" to an "int". At this point "choice is still
		                  // equal to "r".
	{
		if (choice == 'r')
		{
			std::cout << "Enter the rain amount in inches: ";
			std::cin >> rain_per_day_inc;
		}
		std::cout << "Enter y to enter another or n to print statistics: ";
		std::cin >> rain_per_day_inc;  // <--- Wrong variable. "choice" is what you want, but then
		                               // the if statement would quit working.
	}

	return 0;
}

This points out the problems in the program, but it really needs reworked to be done properly.

Hope that helps,

Andy
Topic archived. No new replies allowed.