Issue checking for an exception

I am adding in exception handling into my code and I have hit a bump in the road. I want to require that eight digits be entered for an account number and then check to ensure that condition evaluates to true. If it does not I want to throw my exception and ask the user to enter the account number again.

Before I enter my exception code I have a while loop checking if there is a problem. That way if the user enter the information wrong again the while loop will catch it and enter the exception code again.

My compiler says:
error: request for member 'length' in 'acct_num', which is of non-class type 'long int'|

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
        long acct_num; //Define a variable
        const long acct_num_length = 8; //Require account number be eight numbers in length
            cout << "Enter client's account number: "; //Send output to the console
            cin >> acct_num; //Capture input from the console
        while ((acct_num < 1) || (!cin) || (acct_num.length () != acct_num_length)) //No need to even check for the exception if the number is positive, but if it is, you cannot exit the loop until the correct information is entered.
        {
        try
            {
            if (!cin)
                throw runtime_error("You cannot enter characters when a number is expected"); //Throws this exception if the if statement evaluates to true.
            else if (acct_num == 0) //Checks to see if a zero was entered and if so an exception is thrown.
                throw runtime_error("You cannot enter a zero"); //Throws this exception if the if statement evaluates to true.
            else if(acct_num <0) //Checks to see if a negative number was entered and if so an exception is thrown.
                throw runtime_error("Number entered cannot be a negative number"); //Throws this exception if the if statement evaluates to true.
            else if (acct_num.length () != acct_num_length)
                throw runtime_error("You must enter 8 numbers for the account number"); //Throws this exception if the if statement evaluates to true.
            }
                catch(runtime_error &charater)  //Catches the thrown exception and creates a reference to negative
                {
                    cout << "I am sorry, but, an exception has occurred" << endl; //Send output to the console, letting the user know an exception has occurred.
                    cout << charater.what() << endl; //Sends thrown exception output to the console  using the functions from the <stdexcept> class.
                    cleanup(); //Simple function to cleanup the characters input when a number is expected.
                    cout << "Enter the client's account number as a whole number: "; //Send output to the console (Give the person another opportunity to enter the account number correctly
                    cin >> acct_num; //Capture input from the console
                }
                catch(runtime_error &zero)  //Catches the thrown exception and creates a reference to zero
                {
                    cout << "I am sorry, but an exception has occurred" << endl; //Send output to the console, letting the user know an exception has occurred.
                    cout << zero.what() << endl; //Sends thrown exception output to the console  using the functions from the <stdexcept> class.
                    cout << "Enter the client's account number as a whole number: "; //Send output to the console (Give the person another opportunity to enter the account number correctly
                    cin >> acct_num; //Capture input from the console
                }
                catch(runtime_error &negative)  //Catches the thrown exception and creates a reference to negative
                {
                    cout << "I am sorry, but, an exception has occurred" << endl; //Send output to the console, letting the user know an exception has occurred.
                    cout << negative.what() << endl; //Sends thrown exception output to the console  using the functions from the <stdexcept> class.
                    cout << "Enter the client's account number as a whole number: "; //Send output to the console (Give the person another opportunity to enter the account number correctly
                    cin >> acct_num; //Capture input from the console
                }
            catch(runtime_error &eight)  //Catches the thrown exception and creates a reference to negative
                {
                    cout << "I am sorry, but, an exception has occurred" << endl; //Send output to the console, letting the user know an exception has occurred.
                    cout << eight.what() << endl; //Sends thrown exception output to the console  using the functions from the <stdexcept> class.
                    cout << "Enter the client's account number as a whole number (must be 8 digits): "; //Send output to the console (Give the person another opportunity to enter the account number correctly
                    cin >> acct_num; //Capture input from the console
                }
        }


Thank you in advance for your help.
Troy
Last edited on
long acct_num; //Define a variable

line 15
else if (acct_num.length () != acct_num_length)

acct_num is a regular variable - you can't call a length() function on it.
Topic archived. No new replies allowed.