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);