Reputation: 33
so here is my code (stripped of the headers because that is irrerevelant.)
int main() {
float program = 0;
float scores = 0;
float test = 0;
float testScores = 0;
float e = 1;
float exam = 0;
float programAverage = 0;
cout << "Enter the number of assignments that were graded: ";
cin >> program;
for (int i = 1; i <= program; i++){
cout << "Enter the score for assignment # " << i <<": "; cin >> scores;
}
cout << "Enter the number of test: ";
cin >> test;
for (int e = 1; e <= test; e++){
cout << "Enter the score for test # " << e << ": "; cin >> testScores;
}
cout << "Enter the final exam score: ";
cin >> exam;
programAverage = (scores/program);
cout << "Program Average: " << programAverage << endl;
}
the last part I am having problems because whenever I compile my program the compiler just remembers the last number the user entered and does not average it. How can i get it to add all the user input numbers together and then average?
Upvotes: 1
Views: 3037
Reputation: 1144
float _sum=0;
for (int i = 1; i <= program; i++){
cout << "Enter the score for assignment # " << i <<": "; cin >> scores;
_sum+=i;
}
programAverage = (_sum/program);
cout << "Program Average: " << programAverage << endl;
Upvotes: 1
Reputation: 36
int main() {
float program = 0;
float scores = 0;
float test = 0;
float testScores = 0;
float e = 1;
float exam = 0;
float programAverage = 0;
float scoresSum = 0; // variable that adds up all the input scores
cout << "Enter the number of assignments that were graded: ";
cin >> program;
for (int i = 1; i <= program; i++){
cout << "Enter the score for assignment # " << i <<": "; cin >> scores;
scoresSum += scores; // adds up all the scores
}
cout << "Enter the number of test: ";
cin >> test;
for (int e = 1; e <= test; e++){
cout << "Enter the score for test # " << e << ": "; cin >> testScores;
}
cout << "Enter the final exam score: ";
cin >> exam;
programAverage = (scoresSum/program); // divide the total score out of program number
cout << "Program Average: " << programAverage << endl;
}
So the problem was that you didn't add up the input scores. The variable "scores" only has the value of the last input score. You have to set up a variable to sum up all the input score so far, such as scoresSum in the code. And add up the score every time a score is submitted.
You can easily find the difference between your code and mine by looking at the line with comment.
Upvotes: 1
Reputation: 65619
Well yeah because of this loop, scores
always has the last value entered:
for (int i = 1; i <= program; i++){
cout << "Enter the score for assignment # " << i <<": "; cin >> scores;
}
An average is defined as a sum divided by the number of instances. You're not summing, you just keep overwriting "scores" with the last read value when you do cin >> scores
. So the problem can be restated as "How can I sum all the numbers the user entered in?" Computers do exactly what you tell them to, and you need to figure out how to exactly tell it to sum all the entered scores
.
Well how would you do that in real life? You'd keep a running tally of all the scores, maybe by adding them with a calculator. You'd first initialize a count:
double sum = 0.0;
Then after the line for `cout << "Enter the score..." you add to sum:
sum = sum + scores;
Or C++ has handy shorthand notation
sum += scores
Upvotes: 0