So I was doing some code for input validation on my homework program and i have the correct code to make sure a number entered it correct but I thought, what if they enter a letter. We aren't required to make sure that the user entered a number but how would i do that?

Here is what I have right now:

cout << "\tAge(yrs.): ";
                    cin >> age;
                    
                        /*
                         Makes sure age is above 0 years. If not, user
                         goes through a while loop until they enter an
                         appropriate value.
                         */
                        
                        while(age<=0){
                            cout << endl
                                << "You did not enter a corect age in years! "
                                << "Please re-enter your age -- " << endl;
                            cout << endl << "\tAge(yrs.): ";
                            cin >> age;
                        }

Recommended Answers

All 5 Replies

This has been answered so many times you should just do a search.

Thanks for assuming I didn't do a search but its ok, I forgive you.

I did do a search but I couldn't find anything that I really understand. We didn't learn anything about strings in class yet so I can't do that. I know using a logical operand would work since characters have assigned ASCII numbers but I'm not sure how to go about doing this. Would I use isdigit? If so, is there another way of doing this because we havent learned it yet so they don't want us to use it.

Thanks for assuming I didn't do a search but its ok, I forgive you.

Gee, thanks. And I forgive you for leaving out important information...

We didn't learn anything about strings in class yet so I can't do that.

Then you can't do it yet. Wait until you learn strings.

Gee, thanks. And I forgive you for leaving out important information...

Didn't know there was a forum rule stating I had to state that I already did a search. Just fingured we could assume I did a search since it says all over the place that we should search before posting :p

Then you can't do it yet. Wait until you learn strings.

Ok, Thanks.

Didn't know there was a forum rule stating I had to state that I already did a search. Just fingured we could assume I did a search since it says all over the place that we should search before posting :p

Actually, you are in the minuscule minority. Extremely few people do a search so it's natural for us to assume you didn't either. In fact, many will ask a question with two visible threads already started on the exact same question. All they had to do was read the other threads, but they are too lazy to look.

Sorry you got lumped into the unwashed millions, but that's how it goes. Just let us know next time what you've done so we know you are in the intelligent group, and not with the monkeys... No insult intended... :icon_wink:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.