# Thread: how do i loop this?

1. ## how do i loop this?

How do i get this code to loop infinitely until the user tells it to stop?

# include <iostream>
# include <cmath>
using namespace std;

double average, sd, s1, s2, s3, s4, deviation;

int main()
{
cout << "Press enter after entering a value.\n";
cout << endl;
cout << "Enter score one: ";
cin >> s1;
cout << "\nEnter score two: ";
cin >> s2;
cout << "\nEnter score three: ";
cin >> s3;
cout << "\nEnter score four: ";
cin >> s4;

average =(s1+s2+s3+s4)/4;
deviation =(((s1-average)*(s1-average))+((s2-average)*(s2-average))+((s3-average)*(s3-average))+((s4-average)*(s4-average)))/3;
sd = sqrt(deviation);

cout << "\nThe average of your four scores is "<<average<<".\n";
cout << "The standard deviation of your scores is "<<sd<<".\n";

return 0;
}

what i mean is that i would like the user to be able to repeat the program. i would like the user to be able to input different scores each time. This would happen over and over again until the user wanted to stop. For instance,

average
mean

"would you like to run the program again? y or n"

then they would hit "y" as long as they wanted to use the program. when they want to quit, they would hit "n"

2. Hi,

You would need to put it in a while loop
Code:
```# include <iostream>
# include <cmath>
#include <conio.h> //for use of getch()

using namespace std;

double average, sd, s1, s2, s3, s4, deviation;

char yesno = 'y'; //while loop control variable

int main()
{
while (yesno!='n'&&yesno!='N') //will execute as long as yesno is not equal to 'n' or 'N'
{
cout << "Press enter after entering a value.\n";
cout << endl;
cout << "Enter score one: ";
cin >> s1;
cout << "\nEnter score two: ";
cin >> s2;
cout << "\nEnter score three: ";
cin >> s3;
cout << "\nEnter score four: ";
cin >> s4;

average =(s1+s2+s3+s4)/4;
deviation =(((s1-average)*(s1-average))+((s2-average)*(s2-    average))+((s3-average)*(s3-average))+((s4-average)*(s4-    average)))/3;
sd = sqrt(deviation);

cout << "\nThe average of your four scores is "<<average<<".\n";
cout << "The standard deviation of your scores is "<<sd<<".\n";

cout<<"Would you like to run the program again?";
cin>>yesno;
}

cout<<"Thankyou bye";
getch();
return 0;
}```
I hope this isn't too confusing, you seem to know what you are on about.

3. the compiler i am using is mvisual 6.0. Would that work with my compiler?

4. I,m reasonably sure it would but if you want you can get rid of the conio.h and getch(); bits so then I'm positive it would,

where I have put cout<<"Thankyou bye": and then getch();

this was just to signify the end of the loop, if the user enters 'y' or 'Y' for yes then it will loop back to the top because 'y' and 'Y' are not equal to 'n' or 'N' if the user enters 'n' or 'N' the that makes the condition in the while statement false so the loop will exit.

While loop only execute the code in the parantheses if its condition is true 'y' is not equal to 'n' or 'N' so that is true.

I hope this is a good enough explanation.

5. I've just tested it, it definitely does work, get rid of the

#include <conio.h> at the top and then get rid of the getch();
at the bottom because your compiler basically does that for you anyway/