Incrementing

I'm trying to increment students by 1, but each time it shows output it says "Student 1..." again and again.

#include <iostream>
using namespace std;

int main()

{

//variables
int i = 0;
int student = i;
int midterm = 0;
int final = 0;
int total = 0;


//Prompt the user to enter the number of students.
cout << "Enter the number of students: ";
cin >> student;


while (i != -1)
{
cout << "Enter student " << i + 1 << " midterm grade: ";
cin >> midterm;
cout << "Enter student " << i + 1 << " final grade: ";
cin >> final;
total = midterm + final;


if (total >= 360 && total <= 400)
{
cout << "Student " << i + 1 << " grade is A." << endl;
}
else if (total >= 320 && total <= 359)
{
cout << "Student " << i + 1 << " grade is B." << endl;
}
else if (total >= 280 && total <= 319)
{
cout << "Student " << i + 1 << " grade is C." << endl;
}
else if (total >= 240 && total <= 279)
{
cout << "Student " << i + 1 << " grade is D." << endl;
}
else if (total < 240)
{
cout << "Student " << i + 1 << " grade is F." << endl;
}
else
{
cout << "Total can not be more than 400 points." << endl;
break;
}
}
}
Last edited on
Presumably you mean something like this:

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

int main()
{
    int students;
    cout << "Enter the number of students: ";
    cin >> students;

    for (int i = 0; i < students; ++i)
    {
        int midterm, final;
        cout << "Enter student " << i + 1 << " midterm grade: ";
        cin >> midterm;
        cout << "Enter student " << i + 1 << " final grade: ";
        cin >> final;

        int total = midterm + final;

        if (total > 400)
        {
            cout << "Total can not be more than 400 points.\n";
            break;
        }

        cout << "Student " << i + 1 << " grade is ";

        if (total < 240)
            cout << 'F';
        else if (total < 280)
            cout << 'D';
        else if (total < 320)
            cout << 'C';
        else if (total < 360)
            cout << 'B';
        else if (total <= 400)
            cout << 'A';

        cout << ".\n";
    }
}

Yes, actually. I appreciate the help.
Hello Awak3nDreams,

Given these lines of code and considering the rest of the while loop:
1
2
3
4
5
while (i != -1)
{
    cout << "Enter student " << i + 1 << " midterm grade: ";
    cin >> midterm;
    cout << "Enter student " << i + 1 << " final grade: ";

Where does the value if "i" change? And where ould "i" get set to (-1)?

Andy
Topic archived. No new replies allowed.