Prevent errors from accidental menu entries?

Here's my code & output. I can't figure out how to keep my program from turning accidental menu option entries into repeating the menu or, worse, putting accidental menu option numbers into calculations. How can i fix these problems?

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
	#include <iostream>
	using namespace std;

	// Function Prototypes
	double calcSum(double, double);
	double calcProduct(double, double);
	 
	int main()
	{
	// Declare Variables
    char option = ' ';
	bool repeat = true;
	double number1 = 0.0;
	double number2 = 0.0;
	double sum = 0.0;
	double product = 0.0;

	// Display Menu - should repeat until user chooses to exit
	// it should repeat if user enters incorrect menu option

		do     //begin do...while loop
	    {
	        cout << "1  Add" << endl;
	        cout << "2  Multiply" << endl;
	        cout << "3  Exit" << endl;
			cout << "Enter an option:  ";
	        cin >> option;
			
			switch (option)     //begin switch statement, which includes processing
			{
			case '1':
				cout << endl;
				cout << "Enter a number:  ";
				cin >> number1;
				cout << "Enter another number:  ";
				cin >> number2;

				sum = calcSum(number1, number2);  //function call
				cout << endl;
				cout << number1 << " + " << number2 << " = " << sum;
				cout << endl << endl;
				break;

			case '2':
				cout << endl;
				cout << "Enter a number:  ";
				cin >> number1;
				cout << "Enter another number:  ";
				cin >> number2;

				product = calcProduct(number1, number2);  //function call
				cout << endl;
				cout << number1 << " * " << number2 << " = " << product;
				cout << endl << endl;
				break;

			case '3':
				cout << endl;
				cout << "Program exited" << endl;
				repeat = false;
				break;

			default:
				cout << endl;
				cout << "Invalid entry. Please try again." << endl;		
			}  //end switch
				
		} while (repeat);  //end do...while
		
		system("pause");
		return 0;
}  //end of main function

//*****Function Definitions*****

double calcSum(double num1, double num2)  //function header
{
	return num1 + num2;
}  //end of calcSum function

double calcProduct(double num1, double num2)  //function header
{
	return num1 * num2;
}  //end of calcProduct function 


Sample of problem output:

1  Add
2  Multiply
3  Exit
Enter an option:  zzz

Invalid entry. Please try again.
1  Add
2  Multiply
3  Exit
Enter an option:
Invalid entry. Please try again.
1  Add
2  Multiply
3  Exit
Enter an option:
Invalid entry. Please try again.
1  Add
2  Multiply
3  Exit
Enter an option:  123

Enter a number:  Enter another number:  4

23 + 4 = 27

1  Add
2  Multiply
3  Exit
Enter an option:  2

Enter a number:  3
Enter another number:  4

3 * 4 = 12

1  Add
2  Multiply
3  Exit
Enter an option:  3

Program exited
Press any key to continue . . .



thanx for any clues!!
Simple, you are using cin >> input as your input from the user but it is declared as type char so the input buffer from typing zzz has three characters in it, so you had to go through all three characters before anything else could be handled. Either flush the buffer or change how you accept input. getchar() works great in this situation since it gets a character and ONLY a character.
I looked up the reference posts here about getchar(), but I have no clue on how to use it. I'm taking a very basic class. Haven't learned about this function or how to flush the buffer. Could you show how I might use getchar() in this situation?

We did study the ignore function already. I tried inserting cin.ignore(); after getting option input from user, but it didn't help much.

Also, do you need the string directive (#include<string>) to use the getchar() function?

Thank you for pointing me in the right direction.
Can I ask why you are using char when you want to retrieve an int? -_-
closed account (zb0S216C)
hbjgd wrote:
Can I ask why you are using char when you want to retrieve an int? (sic)
char is effectively a integral type, but the integral value is converted to the corresponding character based on the native character set. A char is capable of storing an integer, and if you don't convert the value to character, it's a tiny integer type with a range far less than that of short's.

Wazzak
Last edited on
oooooohhh.... i got them backwards. I thought when you store a char as an int it would be converted. But now that I think about it, there aren't enough characters in the alphanumeric system to cover all the ints. MY BAD. =(
Can I ask why you are using char when you want to retrieve an int? -_-


Sure. Just followed an example out of the book, which advised using char for input that won't be used in calculations. I converted the option variable to the integer data type, however, and voila, the problem is solved. So, thanks for asking. I'm still up for learning about getchar(), however.

Framework, I appreciated your insight into the char/int relationship.

I'm still up for learning about getchar(), however.


instead of doing this.

1
2
cout << "Enter an option:  ";
	        cin >> option;


you would do this.

1
2
cout << "Enter an option:  ";
	       option = getchar();
Last edited on
@hbjgd

I had tried that already, and it didn't fix the problem, so I figured there was something else to it.

Then I tried

1
2
3
cout << "Enter an option:  ";
option = getchar();
cin. ignore();


but that didn't help, either.

The only discernible effect the cin.ignore(); instruction had on my program was to reduce the number of "hiccups" to repeating the menu with accidental key presses. The option = getchar(); didn't help at all, though.

Sorry I missed this topic, cin.flush() will flush out the input buffer if I remember correctly but cin isn't what you want here as I stated.

 
char option = getchar();


That should work because it will wait for input, and when you press a key the program stops taking input so you can only enter one key. At this point your variable option should be a character. Then you can check for the character by comparing it to '1' and not option == 1 because as a character, the 1 isn't 1 since the character 1 is represented by the a different ascii code number which is what you really want to test for and putting the one inside single quotes converts it for you automatically.
Thank you WilliamW1979. I'll try that after I figure out this other program.
Topic archived. No new replies allowed.