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++

#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!!

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?

Hmm, smells like you need a structure (often abbreviated as: struct).
structs are used to group related variables into a new user-defined type, let me give you an example, you want to store the student name, together with his grade(s), in that case you could create a structure which looks like this:

struct student
{
  string name;     // to store the student's name
  int grades[20]; // make room for storing 20 grades per student
};

If you only need to store one grade per student, then you can also just replace the array declaration int grades[20] by this: int grade; , but this will allow only 1 grade to be stored per student (if storing multiple grades is not yet part of your assignment, I'm sure it will follow).

Now you've a structure, you can create a structure variable from it, or why not direct a whole array? (as you want to store multiple students)
You can declare a structure variable of type student like this:

student stud0;

Now, lets do something interesting with it, lets change his name and set some grades:

stud0.name = "John"; // set name
stud0.grades[0] = 5;  // give 'John' five out of whatever you want
stud0.grades[1] = 7;  // set another score

A key thing you should remember is that the array in the structure remains uninitialized, this means that you should only access the values you've set, this is pretty clunky, don't you think?
Why not write some code which initializes all scores to -1 for example, so we can see if a student made a test or not.

// Initialize the array
for(int i = 0; i < 20; ++i) {
    stud0.grades[i] = -1;
}

(this loop needs to run 20 times because we want to store 20 grades per student)
Now that we know that -1 can be an indicator to print a grade or not, we can print out only the grades which aren't negative.
(In general you can use this as an indicator to 'see' whether the student has made a test or not).
We can do this like shown here:

// Print out a student's grades
cout << "Grades for student: " << stud0.name << endl;
for(int i = 0; i < 20; ++i) {
  if( stud0.grades[i] > 0 ) {
     cout << stud0.grades[i] << endl;
  }
}

Now you've the base, you can start extending the whole thing, for example, why not add some code which displays a message if the user didn't make a particular test?
And...why not add some code which prints out the test's number?
That would be more interesting, not?

Let's do that now:
This is the previous code, which only prints scores which are 'set' ('set' means here: the grade is higher or equal to zero):

// Print out a student's grades
cout << "Grades for student: " << stud0.name << endl;
for(int i = 0; i < 20; ++i) {
  if( stud0.grades[i] > 0 ) {
     cout << stud0.grades[i] << endl;
  }
}

Let's revise what we want to add:

  • Display the number of the test.
  • Display a message when the user hasn't made the test.
// Print out a student's grades
cout << "Grades for student: " << stud0.name << endl;

for(int i = 0; i < 20; ++i) {
  cout << "Test #" << i << ": "; // Display test number
  
  if( stud0.grades[i] > 0 ) {
     cout << stud0.grades[i] << endl;
  } else { // Display message
     cout << stud0.name << " didn't make this test." << endl;
  }
}

Let's start 'building' an array of students (yes, this is also possible), it's not something special, you can declare an array of type student as follows:

student myClass[15];

don't confuse the word 'class' here with the 'class' keyword from C++, here 'class' simply means a 'class' (like: a class of students).
In our class of students, we made room for 15 students (as you can see in the array declaration).
You can access elements in the array as follows (remember that 'element' is data of type student in this example, so we can do things like this:

myClass[0].name = "John"; // set name of 1st student
myClass[0].grades[13] = 6; // set grade of 1st student (on the 14th test)

As demonstrated in this post, I guess structures will offer you the solution you need, the second part (turning the whole thing into a useful program) is up to you, you'll probably want to add things like this:

  • Let the user enter the name of the user whose scores he wants to see.
  • Compute how good the student performs in comparison to the other students in the same class.
  • And maybe: many more...

Edit I::
I just see you want to store the grades with their matching letters, depending on what your needs are, you can just perform a conversion to the grade at the moment when the corresponding letter is needed, for example: 'A', 'B', 'C', etc...
Or you could replace the grade array of type int with an array of type char, if you only want to store the letters which represent the grade.
The student structure would be modified to this, if you only want to store the letter which represent the grade(s):

struct student
{
  string name;        // to store the student's name
  char grades[20]; // make room for storing 20 grades per student
};

(here the type of array 'grades' is changed from int to char), please note that you shouldn't store a grade like this, because it will give unexpected output:

// This is wrong!
student stud0;
stud0.name = "John";
stud0.grades[0] = 35; // set '35' as John's score

This example is obviously wrong because it will place the ASCII equivalent if 35 in the array, and not the grade 'F', so be sure that you do a conversion first.
(All the previous code in my post would need to be changed as well, if you want to do it the 'character array'-way).
Also, if you want to take this approach, you'll probably want a character to determine whether a test was made or not.
(As long as you know the whole size of the character array it is safe to use a null-terminator for this purpose).

Edit II:: So the resume my whole post in one line: You could use structures for this.

commented: Have to Admire the effort on explaining the basics :) !!! +6
commented: Just had to rep a post like this, looks like plenty of effort went into it. :) +1

