Ask your own question, for FREE!
Computer Science 19 Online
OpenStudy (vheah):

C++ help?? I have to write a program that uses the try, catch, throw method for error handling/validating user input.

OpenStudy (vheah):

This is the criterion I have to follow : 1. The string must not be empty. 2. A "+" sign may or may not be the first character (not a "-" sign). 3. If a "+" sign is present, at least one digit must follow that sign. 4. Scientific, exponential or engineering notation is NOT allowed. 5. No characters or blanks may exist in the string other than be ones mentioned above. 6. The valid integer must be between 0 and 255.

OpenStudy (vheah):

I have done some coding, but my logic if ( e > 255 || e < 0) throw 4; does not work and numbers greater than 255 is still being accepted. I also don't know how the code to accept an integer with + before it and not accept the - : example, +12 is allowed but not -12

OpenStudy (vheah):

for ( a = 0; a <text.length(); a++) if (!isdigit(text.at(a))) throw 3 is the code I typed in to keep invalid symbols, but this prohibit the + sign to work.

OpenStudy (vheah):

My code: try { //Getting integer as a string from the user cout<<"Enter an integer... ('e' to exit): "; getline(cin, text); //Test #1 : If input is equal to 'e', input is displayed to be valid and user can exit. if (text == "e") throw 1; //Test #2 : If length is 0, then string is empty and error is displayed. if (text.length() == 0) throw 2; //Test #3: If int 'e' is greater than 255 or less than 0, error is displayed. if ( e > 255 || e < 0) throw 4; //Test #4: Non-digits entered will display an error. for ( a = 0; a <text.length(); a++) if (!isdigit(text.at(a))) throw 3; //Exit the while loop when all tests are good. e = 0; } catch (int f) { if (f == 2) cout<<"\n ERROR... EMPTY STRING!"<<endl<<" Try again... "; if (f == 3) cout<<"\n ERROR... NOT ALL DIGITS/ACCEPTABLE SYMBOLS!"<<endl<<" Try again..."; if (f == 1) exit (0); if (f == 4) cout<<"\n ERROR... NOT IN BETWEEN 0-255!"<<endl<<" Try again..."; //Reset of sentinel to re-execute the loop. e = 255; }

OpenStudy (vheah):

Please help!

OpenStudy (rsmith6559):

I don't see variable e being declared, or a value assigned to it before it's used in the comparison. After you check for the input not being null and equal to 'e', you could put a scanf() in a try clause and then check to see if the number is below zero.

OpenStudy (vheah):

@rsmith6559 sorry about that. I didn't give the entire code. I realized that the problem was because it was taking as a string that's why it wouldn't follow the logic since it only accept the input as text. The logic would work if it was an integer but it's not.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!