C++ Menu Choice Error

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 :(

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <iomanip>
using namespace std;

int main()

{
    char choice;
    int distance;
    double time;


    const double A = 15.8,
                 C = 12.65,
                 E = 15.2,
                 S = 12,
                 O = 13.88,
                 W = 10.2;

    const int 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;
    }
    else if (choice == C)
    {
        cin >> distance;
        time = distance * C;
        cout << "The total time traveling was: " << time << endl;
    }
    else if (choice == E)
    {
        cin >> distance;
        time = distance * E;
        cout << "The total time traveling was: " << time << endl;
    }
    else if (choice == S)
    {
        cout << "For how many meters? ";
        cin >> distance;
        time = distance * S;
        cout << "The total time traveling was: " << time << endl;
    }
    else if (choice == O)
    {
        cout << "For how many meters? ";
        cin >> distance;
        time = distance * O;
        cout << "The total time traveling was: " << time << endl;
    }
    else if (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").
Last edited on
Hello!

If you wish to make it work, and you expect a character to be input, not an integer value, you have to change:

const int A_CHOICE = A

to

const char A_CHOICE = 'A';
const char C_CHOICE = 'C';

and you have to change:

if (choice == A)

to

if (choice == 'A')
{
...
}

If, on the other hand, you expect an integer, you use an integer value for each of your variables.

Last edited on
Thank you! I appreciate the feedback :)
Topic archived. No new replies allowed.