Just notice a small mistake in my previous post, I said this:

Let's do that now:
This is the previous code, which only prints scores which are 'set' ('set' means here: the grade is higher or equal to zero):

And this was some related example code:

// Print out a student's grades
cout << "Grades for student: " << stud0.name << endl;
for(int i = 0; i < 20; ++i) {
  if( stud0.grades[i] > 0 ) {
     cout << stud0.grades[i] << endl;
  }
}

But you'll need to modify the if-statement so that it becomes:

if( stud0.grades[i] >= 0 ) { // or > -1
   cout << stud0.grades[i] << endl;
}

You'll have to do this through my whole post.

I also said something like:

Let's start 'building' an array of students (yes, this is also possible), it's not something special, you can declare an array of type student as follows:

Here with:

it's not something special

I meant that it's not different from declaring any other array.

Further I noticed an irritating typo in this:

This example is obviously wrong because it will place the ASCII equivalent if 35 in the array, and not the grade 'F', so be sure that you do a conversion first.

The 'if' has to be 'of'.

Edit II:: So the resume my whole post in one line: You could use structures for this.

Ha! How much time did it take you to write that one line, Tux?

But your posts and the OP's post raise a point. Neither the OP nor tux4life is entirely sure of the GOAL of the assignment (and neither am I).

Quote from the OP:

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.

I don't know if there is a project specification or not, but I imagine the requirements are more detailed than what is posted. That's too vague to write a program from. The question is what needs to be stored and what can be thrown away. Does one have to store all of the separate number grades that make the letter grade for an individual student, or just the letter grade, which is calculated? It's impossible to say from the original statement. You'll have to ask your teacher or decide on your own before you can proceed. I'd also say you possibly have a problem in your ORIGINAL program:

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

Seems like this calculation assumes there are four scores. But you can't assume that, right? Otherwise you wouldn't have the "negative number to stop" instruction. Therefore, are you sure you want to hard-code 240, 280, 320, and 360?

Find out for sure what is required, then design your struct and your program around that spec.

What I wrote for the modifying is directly from the book the assignment. Thats why I feel so lost as to what to do because thats all it says. THANKS Tux I am going to print what you wrote and read and start working on my prog. Hopefully I can get it done without anymore help.

I will post results soon.

Thanks guys!!!!

The code I posted above is what the assignment is expecting me to start from. Was a previous weeks project.

In this exercise, you modify the program you created. The modified program will allow the user to display the grade for as many students as needed.

That is exactly how the assignment is worded. So I think that means the same test amount being entered and just as many students as the teacher has in the class. Does this require the same thing that your talking about Tux?

Thanks again guys.

So, you need to make modifications to code you created in a previous assignment.
Is it possible to post the whole description of that previous assignment as well?

The previous assignment was stated as follows:

Create a program that calculates the total points earned on projects and tests then assigns a grade and then displays the total points earned and grade. Make sure you use a negative number to stop the program at any point.


