cin>> types?

This is a discussion on cin>> types? within the C++ Programming forums, part of the General Programming Boards category; Is there a way to specify the type of input a user can enter... for example, I would like to ...

  1. #1
    Registered User remixx540's Avatar
    Join Date
    Nov 2005
    Location
    Mass, USA
    Posts
    4

    cin>> types?

    Is there a way to specify the type of input a user can enter...

    for example, I would like to have the user only allowed to enter a number from 0-9, and it wouldn't allow him to enter letters or symbols.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Not really, apart from inputing into a string, comparing each element to the values 0-9 then if they're all numbers, atoi() the string. It's quite a process though.
    Sent from my iPadŽ

  3. #3
    Registered User remixx540's Avatar
    Join Date
    Nov 2005
    Location
    Mass, USA
    Posts
    4
    Ok, well now each time a user enters a character other than what is accepted by int, it terminates the program. So how would I go about fixing this one? I can't change the variable type because it is part of a switch function.

  4. #4
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    You could simulate it. Do what SlyMaelstrom said, compare the char to a number, if its not 0-9 then clear the screen, replace what was there and make them enter again.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    I just told you what you can do, there is no point in rephrasing the question.

    When you do something like

    Code:
     cin >> i1; //Where i1 is an int
    The user input HAS to be an integer, otherwise the stream will fail and all cin statements will be ignored for the rest of the program. There is no way around this. You can do what I said, it's just about the only way.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    57
    k actually there are two ways i can think of that you can do that with.
    first one is simple

    Code:
    ...
    int number;
    
    do                                                                               //do loop
    {
           cout<<"Please enter a number between 0-9\n";//asks for input
           cin>>number;                                                  //puts it into that int
    }while ( number < 0 && number > 9 ); //repeats it untill 0 to 9 is                                          /                                                             //pressed 
    ...
    and the other one is more comlicated

    Code:
    include <conio>
    ..
    int menu
    menu=getch()
    switch (menu)
    {
                      case(27):stands for esc
                       {
                         cout<<"hello";
                       }
    }
    look up www.asciitable.com
    then have case for each one of the buttons

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Quote Originally Posted by kennny2004
    Code:
    ...
    int number;
    
    do                                                                               //do loop
    {
           cout<<"Please enter a number between 0-9\n";//asks for input
           cin>>number;                                                  //puts it into that int
    }while ( number < 0 && number > 9 ); //repeats it untill 0 to 9 is                                          /                                                             //pressed 
    ...
    This does nothing. You've already input something into an int variable. If it was a character, the stream has already failed. ...and now that the stream is failed, you'd be stuck in an infinite loop...

    Quote Originally Posted by kennny2004
    Code:
    include <conio>
    ..
    int menu
    menu=getch()
    switch (menu)
    {
                      case(27):stands for esc
                       {
                         cout<<"hello";
                       }
    }
    I don't see how this fixes his problem at all. All this does is say hello if the character pressed is the ascii value for escape. If you're insisting using this to check what character it is, you run into the problem of converting to an integer.

    I'm sorry that there is no simple way to do it, but there isn't. Trust me, every budding programmer has run into this issue and they all have to come to terms that there is no easy solution for it.
    Last edited by SlyMaelstrom; 11-06-2005 at 08:40 PM.
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote Originally Posted by SlyMaelstrom
    This does nothing. You've already input something into an int variable. If it was a character, the stream has already failed. ...and now that the stream is failed, you'd be stuck in an infinite loop...
    That is where ignore comes in.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    What are you gonna do? Ignore the input before you know what it is?
    Sent from my iPadŽ

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Ehh, using it along with clear, will get rid of the bad input, here is an example

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
       int input = -1;// initialize to invalid input
    
       cout << "Input a number between 0 and 9 inclusive" << endl;
       do
       {
          cin >> input;
          if (input < 0 || input > 9)
          {
             cin.clear();
             cin.ignore(256,'\n');
             cout << "Please try again, the number must be between 0 and 9" << endl;
          }
          else
          {
             break; // if its valid break out
          }
       } while(true);
    
       cout << "The value was " << input << endl;
    }
    Now, I'm both checking for invalid input, in the case that its out of range, and it will get rid of any bad data in the process. Now, if the bad data exceeds 256 characters, well the loop will keep going until it gets rid of all of it, but it will eventually.

    BTW, google is helpfull, it gave me this upon searching
    http://www.parashift.com/c++-faq-lit....html#faq-15.3
    Thats were I found this information originally, and its a handy little faq. You might notice, they do it a little bit better then I do, with the while loop and all, but it works
    Last edited by Xipher; 11-06-2005 at 10:27 PM.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Here is an example using what is called a "string stream":
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    int main()
    {
    	string input = "";
    	bool badInput = false;
    	
    	do
    	{
    		cout<<"Please enter only digits: ";
    		cin>>input;
    		badInput = false;
    		
    		for(int i = 0; i < input.length(); i++)
    		{
    			if(input[i] < '0' || input[i] > '9')
    			{
    				cout<<"Only digits 0-9!"<<endl;
    				badInput = true;
    				break;
    			}
    			
    		}
    
    	}while(badInput);
    
    
    	istringstream str(input);  //creates what's similar to an input file
    						//containing the string.
    	int num = 0;
    	str>>num; //read from the "input file" into the int variable
    
    	cout<<"You entered: " <<num<<endl;
    	
    	return 0;
    }
    Last edited by 7stud; 11-06-2005 at 10:34 PM.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    If the user types in something that does not fit the type your are expecting, the return value of >> will evaluate to false. This makes it easy to check for input that is invalid (like letters) as well as not what you want specifically (like greater than 9 or less than 0).
    Code:
    #include <iostream>
    #include <limits>
    
    int main()
    {
    	using namespace std;
    	int input = -1;
    	cout << "Enter a number between 0 and 9: ";
    	while (!(cin >> input) || input < 0 || input > 9)
    	{
    		cin.clear();
    		cin.ignore(numeric_limits<streamsize>::max(), '\n');
    		cout << "Try Again.\nEnter a number between 0 and 9: ";
    	}
    	cout << "Your number is: " << input << endl;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid types 'int[int]' for array subscript
    By kolistivra in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2010, 12:57 PM
  2. Replies: 6
    Last Post: 08-23-2008, 02:16 PM
  3. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 03:25 AM
  4. Types, Integral Types, Bytes?!?!?!
    By Kaidao in forum C++ Programming
    Replies: 3
    Last Post: 03-21-2006, 08:15 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21