Thread: isalpha in for loop

  1. #1
    In The Light
    Join Date
    Oct 2001
    Posts
    598

    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;
    }_________________________________________________ ___

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    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.

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    I'm assuming it starts like this:

    Code:
    while(cin >> num)
    {
       ...
    }

  4. #4
    In The Light
    Join Date
    Oct 2001
    Posts
    598
    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;

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    What is high_limit definition?

  6. #6
    Unregistered
    Guest
    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.

  7. #7
    In The Light
    Join Date
    Oct 2001
    Posts
    598
    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?

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    7
    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.

  9. #9
    Unregistered
    Guest
    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;
    }

  10. #10
    In The Light
    Join Date
    Oct 2001
    Posts
    598

    Thumbs up

    Thanks all. your help has proven very helpful and solved the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Palindrome, problem with isalpha
    By Kyeong in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 01:28 PM
  2. Problem with isalpha
    By kron_19792000 in forum C Programming
    Replies: 3
    Last Post: 09-26-2005, 12:13 AM
  3. isalpha || ispunct
    By linuxdude in forum C Programming
    Replies: 3
    Last Post: 05-21-2004, 09:43 PM
  4. isalpha
    By mackol in forum C Programming
    Replies: 9
    Last Post: 11-29-2002, 02:55 PM
  5. Floating Point isalpha();
    By UnclePunker in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 08:00 PM