When I run the program it will continually ask me another score until I strike -1. Not sure why you think it will only ask 4 times? I just re ran the orig prog to make sure and will continually loop until -1 is entered.

Did this help for what you were wondering?

The previous assignment was stated as follows:

Create a program that calculates the total points earned on projects and tests then assigns a grade and then displays the total points earned and grade. Make sure you use a negative number to stop the program at any point.


When I run the program it will continually ask me another score until I strike -1. Not sure why you think it will only ask 4 times? I just re ran the orig prog to make sure and will continually loop until -1 is entered.

Did this help for what you were wondering?

I understand that it will ask as many times as you want. I'm saying that your CALCULATION code (not your input code) assumes the person will enter four answers:

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

360 / 4 = 90 (generic cutoff for A)
320 / 4 = 80 (generic cutoff for B)
280 / 4 = 70 (generic cutoff for C)
240 / 4 = 60 (generic cutoff for D)

What if I have 400 scores and I enter them all and they're all scores of 1 (presumably out of 100), so I flunked every assignment. By your code I'd get an A.

I got you. That now makes sense. I am thinking the grade part needs to stay the same because I am thinking its not asking for endless grade input just endless student input. So would I need to put something in the student array that a -1 or something similar would trigger it to stop asking for more student and display the end result?

I just re read both assignments. I think its just making the code I posted above to be able to print the students name with the entered scores using the same 4 scores formula. and using -1 or something similar to end asking for students and display end result.

Hope I am making sense I feel like I am confusing myself and others more :P

I just re read both assignments. I think its just making the code I posted above to be able to print the students name with the entered scores using the same 4 scores formula. and using -1 or something similar to end asking for students and display end result.

I guess you want to do something like this, please correct me if I'm wrong:

Welcome to our simple student grades database!
To quit the program and display the results, enter an empty student name.
To stop entering grades for a particular student, enter a negative number.

Enter student name: John [I]<ENTER>[/I]
Enter grade #1: 50 [I]<ENTER>[/I]
Enter grade #2: 325 [I]<ENTER>[/I]
Enter grade #3: 253 [I]<ENTER>[/I]
Enter grade #4: -1 [I]<ENTER>[/I]

Enter student name: Tom [I]<ENTER>[/I]
Enter grade #1: 211 [I]<ENTER>[/I]
Enter grade #2: -1 [I]<ENTER>[/I]

Enter student name: [I]<ENTER>[/I]

The results (listed per student):

----- Results for John -----
Grade #1: F
Grade #2: B
Grade #3: C

----- Results for Tom -----
Grade #1: F

(<ENTER> means that you press the enter key at that point)

Yes Tux4Life that sounds correct to me. Or atleast that works for me I should say. Lets do what you just said for the program. Do I just look at your last example and use the ones from before and try and build from there with a string array?

Sorry for what are probably really basic and stupid questions. We hit these last two chapters in the book and I have been just feeling like i totally lost what skills I thought I was developing.

Do I just look at your last example and use the ones from before and try and build from there with a string array?

Depends on what you're planning to do with it.
There are two approaches you'll want to consider, you have to pick the one which fits best your needs.
The first approach is just storing the letter for each grade, for example: 'A', 'B', 'C', 'D' or 'F'.
But there's a downside if you use this approach: what if you want to compare two students with grade 'A', to see which one did better?
It won't be possible here, because you only stored the letter which represents a grade (a letter which represents in which range the users score on a particular test was).

The second approach is store each student's exact score, for example: 325, 89, 426, etc. and convert them to their appropriate grades, only at the moment when you need their corresponding letter.
Also this approach has a downside (if you really can call it like this):
Each time when you need the grade in form of their alphabetic representation ('A', 'B', 'C', 'D' or 'F'), you'll have to call a conversion function, which converts the exact score to the letter which represents the grade (you've already code which does this, you could for example put this code in a function, which you could call each time when you want to convert an exact score).

So, it's up to you to decide which approach you're going to use, before you start writing the rest of the program.

