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'|
long acct_num; //Define a variable
constlong 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.
elseif (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.
elseif(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.
elseif (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
}
}