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

Problem with dot notation

P: n/a
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?


Jul 19 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Acacia wrote:
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 <iostream> // no '.h' -- see the FAQ (link below) #include<string.h> // #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;

#define MAX 20 const unsigned int MAX = 20 // see the FAQ #define ARRAY 4 const unsigned int ARRAY = 4;

struct monkey {
char name[MAX]; // use std::string -- see the FAQ int age;
float weight;
};

void main()
{

struct monkey boy[ARRAY];
You could use a std::vector here -- again, see the FAQ
int index;

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

cout<<"MONKEY BOY "<<(index+1)<<" :"<<endl;
cout<<"Enter the name: ";
cin>>boy.name;
// 'boy' is of type 'struct monkey []', which is an array.
// You should have this instead:

cin >> boy[index].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?


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

- Adam

Jul 19 '05 #2

P: n/a

"Acacia" <ne*******@fatgerbil.co.uk> wrote in message news:bi*******************@news.demon.co.uk...
cin>>boy.name;


boy is an array, you have to specify a particular element
cin >> boy[index].name;
Jul 19 '05 #3

P: n/a
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


Jul 19 '05 #4

P: n/a

"Agent Mulder" wrote:
#include<iostream>
#define MAX 20

struct monkey
{
char name[MAX];
};
...
monkey boy[ARRAY];
...
std::cin>>boy[index].name;


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

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

HTH,
Patrick
Jul 19 '05 #5

P: n/a
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" <aB**************@KcIoNmGpSuter.org> wrote in message
news:GK*****************@news.uswest.net...
Acacia wrote:
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 <iostream> // no '.h' -- see the FAQ (link below)
#include<string.h>

// #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;

#define MAX 20

const unsigned int MAX = 20 // see the FAQ
#define ARRAY 4

const unsigned int ARRAY = 4;

struct monkey {
char name[MAX];

// use std::string -- see the FAQ
int age;
float weight;
};

void main()
{

struct monkey boy[ARRAY];


You could use a std::vector here -- again, see the FAQ
int index;

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

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


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

cin >> boy[index].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?


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

- Adam

Jul 19 '05 #6

P: n/a

"Acacia" <ne*******@fatgerbil.co.uk> wrote in message
news:bi*******************@news.demon.co.uk...
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.


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
Jul 19 '05 #7

P: n/a
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.........
Jul 19 '05 #8

P: n/a

"Acacia" <ne*******@fatgerbil.co.uk> wrote in message
news:bj*******************@news.demon.co.uk...
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.........


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
Jul 19 '05 #9

P: n/a

"John Harrison" <jo*************@hotmail.com> wrote in message
news:bj************@ID-196037.news.uni-berlin.de...

"Acacia" <ne*******@fatgerbil.co.uk> wrote in message
news:bj*******************@news.demon.co.uk...
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.........

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


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

john


Jul 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.