Jump to content
  • Advertisement
Sign in to follow this  
jordi_0071

C++ while in a if else

This topic is 4821 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey i made this code:
#include <iostream>
using namespace std;

int main()
{
	int CountGames;

	cout << "How many games do you have: ";

	cin  >> CountGames;

	if(CountGames > 30 || CountGames == 30)
	{
		cout << "You got a lot games. you are a game freak\n\n\n";
	}
	else if(CountGames < 30 && CountGames > 0)
	{
		cout << CountGames << " Isn't much but atleast you got some\n\n\n";
	}

	else if(CountGames == 0)
	{
		cout << "You got no games. wtf you should get on.\n\n\n";
	}

	else if(CountGames < 0) 
	{ 
		while(CountGames <0)
		{

		cout << "you cannot have less then 0 games. duh\n";
		
		cout << "How many games do you have: ";

		cin  >> CountGames;
		}
	}

	system("pause");

	return 0;
}


but if you first enter -1 and then if you press the second time higher then 0 it pauses the program. how do i solve this?

Share this post


Link to post
Share on other sites
Advertisement
try putting "cin.ignore();" in the while-loop before "cin >> CountGames;".
Also, why write "if(CountGames > 30 || CountGames == 30)"
if you can write "if(Countgame >= 30)"

Share this post


Link to post
Share on other sites
#include <iostream>
using namespace std;

int main()
{
int CountGames = -1;

do
{
cout << "How many games do you have: ";
cin >> CountGames;

if(CountGames<0)
cout << "you cannot have less then 0 games. duh\n";

} while(CountGames<0);


if(CountGames > 30 || CountGames == 30)
{
cout << "You got a lot games. you are a game freak\n\n\n";
}
else if(CountGames < 30 && CountGames > 0)
{
cout << CountGames << " Isn't much but atleast you got some\n\n\n";
}
else if(CountGames == 0)
{
cout << "You got no games. wtf you should get on.\n\n\n";
}

system("pause");
return 0;
}

Share this post


Link to post
Share on other sites
If i was u i would validate your input as soon as it was entered e.g.

#include <iostream>
using namespace std;

int main()
{
int CountGames;

cout << "How many games do you have: ";

cin >> CountGames;
while(CountGames <0)
{

cout << "you cannot have less then 0 games. duh\n";

cout << "How many games do you have: ";

cin >> CountGames;
}

if(CountGames > 30 || CountGames == 30)
{
cout << "You got a lot games. you are a game freak\n\n\n";
}
else if(CountGames < 30 && CountGames > 0)
{
cout << CountGames << " Isn't much but atleast you got some\n\n\n";
}

else if(CountGames == 0)
{
cout << "You got no games. wtf you should get on.\n\n\n";
}

system("pause");

return 0;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!