Question:
Making an error message in C++?
Rylee
2013-01-19 07:52:56 UTC
I was wondering if anyone could help me or point me in the right direction. I have an assignment and part of it I need to create an error message when the user inputs numbers instead of letters for the name input. I am lost on how to do this. I have read my textbook and searched Google through and through but I can’t find an answer. Does anyone have any helpful suggestions?
Six answers:
Gordon
2013-01-19 07:59:14 UTC
Use the try and catch statement. All your code in the try block. Then when there is an error te catch code block with execute.



Define a try/catch like this:

Try

{

//your code

}

catch( ... )

{

//Error message

}
husoski
2013-01-19 08:38:02 UTC
Add to your includes and you'll have character classification functions:



isalpha(c) -- returns true if c is a letter (upper or lower case)

isspace(c) -- returns true if c is a "whitespace" character (space, tab, newline)

isdigit(c) -- returns true if c is a digit 0-9



If you care about upper and lower case in names, there are isupper(c) and islower(c) functions to test for that, plus toupper(c) and tolower(c) functions for conversions. In all cases, c is either a char or an int that contains a character code.



So, you can loop through a C++ string.



// declare these in advance:

string name;

bool good_name = true;



// prompt for the name:

cout << "What is your name? ";

getline(cin, name);



// Test for an empty name or illegal first character:

if (name.empty()) || !isalpha(name[0])

{ good_name = false; }



// if name start is good, test the rest of the name:

for (size_t i=1; good_name && i
{

int c = name[i];

if (!isletter(c) && !isspace(c)) // if not a letter and not a space

{ good_name = false; }

}



// test result

if (!good_name)

{

cout << "That's not a good name!" << endl;

}

else

{

cout << "Hello, " << name << "." << endl;

}



If you are using C strings (char[] array or char* pointer) then you'll use strlen(name) instead of name.length(), and cin.getline(name) instead of the getline(cin,name) function, but most everything else should be the same.



You can use that good_name boolean to control a while or do while loop to repeat the prompt after a test fails. You should have seen examples of that in class.
_Object
2013-01-19 08:07:01 UTC
use atoi() or some other String to Int function, and only if the function returns null (atoi() returns null upon failure) can you continue. This is not the best solution, because if the user inputs the number 0 (or NULL), It will pass the test.

You could print the numbers into an array and read them against a equal ASCII string.

Or you could use something like this.

int Game::validateStrInput( std::string * str )

{

std::cout << std::endl;

std::cin >> ( *str );

( *str );

for ( int i = 0; ( ( unsigned int )i < ( str->size( ) ) ); i++ )

{

if ( isdigit( ( *str )[ i ] ) )

{

continue;

}

else

{

std::cout << "Please Enter a valid string."; //ERROR

validateStrInput( str );

}

}

return ( guess = std::stoi( ( * str ) ) );

}

This is still clunky, because It's recursive, it will print your error message for each invalid character. It also uses both the hideous break and continue statements. You should make it better.

I would start by breaking it down into more functions and checking their single return value.

But. Don't forget this! It's better and more elegant than any of my statements above.

try

{

//...

}

catch

{

//Handle your ERROR!

}

Good luck.
Maya
2013-01-19 07:59:13 UTC
Assuming you are asking how to differentiate number and letters from the string that you read, you just need to loop through the character within the string, and call the isdigit() function to check whether it's a number or not.
roger
2013-01-19 08:55:52 UTC
easy



scan through the string. If you find a character >='0' and <= '9' then you have a number then output your error message.
runkle
2016-08-06 08:01:02 UTC
Your software should not proceed when the user enters flawed knowledge. Here is what you can do: -Request a number from the person -user enters quantity -check if the number is valid -If not, tell the person the number is invalid and ask again. -if it is valid, proceed along with your progam.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...