Thread: isdigit ()

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    71

    isdigit ()

    heya guys
    i am having a little problem with isdigit in C++ even though i have used it in the past in C

    the situation is very simple.
    someone types in values in an integer array.
    i use a loop and go through each element and check if its a digit. even though they are all integers it sitll returns false

    heres the code

    Code:
    int checkAlpha (const student x)
    {
    	for (int i = 0; i < NumLabGrades; i++)
    	{
    		if (x.lab_grade[i] < 0 || x.lab_grade[i] > 25) return 0;
    		if (!(isdigit (x.lab_grade[i]))) return 0;
    	}
    	return 1;
    }
    thanks

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> if (x.lab_grade[i] < 0 || x.lab_grade[i] > 25) return 0;

    What is this? Are we comparing chars or ints here?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Look, isdigit() is for chars, not ints. decimal 48 == '0'. decimal 57 == '9', get it? Better post some more code.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    >someone types in values in an integer array.
    i use a loop and go through each element and check if its a digit.<

    Then you're duplicating effort already built into the C++ stream classes. Input the digits and test the stream state.
    Joe

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    71
    Originally posted by Sebastiani
    Look, isdigit() is for chars, not ints. decimal 48 == '0'. decimal 57 == '9', get it? Better post some more code.
    here is the small lab

    www.mdofit.com/labs/27/lab2.cpp



    i realized it is for character testing but then how to make sure that ppl dont input characters when i ask for ints..
    and with the do while loop i have, (in my other function named getStudentData ) if i enter a character when an int is required it just goes on cout'ing forever..

    the program works and all
    i just want to make it full proof

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    71
    Originally posted by Sebastiani
    >> if (x.lab_grade[i] < 0 || x.lab_grade[i] > 25) return 0;

    What is this? Are we comparing chars or ints here?
    sorry.. that was just another check i had to make so i added that on into my code

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    IMHO, you should validate the data BEFORE it even reaches class data. A good input procedure will solve that. For instance:



    Code:
    bool GetInt(int& value, ifstream& in = cin){
     char buff[256];
     in.getline(buff, 256);
     char * p = buff;
     value = 0;
         while(*p && *p != '\n')
      {
            if( !isdigit(*(p++) )  )
         {
            return false;
         }
      }
     value = atoi(buff);
     return true;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    71
    i have just been in my first ever C++ class for 2 weeks now so we havent yet gotten to
    in.getline (though i can see what it does)
    i understand what the method getline would do
    but i dont understand the functions parameters

    that is
    ifstream& in = cin

    the rest i understand

    if u could just clarify this
    thanks

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Those are just default parameters meaning that, if no file stream is supplied, cin is assumed. "in" is just an alias for whatever that stream may be called, it could just as well be called
    ifstream& mackol, and of course that could be a file stream too, but to be fair, that function is not optimal for file input, and would probably have to be modified before doing so...

    A simplified example of it's use might be:


    Code:
    int array[5];
    int i = 0;
    
    cout << "Please enter 5 values." <<endl;
    
          while(i < 5)
       {
         cout << "Enter a valid integer" <<endl;
              
              if( !GetInt(array[i]) )
           {
              cout << "Invalid input." <<endl;
              continue;
           }
         
        ++i;
       }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. isdigit woes
    By kryonik in forum C Programming
    Replies: 3
    Last Post: 10-14-2005, 05:10 PM
  2. isdigit()
    By kermit in forum C Programming
    Replies: 3
    Last Post: 03-19-2004, 09:59 PM
  3. I read the FAQ on isdigit() but...
    By CaptainJack in forum C Programming
    Replies: 5
    Last Post: 03-16-2004, 06:03 AM
  4. #include cctype and the isdigit
    By nextus in forum C++ Programming
    Replies: 2
    Last Post: 12-26-2002, 07:55 PM
  5. Correct use of isdigit
    By DocDroopy in forum C Programming
    Replies: 3
    Last Post: 08-05-2002, 07:22 AM