Help with cin verification and do-while looping

Ok, I'm having a little bit of a hard time here. You see, I was planning to loop until the user enters a valid input. I only need a numeric input, nothing else.

THE POINT IS:
I kind of did it.
But still, you see, while using the isalpha and the isalnum functions for verification, the cout repeats itself into the number of characters invalid input is. But that's not what I want :(
Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# include<iostream>
# include<ctype.h>

using namespace std;
main()
{
int number;
cout << "Input a number not greater than 100: ";
cin >> number
do
{
cin.clear();
cin.ignore();
cout << "Please enter a valid number: ";
cin >> number;
}
while (isalpha(number) || ( !isalnum(number) || number > 100);

cout << "Your number is " << number << ".";

system("PAUSE");
return 0;
}


You could say my keywords for searching fail 'cuz I couldn't find anything in regards to this matter. I just need an incy weency ounce of your help. I'd be happy if you could provide me links over an article I could read involving this, happier if you could provide me an example I can model after, and the happiest thing in this moment if you could just get to the point :PP.

Thanks.
Do understand that I'm still a newbie to programming. I'm trying my best to learn stuff.
Last edited on
try the following program,it works



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# include<iostream>
#include<cstdlib>

using namespace std;
int main()
{
int number;
do
{
cout << "Please enter number not greater than 100: ";
cin >> number;
}
while (number > 100);

cout << "Your number is " << number << ".";

system("PAUSE");
return 0;
}




enjoy programming :)
Thank you but...
I wanted to loop until the user has inputted a valid value.
Let's say:


Please enter a number not greater than 100: eeer
Please enter a valid value: e40
Please enter a valid value: 500
Please enter a valid value: 40
Your number is 40.



Something like that, it loops until a valid value is inputted. Thanks anyways.
Last edited on
The function isalpha() works only for characters. this actually compares the ascii values of the passed value. The ascii value of characters are 65-90(for capital letters) and 97-122(for small letters).for example u enter a value in an int number 89. this is a value of 'Y' therefore the function will turn to TRUE. this is what creating a problem in your program.
Owwwkay.
That still didn't get into my puny brain.

Thanks for explaining me what those functions are for. Just saw them off from somewhere and thought I might as well use it to distinguish characters from numerals. Guess not.

Thank you :)
Your code works well for numbers greater than 100 but, when, let's say, user inputs letters, it's an infinite loop.

Wonder what I can do to make my program operate similar to the sample output I posted above? Imma keep trying :PP

Thanks for replying to my queries :)
Topic archived. No new replies allowed.