Array of structure Print

Guys i am trying to print an array of structure but all in vain. The error i am receiving is that no suitable conversion from student to student*. Can anybody help me?

#include<iostream>
#include<string.h>


using namespace std;

struct student
{
char department[20];
char course[10];
int Roll_number;
int yearofjoining;
char name[10];

};

void print( student s[3])
{
for(int j = 0; j<3; j++)
{
cout<<"The name of the student is "<< s[j].name<<endl;
cout<<"The name of the department is "<<s[j].department<<endl;
cout<<"The name of the course is "<<s[j].course<<endl;
cout<<"The Rollnumber of the student is "<<s[j].Roll_number<<endl;
cout<<"Year of joinging is "<<s[j].yearofjoining<<endl;
}
}

void main()
{
student *s = new student[3];
for(int i = 0; i<3; i++)
{
cout<<"\nEnter Name: ";
cin.getline(s[i].name ,10);

cout<<"\nEnter Department: ";
cin.getline(s[i].department, 20);

cout<<"\nEnter course: ";
cin.getline(s[i].course, 10);

cout<<"\nEnter Roll Number: ";
cin>>s[i].Roll_number;

cout<<"\nEnter Year of joining: ";
cin>>s[i].yearofjoining;

cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
print(s[3]);
delete [] s;


system("Pause");
}
Hi,
Please edit your post and put it in code tags
http://www.cplusplus.com/articles/jEywvCM9/

delete [] s; is not the correct syntax

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
#include<iostream>
#include<string.h>
#include<limits>//to get numeric_limits working


using namespace std;

struct student
{
char department[20];
char course[10];
int Roll_number;
int yearofjoining;
char name[10];

};

void print( student s[3])
{
for(int j = 0; j<3; j++)
{
cout<<"The name of the student is "<< s[j].name<<endl;
cout<<"The name of the department is "<<s[j].department<<endl;
cout<<"The name of the course is "<<s[j].course<<endl;
cout<<"The Rollnumber of the student is "<<s[j].Roll_number<<endl;
cout<<"Year of joinging is "<<s[j].yearofjoining<<endl;
}
}

int main()//void main is illegal for c++ nowdays
{
student *s = new student[3];
for(int i = 0; i<3; i++)
{
cout<<"\nEnter Name: ";
cin.getline(s[i].name ,10);

cout<<"\nEnter Department: ";
cin.getline(s[i].department, 20);

cout<<"\nEnter course: ";
cin.getline(s[i].course, 10);

cout<<"\nEnter Roll Number: ";
cin>>s[i].Roll_number;

cout<<"\nEnter Year of joining: ";
cin>>s[i].yearofjoining;

cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
print(s);//no need to mention arrays when using vector
delete [] s;


system("Pause");
return 0;//main should return some stuff
}


Hope it helps

thank you very much #gentleguy and #shadder. it worked.
(y)
sorry guys i am new here. Next time i will take care of the format.
Last edited on
Topic archived. No new replies allowed.