Help with my code?

Hi, I'm still learning about c++ and the object oriented programming.
on the first lesson the mentor required us to do a program which takes an input of n number of students where Student is a class and print out their grade in 3 subjects..

my problem is when I enter the last input of the n element of students, it overwrites everything in the previous students except for integers, so every string is overwritten, for example if the first student was named "John" and the second was "Max" they both would print out as "John", that also applies on the subject names.

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
#include<iostream.h>


class Student
{
	public:
	Student()
	{}
	Student(char* Name, char* s1, char* s2, char* s3, int g1, int g2, int g3)
	{
		name = new char [30];
		name=Name;
		sub1 = new char [30];
		sub1=s1;
		sub2 = new char [30];
		sub2=s2;
		sub3 = new char [30];
		sub3=s3;
		grade1=g1;
		grade2=g2;
		grade3=g3;
	}

	char* name;
	char* sub1;
	char* sub2;
	char* sub3;
	int grade1;
	int grade2;
	int grade3;

	void PrintTotal(int x, int y, int z)
	{cout<<x+y+z;}

	void PrintAvg(int X, int Y, int Z, int n)
	{cout<<(X+Y+Z)/n;}


};

main()
{
	int n, g1,g2,g3;
	char *sName, *sub1, *sub2, *sub3;
	cout<<"How many students are there?"<<endl;
	cin>>n;

	sName = new char [30];
	sub1 = new char [30];
	sub2 = new char [30];
	sub3 = new char [30];

	Student *Students = new Student[n];
	for (int i = 0 ; i < n ; i++)
	{
                 //taking input.
		 cout<<"Enter the student name: ";
		 cin>>sName;
		 cout<<"Enter the student's first subject: ";
		 cin>>sub1;
		 cout<<"Enter the student's second subject: ";
		 cin>>sub2;
		 cout<<"Enter the student's third subject: ";
		 cin>>sub3;
		 cout<<"Enter the student's grade in "<<sub1<<" :";
		 cin>>g1;
		 cout<<"Enter the student's grade in "<<sub2<<" :";
		 cin>>g2;
		 cout<<"Enter the student's grade in "<<sub3<<" :";
		 cin>>g3;
		 cout<<"-------------------------------------"<<endl;
		 Student S(sName,sub1,sub2,sub3,g1,g2,g3); //generating object.
		 Students[i]= S;  //storing objects in a pointer.
	}

	for (i = 0 ; i < n ; i++)
	{
                 //printing out.
		 cout<<"Name: " <<Students[i].name <<endl;
		 cout<<"Subject 1: " <<Students[i].sub1;
		 cout<<" - Grade: " <<Students[i].grade1 <<endl;
		 cout<<"Subject 2: " <<Students[i].sub2;
		 cout<<" - Grade: " <<Students[i].grade2 <<endl;
		 cout<<"Subject 3: " <<Students[i].sub3;
		 cout<<" - Grade: " <<Students[i].grade3 <<endl;
		 cout<<"Total = "; Students[i].PrintTotal(Students[i].grade1,Students[i].grade2,Students[i].grade3); cout<<endl;
		 cout<<"Average = ";  Students[i].PrintAvg(Students[i].grade1,Students[i].grade2,Students[i].grade3,n); cout<<endl;
		 cout<<"-------------------------------------"<<endl;
	}
} 



How many students are there?
2
Enter the student name: John
Enter the student's first subject: Maths
Enter the student's second subject: English
Enter the student's third subject: German
Enter the Student's grade in Maths: 50
Enter the Student's grade in English: 60
Enter the Student's grade in German: 70
-------------------------------------------------
Enter the student name: Max
Enter the student's first subject: Chem
Enter the student's second subject: Bio
Enter the student's third subject: Physics
Enter the Student's grade in Maths: 55
Enter the Student's grade in English: 66
Enter the Student's grade in German: 77
-------------------------------------------------
Name:  Max
Subject  1: Chem - Grade: 50
Subject  2: Bio - Grade: 60
Subject  3: Physics - Grade: 70
Total = 180
Average = 90
-------------------------------------------------
Name: Max
Subject 1: Chem - Grade: 55
Subject 2: Bio - Grade: 66
Subject 3: Physics - Grade: 77
Total = 198
Average = 99
-------------------------------------------------


The point is everything is correct except for the value of name and subjects.
Last edited on
Lines 12/14/16/18 are incorrect. Use strcpy instead:

http://www.cplusplus.com/reference/clibrary/cstring/strcpy/
@coder777
Lines 12/14/16/18 are incorrect. Use strcpy instead:

http://www.cplusplus.com/reference/clibrary/cstring/strcpy/


Works like a charm thank you!
can you or anyone else passing explain why wasn't my original code working.
I can't get the hold of how strings fully work.

Thank you again.
You just replaced the pointer. So new on line 11 etc don't have any effect other than wasting memory.

In other word name within each Student object reference to the memory acquired on line 48.
Similar with sub.
Thank you that helped alo!
I thought each time I wrote that line

1
2
name = new char [30];
name=Name;


The object took the value of sName and put it in the value of Student[i].name

but now it's somehow clear.

Thanks again.
Topic archived. No new replies allowed.