I am attempting to write a program that is capable of outputting a menu that the user can select a choice from then calculate time of bird's flight. However, I am getting the "invalid choice" error even when I have entered a valid choice. Please help :(
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
char choice;
int distance;
double time;
constdouble A = 15.8,
C = 12.65,
E = 15.2,
S = 12,
O = 13.88,
W = 10.2;
constint A_CHOICE= A,
C_CHOICE= C,
E_CHOICE= E,
S_CHOICE= S,
O_CHOICE= O,
W_CHOICE= W;
cout << "\t\tBird Timer Menu \n\n"
<< "A. African Swallow \n"
<< "C. African Swallow + Coconut \n"
<< "E. European Swallow \n"
<< "S. European Swallow + Coconut \n"
<< "O. Owl \n"
<< "W. Owl + Coconut \n\n"
<< "Enter the bird: ";
cin >> choice;
cout << fixed << showpoint << setprecision(4);
if (choice == A)
{
cin >> distance;
time = distance * A;
cout << "The total time traveling was: " << time << endl;
}
elseif (choice == C)
{
cin >> distance;
time = distance * C;
cout << "The total time traveling was: " << time << endl;
}
elseif (choice == E)
{
cin >> distance;
time = distance * E;
cout << "The total time traveling was: " << time << endl;
}
elseif (choice == S)
{
cout << "For how many meters? ";
cin >> distance;
time = distance * S;
cout << "The total time traveling was: " << time << endl;
}
elseif (choice == O)
{
cout << "For how many meters? ";
cin >> distance;
time = distance * O;
cout << "The total time traveling was: " << time << endl;
}
elseif (choice == W)
{
cout << "For how many meters? ";
cin >> distance;
time = distance * W;
cout << "The total time traveling was: " << time << endl;
}
else
{
cout <<"The valid choices are A, C, E, S, O, and W. Run the program\n"
<<"again and select one of those.\n";
}
return 0;
}
Your prompt suggests that you want your user to type in characters (A, C, E, etc.) not numbers (15, 12, 15, etc.).
Remove your x_CHOICE variables because they don't seem to be doing anything useful.
Instead of if (choice == A)
do if (choice == 'A') // comparing to a character! not an integer.
repeat with the other == if statements as well.
'W' is a character. W is the variable that you assigned a value of 10.2.
In general, you should not make one-letter variable names (unless if it's a simple loop counter). You should use more descriptive variables names -- don't just use "W", use owl_coconut_time (or something, it's subjective, but anything is better than "W").