Override invalid entry, with constructor?

Is it possible to use a constructor default to override an invalid entry?

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
#include <iostream>
using namespace std;

class something
{
private:
    int a;
public :
    something(int b=1)
    {
        a=b;
    }
    void the_function()
    {
        cout << "enter #1 :\n";
        cin >> a;
        while (a!=1) // is it possible to make this skip to the default, if user enters invalid entry?
        {cout << "try again";
            cin >> a;
        }
    }
    void display()
    {cout << a;}
};

int main ()
{
    something obj1;
    obj1.the_function();
    obj1.display();
}


.. so instead of looping through the while statement, once an invalid entry has been made, is there anyway to make the default value be used for 'a' and skip right to the display function? thanks for any help
Last edited on
Topic archived. No new replies allowed.