struct,array

I am trying to create an array of struct but errors keep on occurring, and i do not know why, plz help

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
  #include <iostream>
#include <string>
using namespace std;

struct student{

char name;
int age;
char stream;
int mark;

student( char n,  int a,  char s,  int m);
};

student::student( char n,  int a,  char s,  int m){

name = n;
age = a;
stream = s;
mark = m;
}

int main(){

int total = 0;
int average;
student *Sarr;
Sarr = new student[10];

for(int i = 0 ; i < 10; i ++){

    char n;
    int a;
    char s;
    int m;

    cin >> n;
    cin >> a;
    cin >> s;
    cin >> m;

    total += m;

    student Sarr[i] = student(n,a,s,m);
}

}
Fix off would like to point out that your code is leaking memory. It is very important to remember that whenever you do call new you NEED to have a matching delete call.

So at the end of int main() you should have this delete[] Sarr;.

Now the first problem you have is that in your student struct you declared a constructor which means that there is no longer a implicitly generated default constructor. So since we don't have a default constructor when we try to do new student[10]; it will fail because it is trying to call a default constructor which isn't there.

To fix this you need to define a default constructor for the student struct which assigns whatever values you think are good for the members in your struct.

For example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct student{

char name;
int age;
char stream;
int mark;

// When you declared this it deleted the default constructor
// that the compiler generates for you.
student( char n,  int a,  char s,  int m);

// Default constructor
student() : name(' '), age(0), stream(' '), mark(0) {}
};


Now on to your next problem. The problem is that you are treating the Sarr pointer as a actually array variable. It is not a array. It is a pointer to a student object within that array.

When you do

1
2
student *Sarr;
Sarr = new student[10];


the variable Sarr is pointing to the first element in a array full of 10 student objects.

So when you get into your for loop and try calling student Sarr[i] = student(n,a,s,m); you have multiple problems.

1. - student Sarr[i] is not valid code. You are telling the compiler that you wish to declare a new variable of type student but it is illegal to have [i].

To fix this get rid of the
student
(You need to get rid of the [i] also but I will cover that in a sec) so it looks like Sarr[i] = student(n,a,s,m);.

2. - Now that it looks like Sarr[i] = student(n,a,s,m); we need to do something else. Since Sarr is a pointer we can have random access so we need to get rid of the [i] so it looks like Sarr = student(n,a,s,m);. We also need to do one more thing to this statement. Since you declared the array dynamically you need to dynamically declare the object you wish to put in the array. So we need to do this instead Sarr = new student(n,a,s,m);.

3. - Now that we got rid of the random access we need to increment that pointer to the next element in the array. So right after Sarr = student(n,a,s,m); we need to do this.

1
2
Sarr = student(n,a,s,m);
++Sarr;


Now I was just going off your current code so I should point out there is much easier ways to do this like not dynamically declaring the array and other things. So you might want to explore those options also. Hope this is of some help.
Last edited on
Something like this might work:

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
#include <iostream>
#include <string>

using namespace std;

struct student {

string name;
int age;
string stream;
int mark;

};

int main(){

	int total = 0;
	student *Sarr;
	Sarr = new student[10];

	for(int i = 0 ; i < 10; i++)
	{
		cout << "Student " << i + 1 << " \n";

		cout << "\tEnter name: ";
		cin >> Sarr[i].name;
		cin.ignore(256, '\n');

		cout << "\tEnter age: ";
		cin >> Sarr[i].age;
		cin.ignore(256, '\n');

		cout << "\tEnter stream: ";
		cin >> Sarr[i].stream;
		cin.ignore(256, '\n');

		cout << "\tEnter mark: ";
		cin >> Sarr[i].mark;
		cin.ignore(256, '\n');
		total += Sarr[i].mark;

	}
	cin.ignore(256, '\n');
	delete [] Sarr;
	return EXIT_SUCCESS;

}
Last edited on
No that doesn't work and won't even compile...
Would you mind explaining the easier way?
Also, i would like to know why is it illegal to have [i] too, thanks
here's my modified version but i can't seem to be able to fill in 10 inputs.


#include <iostream>
#include <string>
using namespace std;

struct student{

char name;
int age;
char stream;
int mark;

student( char n, int a, char s, int m);
student() : name(' '), age(0), stream(' '), mark(0) {}
};

student::student( char n, int a, char s, int m){

name = n;
age = a;
stream = s;
mark = m;
}

int main(){

int total = 0;
int average;
student *Sarr;
Sarr = new student[10];

for(int i = 0 ; i < 10; i ++){

char n;
int a;
char s;
int m;

cin >> n;
cin >> a;
cin >> s;
cin >> m;

total += m;

Sarr = new student(n,a,s,m);
++Sarr;
}

average = total / 10;

cout << "Average mark: " << average << endl;

for( int k = 0; k < 10; k ++){

if (Sarr[k]. mark > average){
cout << Sarr[k].name << " " << Sarr[k].stream << endl;
}
}

}
In your first for loop, why are you using a function to set the data? You can use structure notation to access each component directly.

instead of

1
2
3
4
cin >> n;
cin >> a;
cin >> s;
cin >> m;


loop this ten times and read it directly into each structure...
total will track the total value of all of your marks

1
2
3
4
5
cin >> Sarr[i].name;
cin >> Sarr[i].age;
cin >> Sarr[i].stream;
cin >> Sarr[i].mark;
total += Sarr[i].mark;


Unless your name and stream variables are a single character, I don't think you mean to be using char variables. I recommend maybe strings.

@Codegazer: I just compiled and ran mine without error. It could be a platform incompatibility.

Also, here is an example without dynamically allocating the array

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>
#include <string>

using namespace std;

struct student {

string name;
int age;
string stream;
int mark;

};

int main(){

	int total = 0;
	student Sarr[10];

	for(int i = 0 ; i < 10; i++)
	{
		cout << "Student " << i + 1 << " \n";

		cout << "\tEnter name: ";
		cin >> Sarr[i].name;
		cin.ignore(256, '\n');

		cout << "\tEnter age: ";
		cin >> Sarr[i].age;
		cin.ignore(256, '\n');

		cout << "\tEnter stream: ";
		cin >> Sarr[i].stream;
		cin.ignore(256, '\n');

		cout << "\tEnter mark: ";
		cin >> Sarr[i].mark;
		cin.ignore(256, '\n');
		total += Sarr[i].mark;

	}
	cin.ignore(256, '\n');
	return EXIT_SUCCESS;

}


1>------ Build started: Project: forum3, Configuration: Debug Win32 ------
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
Topic archived. No new replies allowed.