By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,282 Members | 1,548 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,282 IT Pros & Developers. It's quick & easy.

Need Help - Structure Required on Left Side

P: n/a
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[i].roll_no;
cout<<"\n\tEnter Name = ";gets(st[i].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[i].roll_no;
cout<<"\n\tName = ";puts(st[i].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;
}
}

Aug 12 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
> 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[i].roll_no;
cout<<"\n\tEnter Name = ";gets(st[i].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[i].roll_no;
cout<<"\n\tName = ";puts(st[i].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
Aug 12 '05 #2

P: n/a
imranzafar wrote:
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[i].roll_no;
cout<<"\n\tEnter Name = ";gets(st[i].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[i].roll_no;
cout<<"\n\tName = ";puts(st[i].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
Aug 12 '05 #3

P: n/a
> void set (void)
{
for (int i=0; i<=3-1; i++)
{
cout<<"\n\tEnter Roll No. = ";cin>>st[i].roll_no;
cout<<"\n\tEnter Name = ";gets(st[i].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[i].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[i].roll_no;
cout<<"\n\tEnter Name = ";gets(st[i].name);

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

}
}

Srini

Aug 12 '05 #4

P: n/a
imranzafar wrote:

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[i].roll_no;
cout<<"\n\tEnter Name = ";gets(st[i].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[i].roll_no;
cout<<"\n\tEnter Name = ";gets(st[i].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.

--
Karl Heinz Buchegger
kb******@gascad.at
Aug 12 '05 #5

P: n/a
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
Aug 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.