Confused how to modify a program I wrote last week

Last week for class I wrote the program below. This week part of our assignments are to the program take this program and modify it so the user can display the grade for as many students as they choose.

What I am not understanding is, is it asking to be able to let the user enter each students name individually and then have it display a list of names with the grades associated with it? If this is the case would this be accomplished with a string statement? if it is a string statement I am not sure I would know to do it.

Or is it just asking to display a whole bunch of grades at the end for each student the user enters? If this is the case would I go about this by writing a variable that would keep track of each score seperatly until the user says every score and student is entered?

Sorry I feel super lost this week in C++
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
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
	//declare variables
	int score = 0;
	int totalPoints = 0;  //accumulator
	char grade = ' ';

	//get first score
	cout << "First score (negative number to stop): ";
	cin >> score;

	while (score >= 0)
	{
		//add score to accumulator
		totalPoints = totalPoints + score;
		//get next score 
		cout << " Next score (negative number to stop); ";
		cin >> score;
	}//end while

	//assign grade
	if (totalPoints >= 360)
		grade = 'A';
	else if (totalPoints >= 320)
		grade = 'B';
	else if (totalPoints >= 280)
		grade = 'C';
	else if (totalPoints >= 240)
		grade = 'D';
	else grade = 'F';
	//end ifs

	//display total points and grade for each student
	cout << endl;
	cout << "Total points: " << totalPoints << endl;
	cout << "Grade: " << grade << endl;

	return 0;
}//end of main function 


Thanks for the help everyone!!
I would do it so that you ask for a students name, and then his scores, and loop that process until you're done. Then do another loop at the end to display your students names and their grades.

As a tip, I would #include <string> and use an array of strings.

If you need any help on writing the code I just wrote your program lol :)
Thanks for the reply mcleano!! Yeah could you help me with how to start the string arrays please. I am having a hard time grasping how to add the strings in. I atleast was thinking correctly that I should get the student name and store the name and there scores. And I am happy I realised I would need a string before it was said so I think I am sort of getting a grasp.
Thanks again mcleano and you can try sending me a sample of how and where to add the string arrays and I can give it a shot first and see if I can do it.
Ok, well I tried to stick to your structure without changing it too much so here is the beginning... read through it and ry and finish it off yourself:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
	//declare variables
	int score = 0, i = 0;
	int totalPoints[3], totalPointsTemp = 0; 
	char grade[3];
	std::string names[3];

	while (i<3) // an outer loop
	{
	    std::cout << "Enter students name: ";
	    std::getline(std::cin, names[i]);
		
	    //get first score
	    std::cout << "First score (negative number to stop): ";
	    std::cin >> score;
	    std::cin.ignore();


Note: if you have for example three arrays - one holding the name, one holding, points, one holding grades - you can have them so that they line up. By that I mean, the first element([0]) of the point and grade arrays, will correspond to the first name. The second([1]) element of the point and grade arrays, will correspond to the second name etc...
Awesome thank you big time. I will study this and give it a shot and post what I come up with even if I get it to work so you can make sure even though it works that I am coding things properly.
Well this is what I was able to come up with so far. It seem that I am really close to the desired results. If I only enter one student everything works fine. But if I enter more than one student the results for the student before disapears.
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 <string>
#include <iostream>

using namespace std;

int main()
{
    //declare variables
    int score = 0, i = 0;
    int totalPoints = 0, totalPointsTemp = 0;
    char grade = ' ';
    string names[40];

    for (int x;;x += 1) // an outer loop
    {
        cout << "Enter students name: " << endl;
        getline(cin, names[i]);

        //get first score
        totalPoints = totalPoints + score;
        cout << endl << "First score (negative number to stop): " << endl;
        cin >> score;
		if (score >= 0)
			totalPoints += score;
		else
			break;

        //get next score
        cout << endl << "Next score (negative number to stop): " << endl;
        cin >> score;
		if (score >= 0)
			totalPoints += score;
		else 
			break;
    }

    //assign grade
    if (totalPoints >= 360)
        grade = 'A';
    else if (totalPoints >= 320)
        grade = 'B';
    else if (totalPoints >= 280)
        grade = 'C';
    else if (totalPoints >= 240)
        grade = 'D';
    else grade = 'F';
    //end ifs

    //display total points and grade for each student
    cout << endl;
    cout << names[i] << endl;
    cout << "Total points: " << totalPoints << endl;
    cout << "Grade: " << grade << endl;

    return 0;
}//end of main function 
Last edited on
A few things you've forgot to do so far (haven't looked at whole program):

You haven't got arrays for grades and points so you're never storing any results for each student.
Also, the code you have got to input scores isn't in a loop so you can only enter a maximum of 2 scores.
Lastly, you're not incrementing i so you everytime you save a students name you're overwriting the first element.

Refer back to the NOTE section in my previous post. Also take another look at the start of my example again.
Topic archived. No new replies allowed.