Thread: Checking data types and errors

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    Checking data types and errors

    how do i check a variable to make sure it is the correct data type?

    i'm sure this question is answered somewhere on this board but i couldn't find it...

    double number1;
    cin >> number1;


    if i enter a number the program runs fine.. but if by accident i type in 34D23KJ or something the program tries to put this into number1 then realizes it is not a valid double and crashes...

    is there a function i can run to return true or false?!?!?

    like number1.fail() or something... i'm a bit lost

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    #include <iostream>
    using namespace std;
    
    int main() 
    {  
      double d;
      cin>>d;
      if ( !cin.fail() )
        cout<<"Valid data type"<<endl;
      else
        cout<<"Invalid data type"<<endl;
      return 0;
    }
    or
    Code:
    #include <iostream>
    using namespace std;
    
    int main() 
    {  
      double d;
      if ( cin>>d )
        cout<<"Valid data type"<<endl;
      else
        cout<<"Invalid data type"<<endl;
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    thanx

    thanx man.. exactly what i needed gracias

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    20
    Sorry to butt in, but I had a quick (meaningless?) question: what's the difference between
    Code:
     !cin
    and
    Code:
    cin.fail()
    ?
    Self Learner--patience required

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Technically, if you check the failbit of cin with fail() then you can call clear() to reset it and be able to read from cin again. cin is a bit finicky in this area though, which is why I prefer getline() and parsing

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Well, I wrote this little program:

    Code:
    int main()
    {
    	int a = 0;
    	cin >> a;
    	if (!cin)
    		cout << "NOT CIN\n";
    	if (cin.fail())
    		cout << "CIN FAILED\n";
    	return(0);
    }
    And both NOT CIN and CIN FAILED were outputted if I inputted a character instead of an integer. So... I'm not sure if there is one.

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    20
    Prelude, I'm sorry I just didn't understand you. Can you dumb it down a bit?
    Self Learner--patience required

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Both of the choices you offered do the same thing. But the difference is in how you can recover from an error. http://www.cplusplus.com/ref/iostream/ios/clear.html

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  4. using various data types in c language
    By sanju in forum C Programming
    Replies: 1
    Last Post: 11-28-2002, 10:24 AM
  5. data types
    By wazilian in forum C Programming
    Replies: 1
    Last Post: 12-07-2001, 01:52 PM