Addition inside loop.

Hey guys,I need someone to inform me what I'm doing wrong. I'm trying to add inputted test scores inside of a loop to get an average. My problem is, the program will not add all of the inputted scores. It only adds the last number plus itself.
Thank you.

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

int main ()
{
int scores, total, test, average, start=1;
string name;
cout << "Please enter your name: ";
cin >> name;
cout << "How many test scores do you have: ";
cin >> scores;

while (start <= scores)
{
cout << "Please enter your score for test " << start++ << endl;
cin >> test;
total = test + test;
}
average = total / scores;
cout << "The test average for " << name << " is " << average;

system("pause");
return 0;
}
You meant total = total + test; for the third line of the while loop, right?

EDIT: Or, alternatively:
total += test;
Think of the += operator as a synonym of
= (variable on left side) +

-Albatross
Last edited on
Ahhh, count=count+1... Thanks for catching that, that worked! I appreciate you taking the time to answer my question. Now i need to remember to use the += operator.
Topic archived. No new replies allowed.