HELP with looping

hello! can someone tell me how can i make this loop individual for every test case and at the end make it combined.

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
  int days_of_rain = 0;
    int rain_per_day_inc = 0;
    int total_rain_days = 0;
    int total_rain = 0;

    int days_of_snow = 0;
    int snow_per_day_inc = 0;
    int total_snow_days = 0;
    int total_snow = 0;

    char choice;

    while (choice != 'n') {
        std::cout << "Add to (R)ain\n";
        std::cout << "Add to (S)now\n";
        std::cin >> choice;
        if (choice == 'R')
        {
            std::cout << "Enter the rain amount in inches: ";
            std::cin >> rain_per_day_inc;
            total_rain += rain_per_day_inc;
            total_rain_days += days_of_rain;
            days_of_rain++;
        } else if (choice == 'S') {
            std::cout << "Enter the snow amount in inches: ";
            std::cin >> snow_per_day_inc;
            total_snow += snow_per_day_inc;
            total_snow_days += days_of_snow;
            days_of_snow++;
        } std::cout << "Choice: Enter y to enter another or n to print statistics: ";
        std::cin >> choice;
    }
    std::cout << "Weather statistics: " << std::endl;
    std::cout << "There were " << total_rain_days
              << " day(s) of rain." << std::endl;
    std::cout << "There were " << total_rain/12 << " feet and "
              << total_rain%12 << " inches of rain." << std::endl;
    std::cout << "There were " << total_snow_days
              << " day(s) of snow." << std::endl;
    std::cout << "There were " << total_snow/12 << " feet and " << total_snow%12
              << " inches of snow." << std::endl;


if you run this code and input data, at the end when you click n to stop it gives all the values. how can i make it individual for rain and snow independently and then there at the very end out put all the data.
so for example it has to be something like this, after inputting at the values for rain just this would come out
"There were X day(s) of rain.
There were X feet and X inches of rain."
same for snow. please help, i know i did the loop wrong but how do i fix it

Last edited on
It is not that wrong. You may do following step to get what you want:

Move line 14 to 16 before the loop.
Use another variable for the '... enter another ....' (line 31). Important!
After the loop you can output the statistics according the initial choice with if (choice == 'R') ... like within the loop.
Topic archived. No new replies allowed.