Jump to content

C++ Endless Loop


Recommended Posts

So here is a very stripped down version of my code, but it is throwing the same error. Basically (I think, the cin (buffer?)) is not getting reset, and some maybe hidden characters are being carried over to the next iteration of the loop.

 


int choice;
bool built=false;

while(!built){

    cout << "Enter your number: ";

	cin >> choice;
	cin.get();

	if(choice>0 && choice<7){

                         //stuff that may lead to built=true;		

			}else{

			cout << "\n\nYour number must be between 1 and 6!!!\n\n";

			}//end if

	}//end while

 

if you enter a number it checks just fine. if you enter a letter though, it just goes into an endless loop like this:

 

Enter your number:

 

5 //ok

 

Enter your number:

 

8 //errors but works

 

Enter your number:

 

k //letter

 

Enter your number: Your number must be between 1 and 6!!!

 

Enter your number: Your number must be between 1 and 6!!!

 

Enter your number: Your number must be between 1 and 6!!!

 

Enter your number: Your number must be between 1 and 6!!!

 

Enter your number: Your number must be between 1 and 6!!!

 

- -

- -

- -

 

and so on forever :(

 

can someone point me in the correct direction? thanks!

Link to comment
https://forums.phpfreaks.com/topic/174920-c-endless-loop/
Share on other sites

Since I'm not using the system(); commands, I use this at the end of my code to simulate the end button

 

cout << "Thanks for playing :)\n\nPress Enter to exit..."; // system("PAUSE"); workaround

cin.get();

 

return(0);

 

if I don't do the cin.get(), there are the extra 2 characters left in space.

 

 

there was a thread here

 

http://www.phpfreaks.com/forums/index.php/topic,268620.0.html

 

that I had the issue in and corbin helped me fix...

Link to comment
https://forums.phpfreaks.com/topic/174920-c-endless-loop/#findComment-921899
Share on other sites

Since I'm not using the system(); commands, I use this at the end of my code to simulate the end button

 

cout << "Thanks for playing :)\n\nPress Enter to exit..."; // system("PAUSE"); workaround

cin.get();

 

return(0);

 

if I don't do the cin.get(), there are the extra 2 characters left in space.

 

 

there was a thread here

 

http://www.phpfreaks.com/forums/index.php/topic,268620.0.html

 

that I had the issue in and corbin helped me fix...

 

Ah.  Back when I was learning C++ in the late 90's, we only ever used the >> operator for input.  If we wanted to terminate the loop, we'd feed it an end of line/file character, which was Ctrl-D on our linux machines.  So, typically our loops would look like:

 

while (cin >> someInput, cout << "=> ")
{
   //do stuff
}

 

Unfortunately, my C++ is weak, so I'm not sure what is causing your problem.

Link to comment
https://forums.phpfreaks.com/topic/174920-c-endless-loop/#findComment-921910
Share on other sites

  • 4 weeks later...

Hi seventheyejosh,

The problem with your code is that your condtion checks just for numeric values between 1-7 and not the other values. In C++ internal typecasting is done as per the type of variable you are taking into account. You have taken the choice variable as an int and this means that every value in this will be stored as an int. When you type a character as input, its relevant ascii code gets stored in thechoice variable and this is not the same value between 0 and 7. The cin.get() might not have any role to play at this stage as your condition does not check for the input recieved via this statement. It just checks the choice variable.

Hope this answers your query.

Cheers,

guddoosk

Link to comment
https://forums.phpfreaks.com/topic/174920-c-endless-loop/#findComment-939744
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.