-
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;
}
[code][/code]tagged by Salem
-
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;
}
-
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.
-
lol, look at his code again Inquirer.... :) see the error?
Oh, and yea... good idea SS
-
You can use cin to get pretty much every basic type (perhaps not bools).
-
BMJ:
so can you input integers thru cin? *is confused* i never thought you could.........
:confused:
~Inquirer
-
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. :)
-
hehe. I didn't even look that far. But, i learned something today. thanks.
~Inquirer
-
You should use atoi() when getting integer input with cin
-
Thanks you guys
Thank you very much, I understand what i did wrong
-
I knew you could figure it out ;) :)
-
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;
}
-
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?
-
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"
-
Hi,
I would really like to know how did you input integers without using cin?