Thread: Variable Type Problems

  1. #1
    Registered User Gnoober's Avatar
    Join Date
    Oct 2002
    Posts
    40

    Variable Type Problems

    I know that this question has been answered before but I have searched all over and not found what I'm looking for. I'm writing a console application. We all know how much the computer hates it when you input a character when you should have inputted some type of number. I would like to know how to avoid this... Example

    Code:
    ...
    int I;
    
    cin>>I;
    
    if (A character was put in){
        start over
    }
    else {
        Its all good
    }
    I'm sure someone else has asked this before but I searched for a long time and didn't find what I was looking for... Thanks for your help!
    - Grady (:

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    There is a funciton called isalpha() that may be what you are looking for.

    Code:
    do
    {
    c=getch();
    }while( ! isalpha(c) )
    Best Regards,

    Bonkey

  3. #3
    Registered User Gnoober's Avatar
    Join Date
    Oct 2002
    Posts
    40
    I still don't understand...
    In your example, of what variable type is "c".
    And, I'm not sure if this matters, but I'm using "cin" not getch();
    - Grady (:

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, fgets() has always worked for me

    Why not try this:

    Code:
    int iget(int max_buff = 100) {
     char buff[max_buff ];
     return atoi(fgets(buff, max_buff , stdin));
    }
    
    
    
    int main() {
    
    int num;
    
    do{
    
      num = iget();
    
      if(num != 0) //...user pressed enter, but inputted nothing...
      break;
    
    }while(1);
    
    return 0;
    }
    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;
    }

  5. #5
    Registered User Gnoober's Avatar
    Join Date
    Oct 2002
    Posts
    40
    So what you are saying is that there is no possible way to avoid this problem using std::cin? I don't get the other examples, I just finished learning the basics of the language.
    - Grady (:

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Of course you can over-ride cin, but I haven't tried that yet...but look:



    Code:
    
    
    
    
    class my_cin {
    public:
    
    char buffer[10000];
    
    int operator >> (int num) {
      return num = atoi(fgets(buffer, 10000, stdin));  
     }
    float operator >> (float num) {
      return num = atof(fgets(buffer, 10000, stdin));  
     }
    long operator >> (long num) {
      return num = atol(fgets(buffer, 10000, stdin));  
     }
    double operator >> (double num) {
      return num = atod(fgets(buffer, 10000, stdin));  
     }
    char operator >> (char c) {
     return   c = getch();
     }
    char * operator >> (char str[]) {
     return fgets(str, 10000, stdin);  
     }
    char * get( char str[], int max ) {
     return fgets(str, max, stdin);
    }
    };
    
    Now declare a global input object (like cin)...
    
    
    my_cin inn;
    
    
    
    int main() {
    
    char c;
    int i;
    double d;
    char string[100];
    
    inn >> string;
    
    inn >> d;
    
    //...or even:
    
    char ch = inn >> c; //...get c, and also assign value to ch.
    
    return 0;
    }
    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;
    }

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    look into the istream methods called good(), bad(), fail(), and clear() and then you could try something like this;
    Code:
    int num;
    int flag = 0;
    
    while(flag == 0)
    {
      cout << "enter a num" << endl;
      cin >> num;
      if(!cin.good())
      {
        cin.clear();
         cout << "you did not enter an integer, try again. " << endl;
      }
      else
        flag = 1;
    }
    It should prevent entering a char when requesting an int but it may not detect entering a double instead of an int (it may just truncate the double at the decimal point, leaving the decimal point and fraction in the input buffer). It also won't prevent the user from entering 12345567789 which may also screw things up. But it is one of the things you can try.

  8. #8
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Sebastiani

    Why not try this:
    Code:
    int iget(int max_buff = 100) {
     char buff[max_buff ];
     return atoi(fgets(buff, max_buff , stdin));
    }
    Because it won't work.
    The size of arrays have to be constant

    Use elad's method with a little modification: Add ignore(10000) after cin.clear().

    But if you're looking for a complete function, try this:
    It won't decrease functionality of cin like declaring a new class will.
    (custom classes might override istream::operator>>)

    Code:
    //nice, safe reading function
    template<typename T> bool get(T& t)
    {
        using std::cin;
        cin>>t;
        if (cin.eof())
            cin.clear();
        else if (cin.fail())
        {
            cin.clear(); 
            cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');    
        }
        else if (cin.bad())
            cin.clear();
        else
        {
            cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); 
            return true;
        }
        return false;
    }
    Last edited by Sang-drax; 10-14-2002 at 04:14 PM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Very good point Sang-Drax. Only poeple like me who hate the STL should be writing their own input classes

    But very good code example...
    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. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. "Deciding" in runtime the type of a variable
    By mikahell in forum C++ Programming
    Replies: 28
    Last Post: 07-22-2006, 09:51 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 12:51 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM