Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem with dot notation

Acacia
Guest
 
Posts: n/a
#1: Jul 19 '05
i get this error on any line with dot notation. Oh, and do I need to use
cin.getline(boy.name)?:

error:'.':left operand points to 'struct' use '->'

This is the program in question:

#include<iostream.h>
#include<string.h>

#define MAX 20
#define ARRAY 4

struct monkey {
char name[MAX];
int age;
float weight;
};

void main()
{

struct monkey boy[ARRAY];
int index;

for(index=0; index<ARRAY; index++)
{

cout<<"MONKEY BOY "<<(index+1)<<" :"<<endl;
cout<<"Enter the name: ";
cin>>boy.name;
cout<<endl<<"Enter the age: ";
cin>>boy.age;
cout<<endl<<"Enter the weight: ";
cin>>boy.weight;
cout<<endl<<endl;

}

}

Does anybody know what is wrong?





Adam Fineman
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Problem with dot notation


Acacia wrote:[color=blue]
> i get this error on any line with dot notation. Oh, and do I need to use
> cin.getline(boy.name)?:
>
> error:'.':left operand points to 'struct' use '->'
>
> This is the program in question:
>
> #include<iostream.h>[/color]
#include <iostream> // no '.h' -- see the FAQ (link below)[color=blue]
> #include<string.h>[/color]
// #include <string> or <cstring>... hard to tell which you need,
// because you're not actually using either.

using std::cout; // or, just 'using namespace std;
using std::end;
using std::cin;
[color=blue]
>
> #define MAX 20[/color]
const unsigned int MAX = 20 // see the FAQ[color=blue]
> #define ARRAY 4[/color]
const unsigned int ARRAY = 4;
[color=blue]
>
> struct monkey {
> char name[MAX];[/color]
// use std::string -- see the FAQ[color=blue]
> int age;
> float weight;
> };
>
> void main()
> {
>
> struct monkey boy[ARRAY];[/color]

You could use a std::vector here -- again, see the FAQ
[color=blue]
> int index;
>
> for(index=0; index<ARRAY; index++)
> {
>
> cout<<"MONKEY BOY "<<(index+1)<<" :"<<endl;
> cout<<"Enter the name: ";
> cin>>boy.name;[/color]

// 'boy' is of type 'struct monkey []', which is an array.
// You should have this instead:

cin >> boy[index].name
[color=blue]
> cout<<endl<<"Enter the age: ";
> cin>>boy.age;
> cout<<endl<<"Enter the weight: ";
> cin>>boy.weight;
> cout<<endl<<endl;
>
> }
>
> }
>
> Does anybody know what is wrong?
>
>[/color]

You really need to read the FAQ: http://parashift.com/c++-faq-lite/

- Adam

Ron Natalie
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Problem with dot notation



"Acacia" <newsgroup@fatgerbil.co.uk> wrote in message news:biggbj$jqc$1$8302bc10@news.demon.co.uk...
[color=blue]
> cin>>boy.name;[/color]

boy is an array, you have to specify a particular element
cin >> boy[index].name;


Agent Mulder
Guest
 
Posts: n/a
#4: Jul 19 '05

re: Problem with dot notation


Does anybody know what is wrong?

#include<iostream>
#include<string>
#define MAX 20
#define ARRAY 4
struct monkey
{
char name[MAX];
int age;
float weight;
};
int main(int,char**)
{
monkey boy[ARRAY];
for(int index=0;index<ARRAY;index++)
{
std::cout<<"\nMONKEY BOY "<<(index+1)<<" :";
std::cout<<"\nEnter the name: ";
std::cin>>boy[index].name;
std::cout<<"\nEnter the age: ";
std::cin>>boy[index].age;
std::cout<<"\nEnter the weight: ";
std::cin>>boy[index].weight;
std::cout<<"\n\n";
}}

-X




Patrick Frankenberger
Guest
 
Posts: n/a
#5: Jul 19 '05

re: Problem with dot notation



"Agent Mulder" wrote:[color=blue]
> #include<iostream>
> #define MAX 20
>
> struct monkey
> {
> char name[MAX];
> };
> ...
> monkey boy[ARRAY];
> ...
> std::cin>>boy[index].name;[/color]

cin.operator>>(char*) is unsafe.

Use std::string or
std::cin >> std::setw(MAX) >> boy[index].name;

HTH,
Patrick


Acacia
Guest
 
Posts: n/a
#6: Jul 19 '05

re: Problem with dot notation


I think it was the array that did it. After posting i tried

boy.name[index]

after realising i hadn't stated with element of the array to use, which
didn't work. I have just tried with

boy[index].name

