Please help me!

Hello! i wrote this code as a personal project, but i dont know how to do the following: In the start of the program, when it asks for "do you want to enter the program for rain or snow (y/n):" how can i make it so that if they enter another letter besides 'y' or 'n' to output error!
ive tried some things but none of them seems to work...

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

int main() {
    int days_of_rain = 0;
    int rain_inches = 0;
    int days_of_snow = 0;
    int snow_inches = 0;
    int temporary_storage = 0;
    const int Foot = 12;
    const char R = 'r';
    const char S = 's';
    const char Y = 'y';
    const char N = 'n';
    char choice = ' ';


        std::cout << "do you want to enter the program for rain or snow (y/n): ";
        std::cin >> choice;

    while (choice == Y) {
        std::cout << "Enter 'r' for rain or 's' for snow: ";
        std::cin >> choice;
        switch (choice) {
            case R:
                std::cout << "Enter the rain amount in inches: ";
                std::cin >> temporary_storage;
                rain_inches += temporary_storage;
                days_of_rain++;
                temporary_storage = 0;
                break;

            case S:
                std::cout << "Enter the snow amount in inches: ";
                std::cin >> temporary_storage;
                snow_inches += temporary_storage;
                days_of_snow++;
                temporary_storage = 0;
                break;
            default:
                std::cout << "Error enter only r or s! ";
        }
        std::cout << "do you want to enter the program for rain or snow again? (y/n):";
        std::cin >> choice;
    }
    if ( days_of_rain > 0){
    std::cout << "There were " << days_of_rain << " day(s) of rain with " << rain_inches/Foot << " ft and "
    << rain_inches << " inches." << std::endl;
        } else std::cout << "There was no rain!\n";
    if (days_of_snow > 0) {
        std::cout << "There were " << days_of_snow << " day(s) of snow with " << snow_inches / Foot << " ft and "
                  << snow_inches << " inches." << std::endl;
    }else std::cout << "There was no snow!\n";

    return 0;
}
You would use a if statement
such as pseudo
1
2
 if y or n or Y or N
else print error
.
Topic archived. No new replies allowed.