I am writing a program where I need a for loop to run a certain amount of times, depending on user input.
int num will be entered by the user.
Everything is declared and no syntax errors, the counter just doesn't work.
I currently have this function:
1 2 3 4 5 6 7 8 9 10 11 12 13
double getScores(int num){
int i;
double score_total = 0;
double score;
for( i=0; i < num; i++){
cout << "counter: " << i << endl; // check to see if counters working
cout << "num: " << num << endl; //check if user input still holds true.
cout << "Enter your score: ";
cin >> score;
score_total = score_total + score;
}
return score_total;
}
everything works fine, I don't have any syntax errors. However when I compile the program, my cout statement shows the counter being at 0, all the time.
Lets say num is 3, the statement is printed out only twice and the counter remains at zero..
Can someone help me take a look at what might be wrong?
I can't duplicate your error description. When I run the program I get the following output.
1 2 3 4 5 6 7 8 9
counter: 0
num: 3
Enter your score: 3
counter: 1
num: 3
Enter your score: 4
counter: 2
num: 3
Enter your score: 5
The value of total and counter after the loop: 12 3
Which is what I would expect. The counter starts at zero and increments each time through the loop until it it is larger than num. The variable num starts at 3 and remains the same throughout the loop.