please help new to c++ programming
in this program am trying to input 5 students information and get there output
but i got wired error {illegal structure operation}
please help

#include <iostream.h>
#include <conio.h>
struct student
{
int no;
char name[20];
float degree;
};
void main()
{
student s;
int i;
for (i=0;i<5;i++)
{
cout<<"\nEnter student number: ";
cin>>s.no;
cout<<"\nEnter student name: ";
cin>>s.name;
cout<<"\nEnter student degree: ";
cin>>s.degree;
}
cout<<"\n\nThe elements are: ";

for (i=0;i<5;i++)
cout<<s.name<<" "<<s.name<<" "<<s.degree<<endl;

getch();
}

Recommended Answers

All 4 Replies

please post code in the code tags [code] /* code here */ [/code].

Then, it seems you're using a highly outdated compiler that supports a non-standard version of C++, perhaps you would wish to upgrade to a newer compiler so that we (and others) can better help you?

There are a number of freely available up-to-date compilers for many platforms, please look into it.

Also, please see this thread with regard to using

void main()

:
http://www.daniweb.com/software-development/cpp/threads/403189

I am wondering under Linux what would 'echo $?' give you back, after you run a program with no return value.

My attempt to make such a program have failed :
void.cpp:3:12: error: ‘::main’ must return ‘int’. - I am still curious though.


@manfredbill - The problem is you are trying to use the index operator on a non-array like object.
In your main you declared one student structure called 's'. You're using the index operator, but 's' is not an array, it is a single type of data. It's like saying:

int x = 124;
x[2] = 0; // illegal use of operator []

That's why you got the error message. You can fix it by making an array of your structure.

student[5] array_of_structure_student;

Now you can store more student information.

I am wondering under Linux what would 'echo $?' give you back, after you run a program with no return value.

My attempt to make such a program have failed :
void.cpp:3:12: error: ‘::main’ must return ‘int’. - I am still curious though.


@manfredbill - The problem is you are trying to use the index operator on a non-array like object.
In your main you declared one student structure called 's'. You're using the index operator, but 's' is not an array, it is a single type of data. It's like saying:

int x = 124;
x[2] = 0; // illegal use of operator []

That's why you got the error message. You can fix it by making an array of your structure.

student[5] array_of_structure_student;

Now you can store more student information.

Actually Lurr, I'm not aware you can use the java-style brackets in that fashion with C++.

try: Student arrayOfStructureStudent[5];

Yes, thank you. Sorry about that, when I re-edited the post I screwed it up.

student array_of_structure_student[5]; //corrected
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.