Hello xxazizixx1,
To go with what
salem c said.
PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.
Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
|
You have a program that is a goo start, but has problems.
First I would concentrate on the function that gets the input before you worry about anything else. Until you get something to work with the rest of the program does not do much for you.
Looking over the requirements of the program needed:
4)The question asks me to create a program to help a teacher to manage her students' test score.
And the teacher need to determine the number of the students. their names and scores.
then, i need to display the rank of each students based on their marks descending and convert their
marks to grade.
|
On line 1 it refers to a single score, but on line 2 it refers to "scores". Does this mean multiple scores per student or a single score for all the students. It does make a difference.
To start with you have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
#include <string>
using namespace std;
class student
{
private:
string studentName[];
char grade[10];
int studentMark[1000], studentnumber[1000];
public:
void getInput(); //Students name and scores from user
void display(); //output
void getRank(); //Sort rank in descending order
void getGrade(); //determining grade
};
|
This would work better as:
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 <string>
using namespace std;
constexpr int MAXSIZE{ 10 }; // <--- Change to 1000 later if needed.
constexpr int MAXGRADES{ 10 };
class student
{
private:
string m_studentNames[MAXSIZE];
char m_grades[MAXGRADES]{};
int m_studentMarks[MAXSIZE]{}, m_studentnumbers[MAXSIZE]{};
public:
void getInput(); //Students name and scores from user
void display(); //output
void getRank(); //Sort rank in descending order
void getGrade(); //determining grade
};
|
The first difference that you should notice is in line 9, your code, and line 12, my code. An array needs a constant number for the size. This could be a number like 10 or a variable defined as a constant.
Defining the constants on lines 6 and 7 as global variables you can use them in any line of code that follows. The other advantage is that you have 1 place to change the value and it will be used everywhere in the program that "MAXSIZE" is used.
Looking closer at the private variables I add the prefix "m_". I have seen this done in several tutorials. It is one way to keep the class variables different from a regular variable.
Also I added a "s"to the end of the variable names because they are arrays and the "s" helps you to remember that and it is more descriptive for what the variable holds, since it is an array.
1 2 3 4
|
void student::getInput()
{
string studentName;
int studentnumber, studentMark, maxNo;
|
Needs reworked.
If you are thinking that "studentName", "studentnumber" and "studentMark" are referring to the class variables with the same name they are not. You have defined local variables that overshadow the class variables. This could be useful, but since they have the same name as the class variable it is confusing.
The rest of the function works, somewhat, but at the same time does not work.
Your for loop is a good start, but you are not using it the way it needs.
"maxNo" first is a bad name and tends to give the wrong idea when you use it. Consider "index" or "idx". Second it is better to define the variable in the for loop as
for (int idx = 0; ...)
. This keeps the variable local to the for loop and is destroyed when the for loop ends. Unless there is some reason to keep the last value of the variable after the for loop ends.
Again you are storing the input into local variables, but not in the class arrays.
I have not had the chance to work on this function yet. When I do i will have a better idea what needs done.
To answer your question, which you are not quite ready for.
1 2 3 4 5 6 7 8 9 10 11 12
|
void student::display()
{
/* cout <<"======================================================"<<endl;
cout <<" Exam's Result "<<endl;
cout <<"======================================================"<<endl;
cout <<"Rank : Name: Score: Grade:"<<endl;
*/
cout << "Name: " << studentName;
cout << "Marks: " << studentMark[100];
}
|
In line 10 "studentName" is the name of the array, so all it will print is the address of the beginning of the array. The next line will print element 100 of the "studentMark" array. This is not what you want.
After you print the heading lines 10 and 11 plus others to match the column headings needs to be in a for loop to print out each element of the array that has a value.
Once you get the input working then you can work on the output. Till then the output will not be worth much if there is nothing in the array to print.
Andy