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...
#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;
constint Foot = 12;
constchar R = 'r';
constchar S = 's';
constchar Y = 'y';
constchar 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;
}