I am thinking the grade part needs to stay the same because I am thinking its not asking for endless grade input just endless student input.

...

I just re read both assignments. I think its just making the code I posted above to be able to print the students name with the entered scores using the same 4 scores formula.

I guess you want to do something like this, please correct me if I'm wrong:

Welcome to our simple student grades database!
To quit the program and display the results, enter an empty student name.
To stop entering grades for a particular student, enter a negative number.

Enter student name: John [I]<ENTER>[/I]
Enter grade #1: 50 [I]<ENTER>[/I]
Enter grade #2: 325 [I]<ENTER>[/I]
Enter grade #3: 253 [I]<ENTER>[/I]
Enter grade #4: -1 [I]<ENTER>[/I]

Enter student name: Tom [I]<ENTER>[/I]
Enter grade #1: 211 [I]<ENTER>[/I]
Enter grade #2: -1 [I]<ENTER>[/I]

Enter student name: [I]<ENTER>[/I]

The results (listed per student):

----- Results for John -----
Grade #1: F
Grade #2: B
Grade #3: C

----- Results for Tom -----
Grade #1: F

(<ENTER> means that you press the enter key at that point)

Yes Tux4Life that sounds correct to me. Or atleast that works for me I should say.

See highlighted quotes in red.

Why would you use a grading system based on 4 scores if there could be more than 4 scores or less than 4 scores? And the grading system is based on 400 possible points (at least if you are using the 90-80-70-60 breakdown). Tux's example has someone getting like 600 points, which is more than 400. Tux's example also has one grade for each individual score. Your original program has ONE grade for each student based on the TOTAL points. So you can base the program off of Tux's example run or you can base it off your original program. But you have to pick one or the other because they are different.

Has to be off the first program. The reason that formula was used was because it was what was stated to use as the formula. But it has to be off the original program

Would this be a good way to start going from the original prog?

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();

Would this be a good way to start going from the original prog?

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();

From post 1:

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.

I'm the user. I choose to enter and display information for twenty students. Can I do this with your program?

Has to be off the first program. The reason that formula was used was because it was what was stated to use as the formula. But it has to be off the original program

Well the formula has some assumptions somewhere and there is some reasoning, presumably, for the numbers in this formula and if your teacher hasn't explained and you don't understand what the assumptions and the reasoning is, you should ask. You should also tell the user what is expected, what an A represents, etc. If I run the program, I have no idea that if I want the program to give me an A, I have to have points that total more than 360 to get an A. I run your program, it tells me to enter three scores, it tells me nothing about the range of the points, so I enter ten three times and I get an F, but I have no idea why. Don't assume the user has any idea what he/she is supposed to enter or has any idea what the program does. You have to tell them.

Well I have to assume the user knows what they want from this program because thats what the book and the teacher are assuming from me. They say it as Jane Doe wasnt a program that will do "X" then here are the guidlines and if they want a formula used like the one that gives the grades its stated.

Either I am not reading into this problem enough or I think others are reading to deeply into it.

I just need to be able to enter the students name how ever many scores the teacher wants to enter with using a neg num to end it. Then display the students names in some kind of order with the associated grade and score.

Not trying to come off rude if it seems that way, I am just frustrated and can only give you as much information as I have. My teacher would just tell me exactly this. " Do no more or less than what the book specifically states in the assignment. "
Unfortunatly I can not get help from my teacher while I am working on the problem at home on a weekend and cant always afford to wait until the next time I can get in or there is a class or anything like that.

Anyways hope someone can help show me what I am missing again here please

#include <string>
#include <iostream>

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

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

	while (score >= 0) // an outer loop
	{
		cout << "Enter students name: " << endl;
		getline(std::cin, names[++i]);

		//get first score
		totalPoints = totalPoints + score;
		cout << endl << "First score (negative number to stop): " << endl;
		cin >> score;
		cin.ignore();

		//get next score 
		cout << endl << "Next score (negative number to stop): " << endl;
		cin >> score;
		cin.ignore();
	}

	//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

