Exception Catching

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();
}
You will, in effect, end up with:
1
2
3
4
5
6
7
8
9
10
try
{
    cin >> choice;
    if (! (1 <= choice && choice <= 3) )
        throw std::out_of_range("choice out of range");
}
catch (const out_of_range &)
{
    cerr << "An error occurred. Please choose a valid menu item number.";
}


Now, that can be encapsulated in a function or a type, but that's the code that needs to be run to get that result.

You should catch by reference at least, and const reference if you can.
Please do not doublepost. The other thread: http://www.cplusplus.com/forum/general/219131/
Also, you will need to include the <stdexcept> library for std::out_of_range.

kbw wrote:
Now, that can be encapsulated in a function or a type, but that's the code that needs to be run to get that result.
To the OP: You can have a friend function (or just a function) which overloads the >> operator (std::cin >> type) that throws an error when the input is invalid.

1
2
3
4
5
6
7
std::istream& operator>>(std::istream& is, const class_type& rhs)
{
    // take input with is and throw error if input is invalid
    // this function should be called within a try block and of course catch blocks should follow it
    // or you could just ask the user to re-enter input in here
    return is; // return std::cin at the end
}
Last edited on
Topic archived. No new replies allowed.