Problems with my code.

1)First of all, i am very new with c++. know a bit or two seriously. I have assignment due next week.
2)Why my program display address memory instead of the value entered by the user?
3)Do i need to use switch or just use if loop to convert students mark to grade.
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 score(1 score only)(edit/sorry my bad). then, i need to display the rank of each students based on their marks descending and convert their marks to grade.
so this is my code. So basically i need to display Rank,Name,scores and grade. With current code, Im able to ask user to give input, but unable to print it back. Im really stuck rn

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#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

	

		
};

void student::getInput()
{
	string studentName;
	int studentnumber, studentMark, maxNo;

	
	cout << "Enter number of students : " ;
	cin >> studentnumber;
	
	for(maxNo=0; maxNo<studentnumber; maxNo++ ){
		cout << "Enter the student's data # " << maxNo+1<<".\n";
		cout << "Student Name:";
		cin>>studentName;
		cout << "Exam Score:";
		cin >> studentMark;
		cout <<endl;
		if(studentMark > 100 ){
			cout << "Invalid score! Please enter a valid score."<<endl;
			cout << "Exam Score:" ;
			cin >> studentMark;
			cin.ignore();
		}else if (studentMark<0){
			cout<<"Negative value! Please enter a valid score." <<endl;
			cout << "Exam Score:";
			cin >>studentMark;
			cin.ignore();
		}else {
			cout <<endl;
		}
	}
	
}

void student::getGrade()
{
	int grade;
	
	if (grade >= 80){
		cout << "A"; 
	}else if ( grade >= 70){
		cout <<"B";
	}else if (grade >=60){
		cout << "C" ;
	}else if (grade >=50){
		cout <<"D" ; 
	}else {
		cout<<"F";
	}
	
	
}


/*void convGrade(int studentMark)
	{
		switch(studentMark)
		{
			case(studentMark >=80) :
				cout << "A"<<endl;
				break;
			case(studentMark >=70) :
				cout <<"B"<<endl;
				break;
			case(studentMark >=60) :
				cout<< "C"<<endl;
			case(studentMark >=50) :
				cout << "D" <<endl;
				break;
			case(studentMark <=49) :
				cout << "F" <<endl;
				break;
			default:
				cout <<"Invalid Number." <<endl;
		}
	}*/


 

void student::getRank(){
	int studentMark[1000], temp;
	
	for(int i=0; i<=100; i++){
		if (studentMark[i]<studentMark[i+1]){
			temp= studentMark[i];
			studentMark[i]=studentMark[i+1];
			studentMark[i+1]=studentMark[i];
		}
	}
	for(int i=0; i<100; i++)
	cout<< studentMark[i]<<endl;
}

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];
	
	
	
}


int main()
{
	student s;
	s.getInput();
	
	s.getGrade();
	s.display();
	}	

Last edited on
Please edit for readability.
https://www.cplusplus.com/articles/jEywvCM9/
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
Thank you very much Andy, I'll try to reedit my code with what you've told me. Now I'm a bit clear with array after what you've explained to me. I'll work on this and I'll update anything here later. Thanks! XD

Topic archived. No new replies allowed.