Well I have to assume the user knows what they want from this program because thats what the book and the teacher are assuming from me. They say it as Jane Doe wasnt a program that will do "X" then here are the guidlines and if they want a formula used like the one that gives the grades its stated.

Either I am not reading into this problem enough or I think others are reading to deeply into it.

I just need to be able to enter the students name how ever many scores the teacher wants to enter with using a neg num to end it. Then display the students names in some kind of order with the associated grade and score.

Not trying to come off rude if it seems that way, I am just frustrated and can only give you as much information as I have. My teacher would just tell me exactly this. " Do no more or less than what the book specifically states in the assignment. "
Unfortunatly I can not get help from my teacher while I am working on the problem at home on a weekend and cant always afford to wait until the next time I can get in or there is a class or anything like that.

Anyways hope someone can help show me what I am missing again here please

#include <string>
#include <iostream>

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

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

	while (score >= 0) // an outer loop
	{
		cout << "Enter students name: " << endl;
		getline(std::cin, names[++i]);

		//get first score
		totalPoints = totalPoints + score;
		cout << endl << "First score (negative number to stop): " << endl;
		cin >> score;
		cin.ignore();

		//get next score 
		cout << endl << "Next score (negative number to stop): " << endl;
		cin >> score;
		cin.ignore();
	}

	//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

I know you're not trying to be rude. I'm not trying to be rude either, but there's a larger point and it goes well beyond how to code a particular program in C++. It has to do with programming in general and how to post on forums. The point is that you can't code anything until you know what the program's goals and what the program's assumptions are. You clearly don't, given your first post:

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?

Or is it just asking to display a whole bunch of grades at the end for each student the user enters?

The fact is that you didn't understand what you were supposed to do, so since we're getting all of the information from you, we can't understand what you're supposed to do either, and we can't help much with the nitty gritty till we understand the overall goal and the exact requirements.

When you post, put all of the specifications, whatever you have, in the first post, because with something this vague, people are definitely going to ask for clarification, and if the clarifications don't answer all of the questions, they're going to ask for clarifications on the clarifications. And if something doesn't make any sense, based on either the spec itself or something else you're doing or saying, people are going to ask for a clarification on THAT. That's not considered reading too much into it. It's absolutely essential, so you have to expect the questions. Expect to have to read the specification ten times, slowly, word for word, before understanding what you're being asked to do. This specification is pretty short and vague.


So as far as what your teacher says: "Do what's in the spec and nothing more...", here's the first spec...

Create a program that calculates the total points earned on projects and tests then assigns a grade and then displays the total points earned and grade. Make sure you use a negative number to stop the program at any point.

Here's the second spec...

In this exercise, you modify the program you created. The modified program will allow the user to display the grade for as many students as needed.

Nothing in there says to use a struct or to store anything for later use. So no arrays. You can have as many scores for a student as you want and as many students as you want. The wording for the spec is too vague for my taste, but if it's what you have, it's what you have.

There's also nothing in the spec about asking for anyone's name, but if you want to ask for the name, keep it in there.

So if you keep the name, you need four variables:

  1. A string to store a single student's name.
  2. An int to store the points from a single test.
  3. An int to store the cumulative test points.
  4. A char to store the letter grade.

These are the same four variables you needed originally (assuming you asked for the name) when you only had to worry about ONE student. You don't have to add anything. Here's your outline.

string name;
char letterGrade;
int pointsForSingleTest;
int cumulativePoints;

// Prompt for first student's name
// Read in first student's name.
while (/* check to see whether we're done entering students*/)
{
    // Initialize cumulativePoints to 0.
    // Prompt for first test score.
    // Read in first test score.
    while (/* check to see if we're done entering test scores for this student */
    {
        // add this test score to cumulative points.
        // Prompt for next test score.
        // Read in next test score.
    }

    // calculate letter grade using formula.
    // Display this student's name, total points, and letter grade.
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.