Need Help - Structure Required on Left Side

I

imranzafar

Hi! I am a beginner in C++. This is little assignment. It gives an
error "Structure Require on Left Side of dot operator" Can anybody help
me what went wrong in this code?

#include <conio.h>
#include <stdio.h>
#include <iostream.h>

void set(void);
void display(void);

struct subject
{
char sub_id[50];
int credit;
};
struct student
{
int roll_no;
char name[50];
subject sub[3];
};
student st[3];

void main (void)
{
clrscr();

set();
display();
getch();
}


void set (void)
{
for (int i=0; i<=3-1; i++)
{
cout<<"\n\tEnter Roll No. = ";cin>>st.roll_no;
cout<<"\n\tEnter Name = ";gets(st.name);

for (int j=0; j<=3-1; j++)

cout<<"\n\tEnter Subject: "<<j+1<<" = "; gets(st.sub[j].sub_id);
cout<<"\n\tCredit ";cin>>(st.sub[j]).credit;
}
}

void display (void)
{
for (int i=0; i<=3-1; i++)
{
cout<<"\n\t Roll No. = "<<st.roll_no;
cout<<"\n\tName = ";puts(st.name);


for (int j=0; j<=3-1; j++)

cout<<"\n\t Subject: "<<j+1<<" = ";puts((st.sub[j]).sub_id);
cout<<"\n\tCredit "<<(st.sub[j]).credit;
}
}
 
B

benben

Hi! I am a beginner in C++. This is little assignment. It gives an
error "Structure Require on Left Side of dot operator" Can anybody help
me what went wrong in this code?

#include <conio.h>
#include <stdio.h>
#include <iostream.h>

void set(void);
void display(void);

struct subject
{
char sub_id[50];
int credit;
};
struct student
{
int roll_no;
char name[50];
subject sub[3];
};
student st[3];

void main (void)
{
clrscr();

set();
display();
getch();
}


void set (void)
{
for (int i=0; i<=3-1; i++)
{
cout<<"\n\tEnter Roll No. = ";cin>>st.roll_no;
cout<<"\n\tEnter Name = ";gets(st.name);

for (int j=0; j<=3-1; j++)

cout<<"\n\tEnter Subject: "<<j+1<<" = "; gets(st.sub[j].sub_id);
cout<<"\n\tCredit ";cin>>(st.sub[j]).credit;
}
}

void display (void)
{
for (int i=0; i<=3-1; i++)
{
cout<<"\n\t Roll No. = "<<st.roll_no;
cout<<"\n\tName = ";puts(st.name);


for (int j=0; j<=3-1; j++)

cout<<"\n\t Subject: "<<j+1<<" = ";puts((st.sub[j]).sub_id);
cout<<"\n\tCredit "<<(st.sub[j]).credit;
}
}


If you can comment the line of the problem I will consider helping it a
bit...

Ben
 
J

John Ratliff

imranzafar said:
Hi! I am a beginner in C++. This is little assignment. It gives an
error "Structure Require on Left Side of dot operator" Can anybody help
me what went wrong in this code?

#include <conio.h>
#include <stdio.h>
#include <iostream.h>

Okay, for starters, conio.h had nothing to do with c++ and is completely
non-standard.

stdio.h and iostream.h are pre-ANSI/ISO C++. They should be
<cstdio> and <iostream> respectively. Looks like a C program except you
chose to use cout. Maybe you should consider printf instead and just
make it a C program.
void set(void);
void display(void);

struct subject
{
char sub_id[50];
int credit;
};
struct student
{
int roll_no;
char name[50];
subject sub[3];
};
student st[3];

void main (void)
{
clrscr();

set();
display();
getch();
}


void set (void)
{
for (int i=0; i<=3-1; i++)
{
cout<<"\n\tEnter Roll No. = ";cin>>st.roll_no;
cout<<"\n\tEnter Name = ";gets(st.name);

for (int j=0; j<=3-1; j++)

cout<<"\n\tEnter Subject: "<<j+1<<" = "; gets(st.sub[j].sub_id);
cout<<"\n\tCredit ";cin>>(st.sub[j]).credit;
}
}

void display (void)
{
for (int i=0; i<=3-1; i++)
{
cout<<"\n\t Roll No. = "<<st.roll_no;
cout<<"\n\tName = ";puts(st.name);


for (int j=0; j<=3-1; j++)

cout<<"\n\t Subject: "<<j+1<<" = ";puts((st.sub[j]).sub_id);
cout<<"\n\tCredit "<<(st.sub[j]).credit;
}
}


