i am trying to use a while loop to display a menu system. my code works but i need help with error handling. how do i go about re-displaying the menu when an invalid selection has been made. I would also like to inform the user that an invalid selection was made. thanks
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
using std::hex;
using std::dec;
using std::oct;
using std::setbase;
cin.clear();//clears error state of stream
cin.ignore();//discard characters remaining in the input buffer
cout << "invalid selection,try again" << endl;
switch(option)
{
case '1':
{
cout << "Enter a decimal number: ";
cin >> decimal;
cout << decimal << " in octal is: "
<< oct << decimal << '\n' << endl;
break;
}
case '2':
{
cout << "Enter a decimal number: ";
cin >> decimal;
cout << dec << decimal << " in hexadecimal is: "
<< hex << decimal << '\n'<< endl;
break;
}
case '3':
{
cout << "goodbye" << endl;
system("pause");
exit(0);
break;
}
}
}
return 0;
}
if i select a valid menu option i get
invalid selection,try again
Enter a decimal number:
when selecting a valid menu option i do not want the statment above enter a decimal number. that should only appear for invalid menu selection
cout << "Please select an option" << endl;
cin >> option;
cin.clear();//clears error state of stream
cin.ignore();//discard characters remaining in the input buffer
cout << "invalid selection,try again" << endl;
There is no branching here. In C++ execution is sequential, and "invalid selection" is displayed indiscriminately here. You should defer displaying it until you know the selection is invalid (e.g. in the default case of the following switch.)
The only thing you need to do is add your algorithms to the act_on_choice () function. I have put a comment in case 1 and case 2, showing you were to add the code.
The way it works if the menu will keep getting displayed until user chooses 1, 2 or 3 from menu.
If you chose option 1 it will do octal conversion, show the answer, prompt user to enter any key and then return to menu.
If you chose option 2 it will do hex conversion, show the answer, prompt user to enter any key and then return to menu.
If you chose option 3 it will tell user they have exited and ask them to enter a key before closing console window.
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
usingnamespace std;
using std::hex;
using std::dec;
using std::oct;
using std::setbase;
//function declarations
void display_menu();
int get_choice ();
void act_on_choice ();
int main()
{
//Do Not Add Anything Else in Main
act_on_choice();//calling function
return 0;
}//end of main
//Defining functions
//function displays menu
void display_menu ()
{
system ("CLS");//clears screen
//this outputs your menu
cout << "Menu" << endl;
cout << "======" << endl;
cout << "(1) - Octal Conversion" << endl;
cout << "(2) - hexadecimal conversion" << endl;
cout << "(3) - Exit Menu" << endl;
cout << "=================================" << endl << endl;
}//end of function
//Function prompts user to make choice from menu and loops until user makes valid choice and then returns their choice
int get_choice ()
{
int choice = 0;//resets or initialises value of choice to ensure while loop entered
while (choice < 1 || choice > 3)//while loop used to keep displaying menu until user enters a valid menu choice - choice has to be 1, 2 or 3
{
display_menu ();//calling function display_menu()
cout << "Please enter your menu choice: ";//user prompted to select from menu
//read input from user
cin >> choice;//selection variable initialised using value input by user
cin.clear();//if users choice not an int and 1, 2 or 3 clears error state of stream
cin.ignore(100, '\n');//discard characters remaining in the input buffer
}//end of while loop
return choice;//users menu choice returned by function
}//end of function
//Function selects correct switch case based on user's menu choice
void act_on_choice ()
{
int choice = 0;
do//this do while loop continues until user selects option 3 from menu and then it terminated
{
choice = get_choice ();//declaring and initialising variable "choice" with value returned from get_choice()- variable holds user's menu choice
switch (choice)
{
case 1:{ //case 1 selected when user entered 1 from menu choice
system ("CLS");//clears screen
//put your code in here for octal conversion
system ("PAUSE");//used to keep display on screen
break;
}//end of case 1
case 2:{ //case 2 selected when user entered 2 from menu choice
system("CLS");
//put your code in here for hexadecimal conversion
system ("PAUSE");//used to keep display on screen
break;
}//end of case 2
default: //default case selected when user entered 3 from menu choice
cout << "You exited menu" << endl;//display message to user
system ("PAUSE");//used to keep display on screen
break;//end of default case
}//end of switch statement
}while (choice != 3);//end of do while loop
}//end of function
No worries mate hope it helps. I'm sure we all know how it feels when you are stuck at a problem and have tried loads of things but don't get anywhere.
I know I still get that feeling. ;)