help regarding struct in C++

Status
Not open for further replies.

Virus King

Member level 2
Joined
Feb 26, 2007
Messages
48
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,288
Location
JaMaN RoAd BaNnU
Activity points
1,602
all of us know that by using int,float,char before main(), we have to use return 0; in the end of program. as shown below



int main()
{


return 0;
}


as we know that int float char are abstract data types.
and struct is composite data type, so by using my composite data type why turbo c compiler gives us error as shown below

struct student
{
int class;
float gpa;
};


student main()
{

student s1;
cout<<"plz enter class and gpa";
cin>>s1.gpa>>s1.class;
return 0;
}
 

AA,
Please attach the compiler message.
Regards,
amraldo
 

the error genrated by compiler is

cannot convert int to student datatype
bcoz int is abstract data type and student is composite data type.
 

AA,
try to modify the line student main to struct student main , in addition to modifying the return statement to return a variable of type sturct student.
Keep me updated.
Regards,
Amraldo.
 

Hi,
This works for me: (compiles and links without error or warning)

Code:
struct student
{
int StudentClass;
float gpa;
};

student main(int argc, char* argv[])
{
	student Me;               // creates a student-structure
	Me.StudentClass=1;  // assigns the int value 0 to the 1st part of structure
	Me.gpa=9.99f;          // assigns the float value 9.99 to the 2nd part of structure
	return Me;                // let main end with returning (with the values of)
                                       // the student-structure Me.
}

The warning you recieve is because in your code (return 0 you tell the compiler to return 0 while you specify also that the return type should be of the struct student.
Advise : stay away from using reserved names like 'class' for variables, especially in c++.

Success with your code.
Jeroen
 

    Virus King

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…