-
isalpha in for loop
would one of you guru types take a look at this at tell me why it either falls through without printing "you must enter a number" or goes into an infinite loop when an alapha caracter is entered.
cout << "\nEnter the upper range: ";
cin >> high_limit;
if(high_limit < 0 )
{
cout << "Number must be positive and bigger than 1!!";
cin >> high_limit;
}
if(isalpha(high_limit)==0)
{
cout << "You must enter a number!\n";
break;
}_________________________________________________ ___
-
Actually I could use some help with this too using C++ iostream methods. I can easily do this code in C but that is another story.
-
I'm assuming it starts like this:
Code:
while(cin >> num)
{
...
}
-
sorry about the tiny code pasted to the original.
this is the loop
while(1)
{
cout << "\nEnter the upper range: ";
cin >> high_limit;
if(high_limit < 0 )
{
cout << "Number must be positive and bigger than 1!!";
cin >> high_limit;
}
if(isalpha(high_limit)==0)
{
cout << "You must enter a number!\n";
return 0;
}
else;
-
What is high_limit definition?
-
isalpha(), isdigit(), isalphanum() all take a single char as their argument. If the char is appropriate (the name of the function indicates what the function looks for) then each function returns a non-zero value. If the char isn't appropriate then the function returns zero.
The code as posted appears to be an attempt at input validation. There are two common approaches to this. You can use the built in istream methods of good(), fail(), and clear() or you can use strings to store all input and then analyze the string for appropriate char input using a loop and calling isalpha(), isdigit(), or isalphanum() in whatever combination you want to confrim appropriate characters are present (or missing). If you want input to eventually be stored in an int then only digits can be present in the string. If that is true then you can use atoi() or atol() to convert the string to type int or type long, depending on the length of the string.
-
thanks guest,
high_limit is declared as a global variable int high_limit; prior to entering int main(......)
am i to understand that isalpha and the like can not take a variable name and must look only at a single input or return value?
-
If your 'high_limit' variable is indeed an 'int' then it cannot store a string. If the user enters letters in instead of a number, the statement will work but zero (0) will be put in 'high_limit'. If the user enters something that isn't a number, 'cin.fail()' will return true. So you could change your code as follows.
while(1)
{
cout << "\nEnter the upper range: ";
cin >> high_limit;
if(high_limit < 0 )
{
// changed this to 'bigger than 0' as that is what the condition
// implies
cout << "Number must be positive and bigger than 0!!";
cin >> high_limit;
}
if(cin.fail())
{
cout << "You must enter a number!\n";
return 0;
}
Hope this helps.
-
isalpha(something)
if something is an int
it turns into an ascii character
meaning the integer 0 = 32
so, when saying isalpha(something)
it will just return true or false
in other words
if(isalpha(something)==0) change to
if(isalpha(something)){
cout<<"you must enter a number"<<endl;
}
-
Thanks all. your help has proven very helpful and solved the problem.