and it works. i don't really understand most of what you said, but I looked
at the FAQ and it confuses me as it is very different from the book I am
learning basic c++ from (a complete idiots guide to c++ (Paul Snaith)) but
the array thing has fixed my problem. I will now attempt to use functions to
achieve this same outcome, then save the information in a binary file (no,
don't help me with this yet - i need to work it out). Also, emitting the

..h

from

iostream.h

returns an arror. I am using MSVC (v1.52).

Thanks for your help.

"Adam Fineman" <aBfLiOnDeDmYaVnI@KcIoNmGpSuter.org> wrote in message
news:GKP2b.196$Hi2.35392@news.uswest.net...[color=blue]
> Acacia wrote:[color=green]
> > i get this error on any line with dot notation. Oh, and do I need to use
> > cin.getline(boy.name)?:
> >
> > error:'.':left operand points to 'struct' use '->'
> >
> > This is the program in question:
> >
> > #include<iostream.h>[/color]
> #include <iostream> // no '.h' -- see the FAQ (link below)[color=green]
> > #include<string.h>[/color]
> // #include <string> or <cstring>... hard to tell which you need,
> // because you're not actually using either.
>
> using std::cout; // or, just 'using namespace std;
> using std::end;
> using std::cin;
>[color=green]
> >
> > #define MAX 20[/color]
> const unsigned int MAX = 20 // see the FAQ[color=green]
> > #define ARRAY 4[/color]
> const unsigned int ARRAY = 4;
>[color=green]
> >
> > struct monkey {
> > char name[MAX];[/color]
> // use std::string -- see the FAQ[color=green]
> > int age;
> > float weight;
> > };
> >
> > void main()
> > {
> >
> > struct monkey boy[ARRAY];[/color]
>
> You could use a std::vector here -- again, see the FAQ
>[color=green]
> > int index;
> >
> > for(index=0; index<ARRAY; index++)
> > {
> >
> > cout<<"MONKEY BOY "<<(index+1)<<" :"<<endl;
> > cout<<"Enter the name: ";
> > cin>>boy.name;[/color]
>
> // 'boy' is of type 'struct monkey []', which is an array.
> // You should have this instead:
>
> cin >> boy[index].name
>[color=green]
> > cout<<endl<<"Enter the age: ";
> > cin>>boy.age;
> > cout<<endl<<"Enter the weight: ";
> > cin>>boy.weight;
> > cout<<endl<<endl;
> >
> > }
> >
> > }
> >
> > Does anybody know what is wrong?
> >
> >[/color]
>
> You really need to read the FAQ: http://parashift.com/c++-faq-lite/
>
> - Adam
>[/color]


John Harrison
Guest
 
Posts: n/a
#7: Jul 19 '05

re: Problem with dot notation



"Acacia" <newsgroup@fatgerbil.co.uk> wrote in message
news:biirld$931$1$8300dec7@news.demon.co.uk...[color=blue]
> I think it was the array that did it. After posting i tried
>
> boy.name[index]
>
> after realising i hadn't stated with element of the array to use, which
> didn't work. I have just tried with
>
> boy[index].name
>
> and it works. i don't really understand most of what you said, but I[/color]
looked[color=blue]
> at the FAQ and it confuses me as it is very different from the book I am
> learning basic c++ from (a complete idiots guide to c++ (Paul Snaith)) but
> the array thing has fixed my problem. I will now attempt to use functions[/color]
to[color=blue]
> achieve this same outcome, then save the information in a binary file (no,
> don't help me with this yet - i need to work it out). Also, emitting the
>
> .h
>
> from
>
> iostream.h
>
> returns an arror. I am using MSVC (v1.52).
>
> Thanks for your help.
>[/color]

One of the hazards for newbies posting to c.l.c++ is that they can get the
answers to a whole load of questions they didn't ask. Its ironic that the
compiler you are using is so old that much of the advice was impossible for
you to follow anyway. If it is possible you should think about upgrading
your compiler, the language you will learn by using VC++ 1.52 is somewhat
different from true C++.

I'm glad you figured it out for yourself, best way to learn.

john


Acacia
Guest
 
Posts: n/a
#8: Jul 19 '05

re: Problem with dot notation


Where can I get hold of a newer compiler? I would like to learn C++ - but
upon visting this newsgroup it seems far more complex than I previously
thought.........


John Harrison
Guest
 
Posts: n/a
#9: Jul 19 '05

re: Problem with dot notation



"Acacia" <newsgroup@fatgerbil.co.uk> wrote in message
news:bj046j$d58$1$8302bc10@news.demon.co.uk...[color=blue]
> Where can I get hold of a newer compiler? I would like to learn C++ - but
> upon visting this newsgroup it seems far more complex than I previously
> thought.........
>[/color]

Well you can buy one. Microsoft do a learning edition of their compiler for
a moderate price. Easy to use, nice IDE.

If you want a free one, then get hold of gcc, from http://gcc.gnu.org/. Just
as good a compiler (better probably) but without the slick interface.

john


John Harrison
Guest
 
Posts: n/a
#10: Jul 19 '05

re: Problem with dot notation



"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:bj0698$e0ief$1@ID-196037.news.uni-berlin.de...[color=blue]
>
> "Acacia" <newsgroup@fatgerbil.co.uk> wrote in message
> news:bj046j$d58$1$8302bc10@news.demon.co.uk...[color=green]
> > Where can I get hold of a newer compiler? I would like to learn C++ -[/color][/color]
but[color=blue][color=green]
> > upon visting this newsgroup it seems far more complex than I previously
> > thought.........
> >[/color]
>
> Well you can buy one. Microsoft do a learning edition of their compiler[/color]
for[color=blue]
> a moderate price. Easy to use, nice IDE.
>
> If you want a free one, then get hold of gcc, from http://gcc.gnu.org/.[/color]
Just[color=blue]
> as good a compiler (better probably) but without the slick interface.
>
> john
>[/color]

Actually gcc for Windows is probably better obtained from here
http://www.cygwin.com.

john




Closed Thread