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?
#include <iostream>
usingnamespace 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 . . .
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?
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.
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.
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.