Exiting a program
Jan 11, 2015 at 4:09am UTC
Below is a prototype menu for a simple/complex number calculator I'm making for an assignment. One of the criteria of the assignment is the user being able to press Q for exiting the program. At the moment I can do it with numbers, but I don't know how to do it with characters. Advice would be appreciated.
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
#include <iostream> // saves basic input output functions
using namespace std;
int main()
{
int option; // user input is saved here
do //do while loop, shows menu until user exits
{
// shows Options for the menu
cout << "1) Simple Calculator " << endl;
cout << "2) Complex Calculator " << endl;
cout << "3) Calculate Capacitance " << endl;
cout << "4) Calculate Reactance "
cout << "5) Exit Program " << endl;
//user input's a number
cout << "Please select an option : " ;
cin >> option; // taking option value as input and saving in variable "option"
if (option == 1) // Simple Calc
{
}
else if (option == 2) // Complex Calc
{
}
else if (option == 3) // Capacitance
{
}
else if (option == 4) // Reactance
{
}
else if (option == 5) // Exit
{
cout << "Terminating Program" << endl;
}
else
{
//Displaying error message
cout << "Invalid Option entered" << endl;
}
}
while (option != 5); //condition of do-while loop
return 0;
}#
Jan 11, 2015 at 5:24am UTC
@Aromasin
Just check with char, instead of int.
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
#include <iostream> // saves basic input output functions
using namespace std;
int main()
{
char option; // user input is saved here
do //do while loop, shows menu until user exits
{
// shows Options for the menu
cout << "1) Simple Calculator " << endl;
cout << "2) Complex Calculator " << endl;
cout << "3) Calculate Capacitance " << endl;
cout << "4) Calculate Reactance " << endl << endl;
cout << "Q) Quit Program " << endl;
//user input's a number
cout << "Please select an option : " ;
cin >> option; // taking option value as input and saving in variable "option"
option = toupper(option); // check for upper case 'Q'
if (option == '1' ) // Simple Calc
{
cout << "Do a simple calc.." << endl << endl;
}
else if (option == '2' ) // Complex Calc
{
cout << "Do very complex calculations." << endl << endl;
}
else if (option == '3' ) // Capacitance
{
cout << "Do some Capacitance calcs here." << endl << endl;
}
else if (option == '4' ) // Reactance
{
cout << "Do a little Reactance calc here." << endl << endl;
}
else if (option == 'Q' ) // Exit
{
cout << "Terminating Program" << endl << endl << endl;
}
else
{
//Displaying error message
cout << "Invalid Option entered" << endl << endl;
}
} while (option != 'Q' ); //condition of do-while loop
cout << "Program is finished!!" << endl << endl << endl;
return 0;
}
Jan 11, 2015 at 5:24am UTC
Try using
char
type variables.
1 2 3 4 5 6
char example;
cout << "Enter a character: " ;
cin >> example;
if (example == 'Q' || example == 'q' ) {
cout << "You entered Q or q" ;
...
Jan 11, 2015 at 5:39am UTC
Ah, that was my issue, didn't put the 1, 2, 3, 4 and Q in quotations. Thanks guys. More questions to come I imagine.
Topic archived. No new replies allowed.