For Loop; totalling input values

I'm having trouble figuring out how to total the values that are put in during the loop. For this program I need to add up the values the user inputs and then display them after the loop. Here is what I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()

{
    const int firstQuiz = 1, lastQuiz = 4;
    int quiz, quizGrade;
    
    for (quiz = firstQuiz; quiz <= lastQuiz; quiz++)
    {
        cout<<"Enter the grade for quiz "<<quiz<<":"<<endl;
        cin>>quizGrade;
    }
    
    cout<<quizGrade;
    
    return 0;
}
You need a separate variable to store the total. Initialize it to zero.
int total = 0;
Then each time you read in a grade, add the value to the total:
total += quizGrade;
This is shorthand for total = total + quizGrade
I've added the int total = 0 along with the total = total + quizGrade, but it's still just returning the last value the user enters?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()

{
    const int firstQuiz = 1, lastQuiz = 4;
    int quiz, quizGrade;
    int total = 0;
    
    for (quiz = firstQuiz; quiz <= lastQuiz; quiz++)
    {
        
        cout<<"Enter the grade for quiz "<<quiz<<":"<<endl;
        cin>>quizGrade;
        total = total + quizGrade;
    }
    
    
    cout<<quizGrade;
    
    return 0;
}
Look at line 23. What do you print there?
Thank keskiverto, sometimes you stare at these things forever and miss the littlest things
Topic archived. No new replies allowed.