7 Replies - 366 Views - Last Post: 10 May 2012 - 10:53 PM Rate Topic: -----

#1 animus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-May 12

Question: how to kill loop, soon as invalid test score

Posted 10 May 2012 - 04:54 PM

My question is how do I kill the loop as soon as the test score becomes outside its limits (lets say 1-3 are right but 4th time through isn't)?

          
//calculates test average and test score total.
#include <iostream>
using namespace std;
int main ()
{
	int limit;
	double score = 0;
	double total = 0;
	double entered = 0;
	double average;
	bool precede = true;
	char response;
	cout << "How many tests: " << endl;
	cin >> limit;
	for(int count = 0;count < limit; count++)
	{
		
		score = score;
		cout << "Enter score   #" << count +1 << endl;
		cin >> entered;
		if (entered <= 0 || entered >= 100)
		{
			cout << "Invalid test score" << endl;
			precede = false;
		}
		
		{
		score = score + entered;
		}
	}
	
	
	total = total + score;
	cout << "Total " << total << endl;
	average = total/limit;
	cout << "Average " << average << endl;
	return 0;
}







Is This A Good Question/Topic? 0
  • +

Replies To: Question: how to kill loop, soon as invalid test score

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Question: how to kill loop, soon as invalid test score

Posted 10 May 2012 - 05:00 PM

You can always use the break keyword to well, break out of loops or continue to reset a loop at its next iteration.
Was This Post Helpful? 1
  • +
  • -

#3 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,472
  • Joined: 20-March 10

Re: Question: how to kill loop, soon as invalid test score

Posted 10 May 2012 - 05:02 PM

Hi,

Use a
break;
Statement

http://msdn.microsof...w(v=vs.80).aspx

Best Wishes

Snoopy.

Ahh ninja'd by DogStopper !
I knew I shouldn't have looked for that link

This post has been edited by snoopy11: 10 May 2012 - 05:03 PM

Was This Post Helpful? 0
  • +
  • -

#4 animus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-May 12

Re: Question: how to kill loop, soon as invalid test score

Posted 10 May 2012 - 05:27 PM

View PostDogstopper, on 10 May 2012 - 05:00 PM, said:

You can always use the break keyword to well, break out of loops or continue to reset a loop at its next iteration.

Is there another way to do it without break/continue, such as using a while, or an if, if else?
Was This Post Helpful? 0
  • +
  • -

#5 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,472
  • Joined: 20-March 10

Re: Question: how to kill loop, soon as invalid test score

Posted 10 May 2012 - 05:34 PM

Why what's wrong with break,

You could just wrap everything
In a while loop

while (precede)

{
// do all your for loop stuff here.
}
Was This Post Helpful? 0
  • +
  • -

#6 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 644
  • Joined: 03-April 12

Re: Question: how to kill loop, soon as invalid test score

Posted 10 May 2012 - 10:29 PM

You can have a while-loop with a flag indicating whether to loop still or stop looping, but that's considered a bad way. Break is better.
Was This Post Helpful? 0
  • +
  • -

#7 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,472
  • Joined: 20-March 10

Re: Question: how to kill loop, soon as invalid test score

Posted 10 May 2012 - 10:35 PM

View Postturboscrew, on 11 May 2012 - 05:29 AM, said:

You can have a while-loop with a flag indicating whether to loop still or stop looping, but that's considered a bad way. Break is better.

Yes I agree
But apparently he doesn't want to use break;

I have no idea why ....

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#8 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 644
  • Joined: 03-April 12

Re: Question: how to kill loop, soon as invalid test score

Posted 10 May 2012 - 10:53 PM

Also:
for(int count = 0;(count < limit) || (score < limit); count++)
can be used, but it's a bit ugly too as so is

for(int count = 0, flag = true;(count < limit) && flag; flag = (score <= high_limit) && (score >= low_limit), count++)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1