Thread: Console application - input handling

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    7

    Question Console application - input handling

    Hello!

    Yes, I'm having a little trouble with the streams. I will make myself clear before I get to my problem: If you think you need to see my code to help, then you probably can't help me!

    Okay!

    I need to know how to handle multiple forms of input. That being, say I need to work with integers, but the user sends in a character, and then instead of blowing up, it stops and tells the user it only accepts in integers, and then requests for input again.

    I need it to use integers, because the integer is also being sent to functions to be evaluated as integers. It is unreasonable for me to test each character, and then return their integer value, so I need to just throw out character input.

    I can't get it done! And, for my class project, I need it to be done. My teacher hasn't exactly been helpful.

    This actually has been an ongoing problem for me to solve over the last 10 months I've been working with c++.. Kind of annoying! Thanks for any help you can offer!

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    One of the things i've been wondering about since i've started c++
    after some googling, i found this

    Code:
    #include <iostream>
    
    int main()
    {
    	int i = 0;
    	if (!(std::cin>>i))
                std::cout<<"isn't an integer";
            else
                std::cout<<"is an integer";
    	return 0;
    }

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    7
    Thank you!

    I had a method down, and was working on it right now, although this one is just stupid in comparison to such a small snippet of code that solves it.


    I was going to have it input into strings, and test to see if it holds integer values, and then assemble the values into an integer. It would break down every value using substr methods (I thought the string class could return exact given positions, but I can't find that, so I built a function to make it easier to return exact positions), and then perform checks on the returned string, which would be a single character, adding to an integer variable as it goes along.

    Like, if the user sent 543, it would know it's only a 3 digit number to start with. After it performed checks on what character, it would basically add each position like so:

    Code:
    integer += 5 * 100;
    integer += 4 * 10;
    integer += 3;
    Although, that doesn't yet account for handling 0 input, which doesn't matter anyways, since I don't have to bother with it anymore!

    Thanks again!

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    7
    Okay, that didn't work for me unfortunately, after testing it. I got the same results I always do, endless loop.. Not sure why it breaks cin, but it does. I did manage a new one, not including my other idea. I used cin.peek(). It allows the user to put things into the stream, however, the command itself doesn't take from the stream, it leaves it IN the stream, and returns the value of what is in the stream as a character.

    Code:
    int getInput()
    {
    	char input;
    	int integer = 0;
    
    	//do while integer is 0
    	do {
    		//get input from the user
    		input = cin.peek();
    		
    		//if input is a digit, put it into an integer variable
    		if (isdigit(input)) { cin>> integer; }
    
    		//otherwise, empty the stream
    		else { cin.ignore(80, '\n'); }
    	} while (!(integer));
    
    	return integer;
    }

    I wish I had known cin.peek() took in input, I only thought it was just to see if anything was left in the stream! I'd have had this done long ago!

    Thanks again for the help.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    This is a common problem. The most common way to deal with it (or at least my favorite) is this:
    Code:
    int integer = 0;
    while (!(cin >> integer))
    {
        cin.clear(); // clear the fail bit
        cin.ignore(1000, '\n'); // ignore the characters
        // Give an error message and prompt the user for an integer again here.
    }
    This is a better general solution IMO because it allows 0 and negative numbers to be input. It also works for any datatype that can make cin fail.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding interface to Win32 console application
    By motiz in forum Windows Programming
    Replies: 5
    Last Post: 01-03-2008, 03:17 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. console application...
    By k1ll3r in forum C++ Programming
    Replies: 4
    Last Post: 05-23-2006, 10:41 AM
  4. Console Application
    By Mont_Blanc in forum C++ Programming
    Replies: 3
    Last Post: 04-17-2004, 03:07 AM
  5. splitting the screen in a Win32 console application
    By watcher_b in forum C Programming
    Replies: 1
    Last Post: 10-19-2002, 05:22 PM