9 Replies - 490 Views - Last Post: 15 June 2009 - 01:31 PM Rate Topic: -----

#1 carbon_coder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 31-December 07

My loops won't work (C++) [resolved]

Posted 15 June 2009 - 12:22 PM

For some reason i can't figure out why loops aren't working

#include <iostream>

using namespace std;

int main()
{
	int answer;

	bool lifeloop = true;
	bool quizloop = false;

	while(lifeloop = true)
	{
		while(quizloop = true)
		{
			printf("Quiz Running");
			cin.get();
		}
		
		if(answer = 1)
		{
			quizloop = true;
		} else if(answer = 2)
		{
			lifeloop = false;
		}
	}

	printf("Game over");
	cin.get();
}


This post has been edited by carbon_coder: 15 June 2009 - 01:32 PM


Is This A Good Question/Topic? 0
  • +

Replies To: My loops won't work (C++) [resolved]

#2 jcmaster2  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 183
  • Joined: 27-April 09

Re: My loops won't work (C++) [resolved]

Posted 15 June 2009 - 12:25 PM

Well it always is true once you start your loop...

That's why
Was This Post Helpful? 0
  • +
  • -

#3 carbon_coder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 31-December 07

Re: My loops won't work (C++) [resolved]

Posted 15 June 2009 - 12:29 PM

How can I fix that. like how should I change the scope where i define this
Was This Post Helpful? 0
  • +
  • -

#4 Zerobu  Icon User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Re: My loops won't work (C++) [resolved]

Posted 15 June 2009 - 12:37 PM

It should be,

while(lifeloop == true)



You did not use '==' you used '=' which would cause an infinite loop.
You must always use '==' inside of a loop when you do a test statement.
Do not use '='.

Also
if( answer == 1)



The same problem is in the else. Get the point?

This post has been edited by Zerobu: 15 June 2009 - 12:40 PM

Was This Post Helpful? 0
  • +
  • -

#5 carbon_coder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 31-December 07

Re: My loops won't work (C++) [resolved]

Posted 15 June 2009 - 12:41 PM

View PostZerobu, on 15 Jun, 2009 - 11:37 AM, said:

It should be,

while(lifeloop == true)



You did not use '==' you used '=' which would cause an infinite loop.
You must always use '==' inside of a loop when you do a test statement.
Do not use '='.

Also
if( answer == 1)



The same problem is in the else. Get the point?


I tried this. i didn't change anything
Was This Post Helpful? 0
  • +
  • -

#6 Zerobu  Icon User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Re: My loops won't work (C++) [resolved]

Posted 15 June 2009 - 12:43 PM

It should work. Post your updated code.
Was This Post Helpful? 0
  • +
  • -

#7 carbon_coder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 31-December 07

Re: My loops won't work (C++) [resolved]

Posted 15 June 2009 - 12:50 PM

Oh wait, the ow is working but now it's printing quiz running twice
#include <iostream>

using namespace std;

int main()
{
	int answer;

	bool lifeloop = true;
	bool quizloop = false;

	while(lifeloop == true)
	{

		while(quizloop == true)
		{
			
			printf("Quiz Running");
			cin.get();
		}
		cout << "do it do it";
		cin >> answer;
		
		if(answer == 1)
		{
			quizloop = true;
		} else if(answer == 2)
		{
			lifeloop = false;
		}
	}

	printf("Game over");
	cin.get();
}


Was This Post Helpful? 0
  • +
  • -

#8 Zerobu  Icon User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Re: My loops won't work (C++) [resolved]

Posted 15 June 2009 - 12:56 PM

while(quizloop == true)
		{
			
			printf("Quiz Running");
			cin.get();
		}



I think there is a problem here. Notice that in this loop quizloop is always true.
Was This Post Helpful? 0
  • +
  • -

#9 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: My loops won't work (C++) [resolved]

Posted 15 June 2009 - 01:12 PM

View Postcarbon_coder, on 15 Jun, 2009 - 11:50 AM, said:

Oh wait, the ow is working but now it's printing quiz running twice
#include <iostream>

using namespace std;

int main()
{
	int answer;

	bool lifeloop = true;
	bool quizloop = false;

	while(lifeloop == true)
	{

		while(quizloop == true)
		{
			
			printf("Quiz Running");
			cin.get();
		}
		cout << "do it do it";
		cin >> answer;
		
		if(answer == 1)
		{
			quizloop = true;
		} else if(answer == 2)
		{
			lifeloop = false;
		}
	}

	printf("Game over");
	cin.get();
}


thats because of your cin.get(),
replace that with this,
#include <limits> // dont forget
while(quizloop == true)
{
				printf("Quiz Running");
				// This is more by far better than using system pause
				cout<<endl<<"Press Enter to Quit...";
				cin.sync();
				cin.ignore(numeric_limits<streamsize> ::max(), '\n');
				return 0;
}


Was This Post Helpful? 0
  • +
  • -

#10 carbon_coder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 31-December 07

Re: My loops won't work (C++) [resolved]

Posted 15 June 2009 - 01:31 PM

oh thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1