Problem with dot notation

A

Acacia

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?
 
A

Adam Fineman

Acacia said:
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 said:
#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
 
A

Agent Mulder

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
 
P

Patrick Frankenberger

Agent Mulder said:
#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
 
A

Acacia

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 said:
Acacia said:
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 said:
#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
 
J

John Harrison

Acacia said:
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
 
A

Acacia

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.........
 
J

John Harrison

Acacia said:
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
 
J

John Harrison

John Harrison said:
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
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top