Exception Catching
Jul 17, 2017 at 7:46am UTC
I'm trying to do a try catch in line 21, so if the user types something other than 1, 2, or 3 it will give them an error. This is wrong, so how would I do this correctly?
1 2 3 4 5 6 7 8
try
{
cin >> choice;
}
catch (out_of_range)
{
cerr << "An error occurred. Please choose a valid menu item number." ;
}
main.cpp
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 93 94 95 96 97 98
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include "Record.h"
using namespace std;
class Recordsbook
{
vector<Record> book;
int x;
Record current;
public :
void Menu()
{
int choice = 0;
cout << "---Main Menu---\n" ;
cout << "1. Input information\n" ;
cout << "2. Display records\n" ;
cout << "3. Exit\n" ;
cin >> choice;
switch (choice)
{
case 1:
InputInfo();
break ;
case 2:
Display();
break ;
case 3:
Exit();
break ;
}
}
void InputInfo()
{
string temp;
cout << "How many records would you like to input?" ;
cin >> x;
while (x<10)
{
cout << "You must input at least 10 records. Please input a bigger value.\n" ;
cin >> x;
}
for (int i = 0; i<x; i++)
{
cout << "What is the record number? " ;
cin >> temp;
current.setRecNum(temp);
cout << "What is the first name? " ;
cin >> temp;
current.setFirstName(temp);
cout << "What is the last name? " ;
cin >> temp;
current.setLastName(temp);
cout << "What is the age? " ;
cin >> temp;
current.setAge(temp);
cout << "What is the telephone number? " ;
cin >> temp;
current.setTelephone(temp);
}
Menu();
};
void Display()
{
int i = 0;
for (i=0; i<x; i++)
{
cout<<"Record number: " ;
cout<<current.getRecNum();
cout<<"\n" ;
cout<< "First name: " ;
cout<<current.getFirstName();
cout<<"\n" ;
cout<< "Last name: " ;
cout<<current.getLastName();
cout<<"\n" ;
cout<< "Age: " ;
cout<<current.getAge();
cout<<"\n" ;
cout<< "Telephone number: " ;
cout<<current.getTelephone();
cout<<"\n" ;
cout<<"\n" ;
}
Menu();
};
void Exit()
{
cout << "Closing..." ;
};
} Recordsbook;
int main()
{
Recordsbook.Menu();
}
Jul 17, 2017 at 8:14am UTC
Something like this, perhaps:
1 2 3 4 5 6 7 8 9 10 11 12
int get_choice()
{
std::cout << "enter choice 1/2/3: " ;
char choice ;
if ( std::cin >> choice && choice > '0' && choice < '4' ) // valid choice: '1' '2' or '3'
return choice - '0' ; // '2' - '0' == 2 etc.
// invalid choice. try again
std::cerr << "invalid choice. try again\n" ;
return get_choice() ; // try again
}
And then:
1 2 3 4 5 6 7 8 9 10 11
void Menu()
{
std::cout << "---Main Menu---\n" ;
// etc
const int choice = get_choice();
switch (choice)
{
case 1:
// etc
Jul 17, 2017 at 8:33am UTC
I would just use an if statement, but I'm trying to work on exception catching. I couldn't find anything that helped me understand how to use it in this situation.
Jul 17, 2017 at 8:55am UTC
This is not a good use-case for using exceptions. However, this is how it could be used:
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
#include <iostream>
int get_choice()
{
std::cout << "enter choice 1/2/3: " ;
char choice ;
if ( std::cin >> choice )
{
if ( choice > '0' && choice < '4' ) return choice - '0' ;
else throw std::out_of_range( "invalid choice" ) ;
}
else throw std::runtime_error( "input failure on stdin" ) ;
}
int main()
{
int choice = 0 ;
while ( choice == 0 ) // keep trying till we get a valid choice
{
try { choice = get_choice() ; }
catch ( const std::out_of_range& ) { std::cerr << "invalid choice. try again\n" ; }
catch ( const std::runtime_error& ) // input failure
{
std::cerr << "input failure on stdin. clearing the failed state. try again\n" ;
std::cin.clear() ; // clear the failed state and try again
}
}
std::cout << "choice is " << choice << '\n' ;
}
Topic archived. No new replies allowed.