All those cout should be std::cout or else you'll need a using
declaration at the top (using std::cout).

That's a whole lotta code. Since I don't have conio, I can't compile
this. Care to tell me where the compiler tells you the problem actually
exists? Not really in the mood to hand trace this.

--John Ratliff
 
S

Srini

void set (void)
{
for (int i=0; i<=3-1; i++)
{
cout<<"\n\tEnter Roll No. = ";cin>>st.roll_no;
cout<<"\n\tEnter Name = ";gets(st.name);

for (int j=0; j<=3-1; j++)

cout<<"\n\tEnter Subject: "<<j+1<<" = "; gets(st.sub[j].sub_id);
cout<<"\n\tCredit ";cin>>(st.sub[j]).credit;
}

}


I think the problem is in this function and also a similar problem in
the display function. The inner loop does not have paranthesis and
you've used "st.sub[j]" where 'st' is an array. It must be
"st.sub[j]" - try the following and see if it resolves your error.

void set (void)
{
for (int i=0; i<=3-1; i++)
{
cout<<"\n\tEnter Roll No. = ";
cin>>st.roll_no;
cout<<"\n\tEnter Name = ";gets(st.name);

for (int j=0; j<=3-1; j++)
{
cout<<"\n\tEnter Subject: "<<j+1<<" = ";
gets(st.sub[j].sub_id);
cout<<"\n\tCredit ";cin>>(st.sub[j]).credit;
}

}
}

Srini
 
K

Karl Heinz Buchegger

imranzafar said:
Hi! I am a beginner in C++. This is little assignment. It gives an
error "Structure Require on Left Side of dot operator" Can anybody help
me what went wrong in this code?

Before I show you your problem a few remarks:

* work on your indentation style
at the moment you have none

it is usually accepted that each '{' opens a new scope
and thus needs an indentation. Where you put the '{' exactly
isn't that much important, but make sure that you visually can
see the block vom 10 feet distance:
for (int i=0; i<=3-1; i++)
{
cout<<"\n\tEnter Roll No. = ";cin>>st.roll_no;
cout<<"\n\tEnter Name = ";gets(st.name);

for (int j=0; j<=3-1; j++)

cout<<"\n\tEnter Subject: "<<j+1<<" = "; gets(st.sub[j].sub_id);
cout<<"\n\tCredit ";cin>>(st.sub[j]).credit;
}


should be formatted like this:

for (int i=0; i<=3-1; i++)
{
cout<<"\n\tEnter Roll No. = ";cin>>st.roll_no;
cout<<"\n\tEnter Name = ";gets(st.name);

for (int j=0; j<=3-1; j++)
cout<<"\n\tEnter Subject: "<<j+1<<" = "; gets(st.sub[j].sub_id);

cout<<"\n\tCredit ";cin>>(st.sub[j]).credit;
}

Here it is easy to see where each block starts and where it ends and which
statements are in the block.

* Use common progamming idioms

for( int j = 0; j <= 3-1; i++ )

is not the way a C++ code would code a for loop.

for( int j = 0; j < 3; i++ ) // (or ++i )

would be the usual way a C++ programmer looks at a for loop. It is so common
that he doesn't even need to think to figure out that this loop runs through
the values 0, 1, 2

But using something other then '<' in the loop control section of a for loop
lets alarm bell ring in our heads. One then has to analyze what the for loop
does.

* Don't write more then one statement in one line.
This way compiler error messages get much more meaningful. The compiler
tells you the line number, your editor shows you the liner number and
you can start working on that error instead of anaylizing the whole line, which
statement is responsible for the error.
Fixing errors can be time consuming. So make yourself life as easy as it can be.

Braking your lines into 2, the compiler tells me that in
gets(st.sub[j].sub_id);

there is somehting wrong.
Now can you figure it out on your own?
Hint: What data type is 'st'. Is it a struture or is it an array?

* Never, and I repeat *never* use gets().
Always use fgets().
gets() is an unsafe function as it is possible for the one entering input
to gets() to simply overrun the buffer supplied by your program. gets() has
no way to protect against this, since it doesn't know, how big that buffer is.
 
D

Default User

John Ratliff wrote:

stdio.h and iostream.h are pre-ANSI/ISO C++. They should be
<cstdio> and <iostream> respectively.

True for <iostream.h>, not true for <stdio.h>. The latter is perfectly
standard, although deprecated, and will probably be perfectly standard
for many moons to come.




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top