classes and using *

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

    classes and using *

    /*
    why doesnt char* city; work when entering a string,
    but char city[256]; does?

    I have to store city names and their temps in an array,
    and then output all info. how do i utilize arrays in this
    program? */


    #include<iostre am.h>
    #include<iomani p.h>
    #include<conio. h>

    class Cities {
    private:
    char city[256];
    int temp;

    public:
    void prclass();
    Cities();
    pause();
    };

    void Cities:: prclass()
    {
    cout<<'\n'<<cit y<<" "<<temp;
    }

    Cities:: Cities()
    {
    int t=0;
    char answer;
    do{
    cout<<"\n\n";
    cout<<"please enter name of city#"<<t+1<<": ";cin>>city ;
    cout<<"\nplease enter "<<city<<"' s temp: ";cin>>temp ;
    t++;
    cout<<"\nAnothe r city?(Y/N)";
    cin>>answer;
    }while(answer =='y' || answer =='Y');
    cout<<"\n\n";
    }

    void pause() {
    cout<<"\n\n";
    cout<<"Press >ENTER< to continue...";
    cout<<"\n\n";
    cin.get();
    }//close of pause

    int main()
    {
    clrscr();
    Cities var;
    var.prclass();
    pause();
    return 0;
    }

    --------------------------------------------------
    remove *batSPAM* to e-mail me
    --------------------------------------------------
  • Thomas Matthews

    #2
    Re: classes and using *

    Developwebsites wrote:[color=blue]
    > /*
    > why doesnt char* city; work when entering a string,
    > but char city[256]; does?
    >
    > I have to store city names and their temps in an array,
    > and then output all info. how do i utilize arrays in this
    > program? */
    >
    >
    > #include<iostre am.h>[/color]
    #include <iostream>
    [color=blue]
    > #include<iomani p.h>[/color]
    #include <iomanip>
    [color=blue]
    > #include<conio. h>[/color]
    Non-standard include file. Do you _really_ need it?

    [color=blue]
    >
    > class Cities {
    > private:
    > char city[256];
    > int temp;
    >
    > public:
    > void prclass();
    > Cities();
    > pause();
    > };[/color]

    When using character strings (i.e. C-style strings) you
    need a place for all the characters. The declaration:
    char * city;
    declares a pointer but does not allocate any space
    for characters. See the FAQ below.
    [color=blue]
    > int main()
    > {
    > clrscr();[/color]
    The clrscr() is not a standard function. Do you really
    need to clear the screen before you execute a simple
    program?

    [color=blue]
    > Cities var;
    > var.prclass();
    > pause();
    > return 0;
    > }[/color]

    I highly recommend that you switch to using the
    std::string class rather than the C-style strings.
    The FAQ explains why.

    --
    Thomas Matthews

    C++ newsgroup welcome message:

    C++ Faq: http://www.parashift.com/c++-faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.l earn.c-c++ faq:

    Other sites:
    http://www.josuttis.com -- C++ STL Library book

    Comment

    • Ron Natalie

      #3
      Re: classes and using *


      "Developwebsite s" <developwebsite s@aol.combatSPA M> wrote in message news:2003100114 1930.15953.0000 0214@mb-m13.aol.com...[color=blue]
      > /*
      > why doesnt char* city; work when entering a string,
      > but char city[256]; does?[/color]

      Because char* is NOT a string type. It is a poitner to char.
      You pass an uninitialized pointer to cin to fill in. If you wanted
      to do that, you must allocate storage
      i.e., city = new char[256];

      However, if you want a string, you should use the string class
      rather than resorting to making mistakes using char*.
      [color=blue]
      > #include<iostre am.h>
      > #include<iomani p.h>[/color]

      You're better advised to use the standard includes here:
      #include <iostream>
      #include <iomanip>
      #include <string>
      using namespace std;
      [color=blue]
      > class Cities {
      > private:
      > char city[256];[/color]
      string city;
      [color=blue]
      > char answer;[/color]

      string answer;
      [color=blue]
      > }while(answer =='y' || answer =='Y');[/color]

      If you user had typed "yes", reading only one char would leave "es" in the buffer
      that would get read by the subsequent inputs.


      Comment

      • jeffc

        #4
        Re: classes and using *


        "Developwebsite s" <developwebsite s@aol.combatSPA M> wrote in message
        news:2003100114 1930.15953.0000 0214@mb-m13.aol.com...[color=blue]
        > /*
        > why doesnt char* city; work when entering a string,
        > but char city[256]; does?[/color]

        When you define an int, how much memory will be allocated? When you define
        a pointer to a char (char*), how much memory will be allocated? When you
        define an array of 256 characters (char city[256]), how much memory will be
        allocated?


        Comment

        • Ron Natalie

          #5
          Re: classes and using *


          "jeffc" <nobody@nowhere .com> wrote in message news:3f7b2015_4 @news1.prserv.n et...[color=blue]
          >
          > "Developwebsite s" <developwebsite s@aol.combatSPA M> wrote in message
          > news:2003100114 1930.15953.0000 0214@mb-m13.aol.com...[color=green]
          > > /*
          > > why doesnt char* city; work when entering a string,
          > > but char city[256]; does?[/color]
          >
          > When you define an int, how much memory will be allocated? When you define
          > a pointer to a char (char*), how much memory will be allocated? When you
          > define an array of 256 characters (char city[256]), how much memory will be
          > allocated?
          >[/color]
          Nice think exercise, but the the real clue is what is different about
          cin >>
          to a char* is versus just about every other type?


          Comment

          • Kevin Goodsell

            #6
            Re: classes and using *

            Developwebsites wrote:
            [color=blue]
            > /*
            > why doesnt char* city; work when entering a string,
            > but char city[256]; does?[/color]

            Did you forget to allocate memory for 'city' to point to? It's hard to
            say, since you apparently didn't post the code that demonstrates the
            problem. You should read this:



            The key points you are missing are 1 and 3 (compilable code, minimal code).
            [color=blue]
            >
            > I have to store city names and their temps in an array,
            > and then output all info. how do i utilize arrays in this
            > program? */
            >[/color]

            In the future, please try to use sane indenting. Random indentation
            makes code very difficult to read.
            [color=blue]
            >
            > #include<iostre am.h>
            > #include<iomani p.h>
            > #include<conio. h>[/color]

            None of these are standard C++ headers. The first two should be
            <iostream> and <iomanip>. The last one, shouldn't be.
            [color=blue]
            >
            > class Cities {
            > private:
            > char city[256];
            > int temp;
            >
            > public:
            > void prclass();
            > Cities();
            > pause();
            > };
            >
            > void Cities:: prclass()
            > {
            > cout<<'\n'<<cit y<<" "<<temp;
            > }
            >
            > Cities:: Cities()
            > {
            > int t=0;
            > char answer;
            > do{
            > cout<<"\n\n";
            > cout<<"please enter name of city#"<<t+1<<": ";cin>>city ;[/color]

            cin >> city is dangerous. What if the user enters more than 255 characters?
            [color=blue]
            > cout<<"\nplease enter "<<city<<"' s temp: ";cin>>temp ;
            > t++;
            > cout<<"\nAnothe r city?(Y/N)";
            > cin>>answer;
            > }while(answer =='y' || answer =='Y');[/color]

            This isn't a very useful loop, considering that it overwrites your data
            each time it executes.

            <snip>

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

            Comment

            • jeffc

              #7
              Re: classes and using *


              "Ron Natalie" <ron@sensor.com > wrote in message
              news:3f7b1f8f$0 $36918$9a6e19ea @news.newshosti ng.com...[color=blue][color=green]
              > >[/color]
              > Nice think exercise, but the the real clue is what is different about
              > cin >>
              > to a char* is versus just about every other type?[/color]

              I probably shouldn't answer questions on char* anymore anyway. I got so
              sick of using them that I barely remember how anymore after switching to
              C++.


              Comment

              • Jonathan Mcdougall

                #8
                Re: classes and using *

                > /*[color=blue]
                > why doesnt char* city; work when entering a string,
                > but char city[256]; does?[/color]

                A pointer points to something. If you don`t have a something,
                it cannot point nowhere. An array is a block of memory
                in which you can put things.
                [color=blue]
                > I have to store city names and their temps in an array,
                > and then output all info. how do i utilize arrays in this
                > program? */[/color]

                Why don`t you use std::string and std::vector?
                [color=blue]
                >
                > #include<iostre am.h>
                > #include<iomani p.h>
                > #include<conio. h>[/color]

                Non standard.

                # include <iostream>
                # include <iomanip>
                # include <string>
                # include <vector>

                Please strip non standard code when you post here.
                [color=blue]
                > class Cities {
                > private:
                > char city[256];
                > int temp;[/color]

                I would recommend

                std::string city;
                [color=blue]
                > public:
                > void prclass();
                > Cities();
                > pause();
                > };[/color]
                [color=blue]
                > Cities:: Cities()
                > {
                > int t=0;
                > char answer;
                > do{
                > cout<<"\n\n";
                > cout<<"please enter name of city#"<<t+1<<": ";cin>>city ;[/color]

                What if the user enters more than 255 characters? If you
                use std::string, it is not a problem since it grows as
                you need.
                [color=blue]
                > cout<<"\nplease enter "<<city<<"' s temp: ";cin>>temp ;
                > t++;
                > cout<<"\nAnothe r city?(Y/N)";
                > cin>>answer;
                > }while(answer =='y' || answer =='Y');
                > cout<<"\n\n";
                > }[/color]

                I don't understand why you ask for another city if you are
                done with initializing this one. Shouldn't the loop go
                in main()?
                [color=blue]
                > void pause() {
                > cout<<"\n\n";
                > cout<<"Press >ENTER< to continue...";
                > cout<<"\n\n";
                > cin.get();
                > }//close of pause[/color]

                Please, too much comments can be as worst as no comments.
                [color=blue]
                > int main()
                > {
                > clrscr();[/color]

                What's that?
                [color=blue]
                > Cities var;
                > var.prclass();
                > pause();
                > return 0;
                > }[/color]

                I think you should have a loop in main() which creates cities
                as long as the user wants it and stores them into a vector, no?

                int main()
                {
                std::vector<Cit ies> cities_list;

                while ( true )
                {
                Cities c;
                cities_list.pus h_back(c);

                // ask if the user is finished. if yes, break
                }
                }


                Jonathan


                Comment

                • Default User

                  #9
                  Re: classes and using *

                  Developwebsites wrote:[color=blue]
                  >
                  > /*
                  > why doesnt char* city; work when entering a string,
                  > but char city[256]; does?[/color]


                  What do mean, "doesn't work"? What memory does the pointer version of
                  city pointer to? Why aren't you using std::string as you've been told to
                  in the past, so you don't have to deal with memory
                  allocation/deallocation? Why do you refuse to study the language but
                  keep wasting our time with questions that have already been dealt with?




                  Brian Rodenborn

                  Comment

                  • Developwebsites

                    #10
                    Re: classes and using *

                    >cin >> city is dangerous. What if the user enters more than 255 characters?[color=blue]
                    >[/color]

                    I've never heard of any city with more than 255 characters, however, I am
                    having problems with cities such as New York or Los Angeles because of blank
                    spaces.
                    [color=blue][color=green]
                    >> cout<<"\nplease enter "<<city<<"' s temp: ";cin>>temp ;
                    >> t++;
                    >> cout<<"\nAnothe r city?(Y/N)";
                    >> cin>>answer;
                    >> }while(answer =='y' || answer =='Y');[/color]
                    >
                    >This isn't a very useful loop, considering that it overwrites your data
                    >each time it executes.
                    >[/color]

                    thats why I asked how to use arrays with this code to store all the info.
                    --------------------------------------------------
                    remove *batSPAM* to e-mail me
                    --------------------------------------------------

                    Comment

                    • Developwebsites

                      #11
                      Re: classes and using *

                      >Why do you refuse to study the language but[color=blue]
                      >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.

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

                      Comment

                      • Kevin Goodsell

                        #12
                        Re: classes and using *

                        Developwebsites wrote:
                        [color=blue][color=green]
                        >>cin >> city is dangerous. What if the user enters more than 255 characters?
                        >>[/color]
                        >
                        >
                        > I've never heard of any city with more than 255 characters,[/color]

                        That completely misses the point. Overflowing a buffer is one of the
                        most common exploits used to compromise computer systems. NEVER trust
                        the user to do what you expect, and never leave your code open to
                        undefined behavior if the user does something strange.
                        [color=blue]
                        > however, I am
                        > having problems with cities such as New York or Los Angeles because of blank
                        > spaces.[/color]

                        That's because the >> operator reads whitespace-delimited tokens. If you
                        want to read a line, use std::getline.

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

                        Comment

                        • Karl Heinz Buchegger

                          #13
                          Re: classes and using *



                          Developwebsites wrote:[color=blue]
                          >[color=green]
                          > >cin >> city is dangerous. What if the user enters more than 255 characters?
                          > >[/color]
                          >
                          > I've never heard of any city with more than 255 characters,[/color]

                          But I have heard of users sleeping on their keyboard and filling in
                          16KB of characters into a 3 character array per accident :-)

                          Never trust a user to input the right amount of things!

                          --
                          Karl Heinz Buchegger
                          kbuchegg@gascad .at

                          Comment

                          • Karl Heinz Buchegger

                            #14
                            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]

                            Start with fixing your design first.
                            Your program cries for 2 classes:

                            * 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)


                            --
                            Karl Heinz Buchegger
                            kbuchegg@gascad .at

                            Comment

                            • Uwe Schnitker

                              #15
                              Re: classes and using *

                              developwebsites @aol.combatSPAM (Developwebsite s) wrote in message news:<200310012 10402.15436.000 00114@mb-m23.aol.com>...[color=blue][color=green]
                              > >cin >> city is dangerous. What if the user enters more than 255 characters?
                              > >[/color]
                              >
                              > I've never heard of any city with more than 255 characters,[/color]

                              Depends on your definition of "city", IMHO.

                              I'm quite sure some Welsh towns or villages have names which are quite long.

                              Uwe

                              Comment

                              Working...