classes and using *

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

    #16
    Re: classes and using *

    Developwebsites wrote:[color=blue]
    >[color=green]
    > >Why do you refuse to study the language but
    > >keep wasting our time with questions that have already been dealt with?
    > >
    > >[/color]
    >
    > why dont you read the 2nd part of my question which was how to use arrays to
    > store several cities using this code.[/color]



    Why are you using arrays?




    Brian Rodenborn

    Comment

    • Developwebsites

      #17
      Re: classes and using *

      >* one for a single city[color=blue]
      > every city has a name
      >
      >* the second for the collection of cities
      > (an array of *cities*, not an array of strings or an array of characters)[/color]

      totally confused.
      please be more specific.
      all i want to do is enter several city names and store them and have them
      printed out using a for loop.

      --------------------------------------------------
      remove *batSPAM* to e-mail me
      --------------------------------------------------

      Comment

      • Developwebsites

        #18
        Re: classes and using *

        >[color=blue]
        >
        >Why are you using arrays?[/color]

        because thats what we covered in class, not vectors, or something else.
        I could use pointers though.
        --------------------------------------------------
        remove *batSPAM* to e-mail me
        --------------------------------------------------

        Comment

        • WW

          #19
          Re: classes and using *

          Developwebsites wrote:[color=blue][color=green]
          >> Why are you using arrays?[/color]
          >
          > because thats what we covered in class, not vectors, or something
          > else. I could use pointers though.[/color]

          And capitals in the beginning of sentences...

          --
          WW aka Attila


          Comment

          • Default User

            #20
            Re: classes and using *

            Developwebsites wrote:[color=blue]
            >[color=green]
            > >
            > >
            > >Why are you using arrays?[/color]
            >
            > because thats what we covered in class, not vectors, or something else.
            > I could use pointers though.[/color]


            Are you doing a school assignment? Nothing in your post indicated that.



            Brian Rodenborn

            Comment

            • Developwebsites

              #21
              Re: classes and using *

              >[color=blue]
              >Are you doing a school assignment? Nothing in your post indicated that.[/color]

              no i am doing top secret work for the CIA.
              --------------------------------------------------
              remove *batSPAM* to e-mail me
              --------------------------------------------------

              Comment

              • Developwebsites

                #22
                Re: classes and using *

                /*
                I am trying to input names of 5 cities in a for loop, store them in an array,
                (not a vector as we havent got there yet), and then print them out.
                is this the correct way of doing it:
                */

                #include<iostre am>
                #include<iomani p>
                #include<string >
                using namespace std;

                class Cities {
                private:
                string *city;

                public:
                Cities();
                void input();
                void output();
                };

                Cities:: Cities()
                {
                }

                void Cities::input()
                {
                int total=0;
                do{
                cout<<"\n";
                cout<<"Please enter name of city: ";
                cin>>city[total];
                total++;
                }while(total<5) ;
                cout<<"\n\n";
                }

                void Cities::output( )
                {
                cout<<"\n";
                cout<<"name of city"
                <<"\n";
                for(int t=0;t<5;t++)
                {
                cout<<setw(5)<< city[t]<<"\n";
                }
                }//close output

                int main()
                {
                Cities info;

                info.input();
                info.output();
                return 0;
                }


                --------------------------------------------------
                remove *batSPAM* to e-mail me
                --------------------------------------------------

                Comment

                • Jonathan Mcdougall

                  #23
                  Re: classes and using *

                  > /*[color=blue]
                  > I am trying to input names of 5 cities in a for loop, store them in an[/color]
                  array,[color=blue]
                  > (not a vector as we havent got there yet), and then print them out.
                  > is this the correct way of doing it:
                  > */[/color]

                  Do not multipost. See my answer in alt.comp.lang.l earn.c-c++ (that sure is
                  a long name).


                  Jonathan


                  Comment

                  • Karl Heinz Buchegger

                    #24
                    Re: classes and using *



                    Developwebsites wrote:[color=blue]
                    >[color=green]
                    > >* one for a single city
                    > > every city has a name
                    > >
                    > >* the second for the collection of cities
                    > > (an array of *cities*, not an array of strings or an array of characters)[/color]
                    >
                    > totally confused.
                    > please be more specific.
                    > all i want to do is enter several city names and store them and have them
                    > printed out using a for loop.[/color]

                    AS I said:

                    class City
                    {
                    public:

                    private:
                    string name;
                    };

                    class Cities
                    {
                    public:

                    private:
                    vector< City > AllCities;
                    };

                    There is one class for a single city.
                    And then there is another class which holds
                    all those cities.


                    You say:[color=blue]
                    > all i want to do is enter several city names and store them and have them
                    > printed out using a for loop.[/color]

                    Fine. But:
                    a city name is a property of a single city. Thus you don't want to store
                    names, you want to store cities. In the near future you might want to
                    expand your example to: a city has a name *and* an inhabitants number.
                    Then you are prepared: just add the number of inhabitants to a single
                    city:

                    class City
                    {
                    public:

                    private:
                    string name;
                    long inhabitants;
                    };

                    No need to fiddle with the vector (array) in Cities. Ciities still holds
                    multiple cities, but a city now contains more information.

                    --
                    Karl Heinz Buchegger
                    kbuchegg@gascad .at

                    Comment

                    • Karl Heinz Buchegger

                      #25
                      Re: classes and using *



                      Developwebsites wrote:[color=blue]
                      >
                      > /*
                      > I am trying to input names of 5 cities in a for loop, store them in an array,
                      > (not a vector as we havent got there yet), and then print them out.
                      > is this the correct way of doing it:
                      > */
                      >
                      > #include<iostre am>
                      > #include<iomani p>
                      > #include<string >
                      > using namespace std;
                      >
                      > class Cities {
                      > private:
                      > string *city;
                      >[/color]

                      That's not an array. It is a pointer to a string. But
                      at the moment this pointer doesn't even point to 1 string,
                      it points to nowehere.

                      If you want an array, you have to create one:

                      string city[5]; // array to store 5 cities

                      better would be to use a vector

                      vector<string> city;

                      becuase you don't have to keep track about how many city names are stored
                      by yourself any longer. Also: vector does the dynamic memory management for you.


                      --
                      Karl Heinz Buchegger
                      kbuchegg@gascad .at

                      Comment

                      • Default User

                        #26
                        Re: classes and using *

                        Developwebsites wrote:[color=blue]
                        >[color=green]
                        > >
                        > >Are you doing a school assignment? Nothing in your post indicated that.[/color]
                        >
                        > no i am doing top secret work for the CIA.[/color]


                        Overdue, but:


                        *plonk*




                        Brian Rodenborn

                        Comment

                        • Developwebsites

                          #27
                          Re: classes and using *

                          >[color=blue]
                          >Overdue, but:
                          >
                          >*plonk*
                          >
                          >
                          >Brian Rodenborn[/color]

                          is the only answer you will get from people like him.
                          *plonk* right back at you shemp lover.


                          --------------------------------------------------
                          remove *batSPAM* to e-mail me
                          --------------------------------------------------

                          Comment

                          • Developwebsites

                            #28
                            Re: classes and using *

                            >If you want an array, you have to create one:[color=blue]
                            >
                            > string city[5]; // array to store 5 cities
                            >[/color]

                            what if the user enters more than 5 cities?
                            number of cities is unknown, and is set at 0, so string *city seemed to solve
                            the problem, compiles fine, but yes it does crash.
                            [color=blue]
                            >better would be to use a vector
                            >
                            > vector<string> city;
                            >[/color]

                            as I've said we did not cover vector and until we do i am not going to use it.


                            --------------------------------------------------
                            remove *batSPAM* to e-mail me
                            --------------------------------------------------

                            Comment

                            • Kevin Goodsell

                              #29
                              Re: classes and using *

                              Developwebsites wrote:[color=blue][color=green]
                              >>If you want an array, you have to create one:
                              >>
                              >> string city[5]; // array to store 5 cities
                              >>[/color]
                              >
                              >
                              > what if the user enters more than 5 cities?[/color]

                              What if? What if you expand the array to 5 billion and they enter 5
                              billion and one? This is a limitation of arrays. If you use arrays, you
                              have to deal with it.
                              [color=blue]
                              > number of cities is unknown, and is set at 0, so string *city seemed to solve
                              > the problem, compiles fine, but yes it does crash.[/color]

                              string *city is far worse than string city[5] in that there is NO SPACE
                              for storing the cities. At least with the array you had room for 5 of
                              them - with the pointer you don't have room for any. OF COURSE it
                              crashes when you try to store data through a wild pointer. What did you
                              expect?

                              Pointers aren't magical things that point to as much space as you want
                              or need. They point to exactly what you tell them too, no more, no less.
                              If you don't tell them to point to anything, they don't. If you try to
                              access the "thing" that an uninitialized pointer points to, the
                              program's behavior is undefined and it will probably crash (on most
                              modern desktop systems).

                              If you are going to use a pointer to string, you first have to make it
                              point to the strings you want to use. You could do this:

                              string *cities;
                              string some_cities[20];
                              cities = some_cities;

                              But obviously you'd be better off just using

                              string cities[20];

                              The other alternative is to determine the number of cities at run time:

                              int num_cities = GetNumberOfCiti es();
                              string *cities = new string[num_cities];

                              But, you'd be better off using a vector, which manages the dynamic array
                              for you.
                              [color=blue]
                              > as I've said we did not cover vector and until we do i am not going to use it.[/color]

                              You can do it however you want. We're just telling you what would be best.

                              -Kevin
                              --
                              My email address is valid, but changes periodically.
                              To contact me please use the address from a recent posting.

                              Comment

                              • Kevin Goodsell

                                #30
                                Re: classes and using *

                                Developwebsites wrote:
                                [color=blue][color=green]
                                >>Overdue, but:
                                >>
                                >>*plonk*
                                >>
                                >>
                                >>Brian Rodenborn[/color]
                                >
                                >
                                > is the only answer you will get from people like him.
                                > *plonk* right back at you shemp lover.
                                >[/color]

                                I've seen a lot of people get good answers from him. He got tired of
                                your attitude. He's not alone.

                                -Kevin
                                --
                                My email address is valid, but changes periodically.
                                To contact me please use the address from a recent posting.

                                Comment

                                Working...