3 Replies - 302 Views - Last Post: 05 October 2012 - 04:23 AM Rate Topic: -----

#1 jaynajay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 17-September 12

How to cancel the loop after few attempts of answers

Posted 04 October 2012 - 02:40 PM

This is my code below for the table of 2. How can I break the loop so that the user has no more than five tries to guess the correct answer or number.
Thanks!


int main (){

	int i = 1;
	int ans;
	while(i<=10)
	{
		cout<<"What is 2 times " <<i <<endl;
		cin>>ans;

		
		if(ans !=i*2)
		
			cout<<"Try again" <<endl;
		
		else i++;
		
		
	}

	
return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: How to cancel the loop after few attempts of answers

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,473
  • Joined: 23-August 08

Re: How to cancel the loop after few attempts of answers

Posted 04 October 2012 - 02:43 PM

Hints:

Another int variable
An if statement
A break statement
Was This Post Helpful? 0
  • +
  • -

#3 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 770
  • Joined: 27-June 09

Re: How to cancel the loop after few attempts of answers

Posted 04 October 2012 - 02:51 PM

See comments

int main (){

	int i = 1; //may need to make another counter variable.
	int ans;
	while(i<=10)
	{
		cout<<"What is 2 times " <<i <<endl;
		cin>>ans;

		/*******Change this if statement to a while loop*********/
		//initialize any new variables
		if(ans !=i*2) //while(ans!=i*2&&SomeOtherCondition){
		
			cout<<"Try again" <<endl;
			/*add the following lines*/
			//cout<<"What is 2 times " <<i <<endl;
			//cin>>ans;
			//increment any new variables
		//}
		
		else i++; //Will no longer need else.  just i++;
		/********************************************************/
		
		
	}

	
return 0;
}



This post has been edited by mojo666: 04 October 2012 - 02:51 PM

Was This Post Helpful? 0
  • +
  • -

#4 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: How to cancel the loop after few attempts of answers

Posted 05 October 2012 - 04:23 AM

Also, you have to reset the tries counter if the user corrects the guess. So the user gets another five tries for the next guess...
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1