# user input loop

• 12-21-2004
asudave2302
user input loop
hey im havin a hell of a time doin sumthing that shud be easy ... ok the assignment is to prompt the user to input the number of grades they want to enter ... then im spose to get the average of those grades and output it all within a for loop .. . can u guys help me out?
• 12-21-2004
andyhunter
Well, why don't you try something and then post what you did and then we will help. Becareful, posting questions like this with no work will get you flammed.

Hint : use a for loop, I am sure you have gone over it in class.
• 12-21-2004
asudave2302
ight heres wut i have done (in my main)
int j;
int loop_limit;
int score;
double average;

cout << "How many scores do you want to enter? ";
cin >> loop_limit;

for (j = 1; j <= loop_limit; average = score / loop_limit, ++j)
{cout << "Enter a score: ";
cin >> score;
average = score / loop_limit;
}

cout << average << endl;
• 12-21-2004
andyhunter
Ok, before anyone flames please use code tags. Simply place [*code] the begginning and [*/code] (minus the asterisk)at the end of the code you submit to make it more readable.

2. no one is going to do the assignment for you so I will point you in the right direction.

Code:

``` int main(void) {           int currentScore, numberScores, score, total, average;           cout << "Enter how many scores you wish to average:";           cin >> numberScores;           for(currentScore = 0; currentScore < numberScores; currentScore++) {                                           //get the scores and add them to total           }           //calculate and display average           return 0; }```
That should get you off to a good start. Happy coding :D
• 12-21-2004
asudave2302
hey one more thing .. i just dont understand how to take in each score as being separate and add them together
• 12-21-2004
Sebastiani
well, you could add them right there in the loop to a 'total' (initialize to zero). you can use an array, too. also, you'll want to calculate the average *after* the loop terminates.
• 12-21-2004
jlou
Code:

``` int score = 0;  int totalscore = 0; ...   cin >> score;   totalscore += score;```
Keep a running total of the scores, and divide that by the number of scores entered. Inside the loop this will be j, after the loop is done this will be loop_limit. If you aren't understanding why that works, think about it, then ask.
• 12-21-2004
CornedBee
i, j etc. are perfectly helpful names. Every programmer knows that a variable called i (or j, k, m, ... if i already exists) is a loop index.

However, in C++ loop indices should be declared inside the loop:
for(int i = 0; i < max; ++i)
• 12-21-2004
andyhunter
Quote:

However, in C++ loop indices should be declared inside the loop:

I don't even need to say anything. ;)
• 12-21-2004
CornedBee
Huh?
• 12-21-2004
andyhunter
In the original code listing "int j" was just declared and then later on it was used in the loop. Thus the variable was viable for the entire function. I prefer not to use cryptic loop index names if their scope extends beyond the loop. Just a style point for me though.
• 12-21-2004
CornedBee
Yes, the code was very C-style in that regard. In C, the variable HAS to be declared at the start of the function, yet programmers didn't want to use long names for the indices, so they just assumed (mostly correctly) that anyone seeing them would know that they are indices.