loop and error handling

hi i am new to c++. please help.

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

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
85
86
87
88
89
90
91
92
  #include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
using std::hex;
using std::dec;
using std::oct;
using std::setbase;



int main() {

	//variable deceleration

 bool menu = true;
 int decimal;
 char option=NULL;



while (true) {



    cout << "******MENU******" << endl;
    cout << "1. Octal Conversion" << endl;
    cout << "2. hexadecimal conversion" << endl;
    cout << "3. quit" << endl;

    cout << "Please select an option" << endl;
    cin >> option;


 if (cin.fail())
 {
	 cin.clear();
     string strjunk="";
     getline(cin,strjunk);

     cout <<"invalid selection,please 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;
    }
Look at my code in the post above titled "Converting from C++ to C#".

I have done this with my menu in that post.
got it.thanks a mil rabmac!!!!
rabmac,after using ur method i now run into a problem where even if i input a valid menu option, my output of "invalid selection" still prints
Post your code and the error message mate.
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
using std::hex;
using std::dec;
using std::oct;
using std::setbase;



int main() {

//variable deceleration

bool menu = true;
int decimal;
char option=NULL;



while (true) {



cout << "******MENU******" << endl;
cout << "1. Octal Conversion" << endl;
cout << "2. hexadecimal conversion" << endl;
cout << "3. quit" << endl;

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;












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
Just putting my daughter to bed mate but will take a look soon and get back to you. Shouldn't be too long.
not a problem.really appreciate the help.
1
2
3
4
5
6
        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.)
Here is the code for you mate.

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.

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
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>

using namespace 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 
Last edited on
you genius!!!! thanks for that,will use it as a guide to code my own. thanks mate
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. ;)
Topic archived. No new replies allowed.