Problem with dot notation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Acacia

    Problem with dot notation

    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<iostre am.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<<"En ter the age: ";
    cin>>boy.age;
    cout<<endl<<"En ter the weight: ";
    cin>>boy.weight ;
    cout<<endl<<end l;

    }

    }

    Does anybody know what is wrong?




  • Adam Fineman

    #2
    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<iostre am.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<<"En ter the age: ";
    > cin>>boy.age;
    > cout<<endl<<"En ter the weight: ";
    > cin>>boy.weight ;
    > cout<<endl<<end l;
    >
    > }
    >
    > }
    >
    > Does anybody know what is wrong?
    >
    >[/color]

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

    - Adam

    Comment

    • Ron Natalie

      #3
      Re: Problem with dot notation


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

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


      Comment

      • Agent Mulder

        #4
        Re: Problem with dot notation

        Does anybody know what is wrong?

        #include<iostre am>
        #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<A RRAY;index++)
        {
        std::cout<<"\nM ONKEY BOY "<<(index+1 )<<" :";
        std::cout<<"\nE nter the name: ";
        std::cin>>boy[index].name;
        std::cout<<"\nE nter the age: ";
        std::cin>>boy[index].age;
        std::cout<<"\nE nter the weight: ";
        std::cin>>boy[index].weight;
        std::cout<<"\n\ n";
        }}

        -X




        Comment

        • Patrick Frankenberger

          #5
          Re: Problem with dot notation


          "Agent Mulder" wrote:[color=blue]
          > #include<iostre am>
          > #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


          Comment

          • Acacia

            #6
            Solution to dot notation array

            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" <aBfLiOnDeDmYaV nI@KcIoNmGpSute r.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<iostre am.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<<"En ter the age: ";
            > > cin>>boy.age;
            > > cout<<endl<<"En ter the weight: ";
            > > cin>>boy.weight ;
            > > cout<<endl<<end l;
            > >
            > > }
            > >
            > > }
            > >
            > > Does anybody know what is wrong?
            > >
            > >[/color]
            >
            > You really need to read the FAQ: http://parashift.com/c++-faq-lite/
            >
            > - Adam
            >[/color]


            Comment

            • John Harrison

              #7
              Re: Solution to dot notation array


              "Acacia" <newsgroup@fatg erbil.co.uk> wrote in message
              news:biirld$931 $1$8300dec7@new s.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


              Comment

              • Acacia

                #8
                Re: Solution to dot notation array

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


                Comment

                • John Harrison

                  #9
                  Re: Solution to dot notation array


                  "Acacia" <newsgroup@fatg erbil.co.uk> wrote in message
                  news:bj046j$d58 $1$8302bc10@new s.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


                  Comment

                  • John Harrison

                    #10
                    Re: Solution to dot notation array


                    "John Harrison" <john_andronicu s@hotmail.com> wrote in message
                    news:bj0698$e0i ef$1@ID-196037.news.uni-berlin.de...[color=blue]
                    >
                    > "Acacia" <newsgroup@fatg erbil.co.uk> wrote in message
                    > news:bj046j$d58 $1$8302bc10@new s.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




                    Comment

                    Working...