Option menu

I want to make a program that has a menu like this

[1] - program to find the average
[2] - program to find the smallest number
[x] - quit

the user will only input either 1,2 or x
and anything that isn't in the choices
the output will be "invalid input "
the problem is if i enter x the output is still invalid output
BTW im using Turbo c++
this is the only compiler my teacher make us use

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
 #include<iostream.h>
#include<conio.h>
#include<string.h>
main()
{
 clrscr();
 int choice;
 cout<<"\n\n[1] - Program To find The Average";
 cout<<"\n\n[2] - Program To Find The Smallest number";
 cout<<"\n\n[x] - Quit";
 cout<<"\n\nEnter Your Choice : ";
 cin>>choice;
if(choice<3 && choice>0)
 {
  switch(choice)
   {
    case 1 :
     {
      int ent,num2,average=0;
      clrscr();
      cout<<"\n\nProgram to find The Average";
      cout<<"\n\nEnter How Many numbers: ";
      cin>>ent;
      for(int i=1;i<=ent;i++)
       {
	cout<<"enter number ["<<i<<"] :";
	cin>>num2;
	average+=num2;
       }
       cout<<"Average : "<<average/ent;
       getch();
       return main();
     }
    case 2:
     {clrscr();
      int ent,num2,smalles;
      clrscr();
      cout<<"\n\nProgram to find smallest number";
      cout<<"\n\nEnter How Many numbers: ";
      cin>>ent;
      for(int i=1;i<=ent;i++)
       {
	cout<<"enter number ["<<i<<"] :";
	cin>>num2;
	if(num2<smalles)smalles=num2;
       }
       cout<<"Smallest Number : "<<smalles;
       getch();


       return main();
     }
   }
 }
if(choice=='x')
 {
  clrscr();
  cout<<"terminated...";
  getch();
  return 0;
 }
else
  {
    clrscr();
    cout<<"invalid input "<<choice;
    getch();
    return 0;
   }


getch();
return 0;
}
Last edited on
Read the input as a char.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// ...
char choice ;
cin >> choice ;

switch(choice)
{
      case '1' :
          // ....

      case '2' :
          // ....

      case 'x' :
          // quit

      default :
          // invalid choice
}
This does repeat the problem:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int main()
{
    using std::cout;

    int choice;
    choice = 'x';
    cout << choice << "\nYour turn\n";
    choice = 0;
    std::cin >> choice;
    if ( 'x' == choice ) cout << "Ho ho ho\n";

    cout << "again\n";
    choice = 0;
    std::cin >> choice;
    if ( 'x' == choice ) cout << "Ho hum\n";
    
    cout << "\nToo late\n";
    return 0;
}

120
Your turn
x
again

Too late

I did type x and it was like it didn't ask the second time.

120
Your turn
120
Ho ho ho
again
x

Too late

I did type first 120 and then x and the second time x!=x, but curiously 120==x.

The problem is: int choice; You do read into an integer variable. x is not an integer and the formatted input sets the stream into state fail. You have to clear the state before any input operation will succeed.

On the system, where I tested my program, the character 'x' has numeric value 120. That is why typing 120 did seem to enter 'x'. The 'x' may have different value in different systems.

Besides, who wants to type 1 or 2 or 120?

You can:
* Use a number (like 0) instead of x
* Read a character '1' or '2' or 'x'
* Do all the necessary magic to successfully mix numeric and character input.

(The last is probably the most educative choice, but are you ready for it?)
Topic archived. No new replies allowed.