Thread: Using cin correctly

  1. #1
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59

    Using cin correctly

    I'm writing a program that takes an integer input via 'cin'.
    I want to make the program to make sure the number is not a character or string (which would cause the program to crash)


    So far.. this is the code I have..

    Code:
    	int getUserTempInput()
    	{
    	int x = 0;
    	while (x != 1)
    		    {
    	cout << "\nEnter a temperature to convert: \n";
    	cin >> userTemp;
    
    		if (cin.good() == true)
    		{
    		x = 1;
    		  }else{
    		    cout << userTemp << " is not a valid integer. Please try again.\n\n" << endl;
    		}
    		    } //end while loop
    		return userTemp;
    	} //end getUserInput

    When i execute the program, and try to enter something like:

    ssdfsdf sdfsdfs fsw

    the program goes into an infinate loop. I looked on google and read something about using 'boolean cin.good()' and tried it in the code, but it doesn't seem to help.
    What function could I use to insure that the correct input is taken in?

    Thanks.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    In general, read user input as a string, then later convert. If this option is not available to you, look in the FAQ.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    One idea is to use cin.clear to clear the flags and cin.ignore to clear the input buffer.
    Code:
    #include <iostream> 
    #include <limits> 
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main()
    {
      int input;
      
      while (cout << "Enter a number: " && !(cin >> input))
      {
        cout << "\nInvalid input" <<endl;
        cin.clear();
        cin.ignore(std::numeric_limits < int >::max(), '\n');
      }
      
      cout <<"You entered " <<input;
    }
    This code is copied from the (FAQ) entry found here:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    you can check for a char (not string) using isascii (int c)

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >you can check for a char (not string) using isascii (int c)
    The input stream will already be in a failure state at this point, so your solution won't work unless the type of the input variable is some form of char or string. It seems that the problem is (a common one) where people expect cin to somehow not fail when trying to read the wrong type of value.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin problem
    By mikahell in forum C++ Programming
    Replies: 12
    Last Post: 08-22-2006, 11:14 AM
  2. problem with cin
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2006, 12:50 PM
  3. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 12:42 PM
  4. cin not allowing input after first use of function
    By Peter5897 in forum C++ Programming
    Replies: 5
    Last Post: 01-31-2006, 06:29 PM
  5. Overriding Cin with Cout
    By Tainted in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2005, 02:57 PM