# why won't this work

• 10-20-2002
papisuave
why won't this work
Code:

```# include <iostream> # include <cmath> using namespace std; int 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;         cout << "\nThe average of your four scores is "<<average<<".\n";         cout << "The stand deviation of your scores is "<<sd<<".\n";         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);         return 0; }```
&#91;code]&#91;/code]tagged by Salem
• 10-20-2002
BMJ
Here.... look at the difference, and think about it, I think you'll figure it out ;)
Code:

```# include <iostream> # include <cmath> using namespace std; int 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 stand deviation of your scores is "<<sd<<".\n";     return 0; }```
• 10-20-2002
SilentStrike
You are outputting the values before you are computing them for one. Second, you probably want to use doubles for the average and standard deviation (if not all the inputs as well) so they can have decimals.
• 10-20-2002
BMJ
lol, look at his code again Inquirer.... :) see the error?

Oh, and yea... good idea SS
• 10-20-2002
SilentStrike
You can use cin to get pretty much every basic type (perhaps not bools).
• 10-20-2002
Inquirer
BMJ:
so can you input integers thru cin? *is confused* i never thought you could.........

:confused:

~Inquirer
• 10-20-2002
BMJ
sure.. you can do this:
Code:

```int x; std::cin >> x;```
And if they enter an integer, it will be stored as x... if they do not enter an integer.... heh, well you'll have a problem. :)
• 10-20-2002
Inquirer
hehe. I didn't even look that far. But, i learned something today. thanks.

~Inquirer
• 10-20-2002
BMJ
You should use atoi() when getting integer input with cin
• 10-20-2002
papisuave
Thanks you guys
Thank you very much, I understand what i did wrong
• 10-20-2002
BMJ
I knew you could figure it out ;) :)
• 10-20-2002
papisuave
new problem
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;
}
• 10-20-2002
BMJ
What do you mean by loop? Do you want it to go through getting all the scores? Or do you want the use to be able to stop at any time by typing 'Q' or something?
• 10-20-2002
papisuave
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"
• 10-21-2002
ammar
Hi,
I would really like to know how did you input integers without using cin?