Do While Loop to Repeat Program

With my program, it is supposed to collect a student's 5 grades and get the average. I've figured out that part with assistance. The second part is to ask if there are any more students using a do while loop to repeat the process if there are. If there are no more students, to end the program, the user should enter a -1 to end the program. But I'm having trouble with repeating it and entering the -1 to end the program. If you could explain what I'm doing wrong, I would really appreciate it. With my output, it will correctly run the first entry, but it goes wrong when My output with the second part is: "Would you like to enter an additional student? -1
Enter Student's First and Last Name:
Enter Grade 1:" And it doesn't allow me to enter a second student's name, it just automatically goes to "Enter Grade 1:" Any guidance would be greatly appreciated.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  #include <iostream>
#include <string>

using namespace std;

int main()
{
    int i;
    float grades, sum = 0, avg;
    string studentName, choice;
    

    cout << "Enter Student's First and Last Name: ";
    getline(cin, studentName);
    cout << endl;

    for (int i = 1; i <= 5; i++)
    {
        cout << "Enter Grade " << i << ": ";
        cin >> grades;
        sum += grades;
    }
    cout << endl;

    avg = sum / 5;
    cout << studentName << "'s grade average is: " << avg;
    cout << endl;
    

    sum = 0;

    do
    {
        cout << "Would you like to enter an additional student? ";
        cin >> choice;

        cout << "Enter Student's First and Last Name: ";
    getline(cin, studentName);
    cout << endl;

    for (int i = 1; i <= 5; i++)
    {
        cout << "Enter Grade " << i << ": ";
        cin >> grades;
        sum += grades;
    }
    cout << endl;

    avg = sum / 5;
    cout << studentName << "'s grade average is: " << avg;
    cout << endl;
    

    sum = 0;
    
    } while (choice != "-1");
The do...while loop goes around lines 9 - 27. You don't need i defined on line 8. Lines 29- aren't needed. The lines 34-35 go after line 27

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
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string choice;

	do {
		float grades, sum = 0;
		string studentName;

		cout << "Enter Student's First and Last Name: ";
		getline(cin, studentName);
		cout << endl;

		for (int i = 1; i <= 5; i++)
		{
			cout << "Enter Grade " << i << ": ";
			cin >> grades;
			sum += grades;
		}

		cout << endl;
		cout << studentName << "'s grade average is: " << sum / 5;
		cout << endl;
		cout << "Would you like to enter an additional student (-1 to terminate)? ";
		cin >> choice;
		cin.ignore(1000, '\n');

	} while (choice != "-1");
}


Topic archived. No new replies allowed.