473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Need Help - Structure Required on Left Side

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
5 3504
> 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
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
> 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Vijay Kumar R Zanvar | last post by:
Hi, Have a look at the following program: //++ /* struct.cpp */ #include <stdio.h> struct { int a;
1
by: Ben Nguyen | last post by:
Im trying to port a hard drive driver that was originally for a different compiler than the one Im using now. The line that doesnt compile with the new compiler is: ActualPartRecord =...
4
by: Garry Jones | last post by:
I have recently constructed a website using a lot of php script (self taught). I now wonder if I should have construted the site in a different way. The page contains a header (a.php) and left...
2
by: wipit | last post by:
I need to process a HTML form in python. I'm using urllib2 and HTMLParser to handle the html. There are several steps I need to take to get to the specific page on the relevant site the first of...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
0
by: dinsdale | last post by:
Wow, this is a really embarassing thing to have to ask about... I have an asp.net page with a multiview control in it. We want to enter person info in a semi - wizard style and I wanted to use...
5
by: Amit_Basnak | last post by:
Dear Friends I have been getting the following error Error 185: "WorkFlow_dce.cpp", line 58 # Left side of '->' requires a pointer to class; type found was 'struct WF_SEARCH_WU'....
12
by: drsmooth | last post by:
i am working with a vector of pointers to Job class objects(job class is one that i defined) when a person in my game dies, a pointer to the job he once had is added to a vector to track jobs that...
14
by: deepak | last post by:
Hi Experts, I'm getting this compilation error while trying to access a member in structure. at what time we will get this error message? Thanks, Deepak
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.