Thread: Getlines and looping

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    1

    Getlines and looping

    I'm trying to get this function to work for class where I'm asking the user to input a number and I'm trying to validate it with this method here.

    The issue is the program runs and loops one full loop saying the input is wrong without letting actually enter anything then actually stops on the second go to ask at which point everything works fine.

    I'm trying to figure out why it's looping on initial function run when I start it and stops on the second go.



    Code:
    //These are my included libraries
    
    #include <iostream>		//Allows cin and cout
    #include <string>		//allows string
    #include <iomanip>		//Allows SetW and setfill
    #include <windows.h>	//Allows doe set_colour
    
    using namespace std;	//Shortcut for cin and cout
    
    
    
    //These are my variables and iMin and iMax are 2 and 20 respectively
       
            int iInput=0;
    	string sTemp;
    	bool bContinue=true;
    	bool bBadInput;			//decides on looping for correct input
    
    
    
    
    	do
    	{
    		cout<<"Please enter a numver between "<<iMin<<" & "<<iMax<<" To start building your house\n";
    		
    		::FlushConsoleInputBuffer(::GetStdHandle(STD_INPUT_HANDLE));	//Flush command
    	
    		//read input as string
    		getline(cin,sTemp,'\n');
    
    		//change string to an int
    		iInput=atoi(sTemp.c_str());
    		
    		//test to see if number was entered, or crap
    		if (iInput==0 && sTemp[0]!='0')
    		{
    			//crap was entered
    
    			//re-prompt
    			cout<<"Please only enter numeric values.\n";
    			
    			//loop again
    			bBadInput=true;	
    		}
    		else
    		{
    			//number was entered
    
    			//allow to leave loop
    			bBadInput=false;
    		}
    		//repeat if not a valid number	
    	}while(bBadInput);

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    i cant see what the problem is, well not the one you are saying anyhow, i compiled it after putting it in main and declaring the (undeclared) integers iMin to 2 and iMax to 20.... then it ran, i put in 15, it printed 15 then closed prog, i put in 21, it printed 21 then closed....i put in letters and it gave me the error ok and looped back as you wanted, it is not doing what you want but its not doing the error you reported either...

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It could be that ::FlushConsoleInputBuffer(::GetStdHandle(STD_INPUT _HANDLE)) is not enough. That flushes the buffer of the console, but cin has its own stream buffer that needs to be flushed with cin.sync().
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by King Mir
    but cin has its own stream buffer that needs to be flushed with cin.sync().
    Use cin.ignore instead: Prelude's concern about cin.sync.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    If you mean cin.ignore(cin.rdbuf().in_avail() ). That would work.

    cin.ignore(numeric_limits<streamsize>::max(), '\n'), on the other hand might not, depending on what's in the buffer.
    Last edited by King Mir; 12-01-2009 at 11:48 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You really shouldn't have to do any flushing of the console input inside that loop, not with sync or ignore or anything else. The only possible input you'd have to guard against would be redirection from a file or some other input where the user isn't actually typing (like perhaps a paste). For normal user input, simply calling getline will get everything they type, because they have to hit enter before the characters are sent to the cin buffer, and getline reads until the newline from hitting enter.

    The problem might be from a call to cin >> earlier in the code (from before the loop). In that case, calling ignore() immediately after that would probably be the best option, rather than as part of this loop.

Popular pages Recent additions subscribe to a feed

Tags for this Thread