why do i get garbage output?

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

    why do i get garbage output?

    using Dev C++4
    user can enter names of cities and temps,
    but when it output city names i get garbage but
    temps are fine.


    struct Cities {
    char city[];
    int temp;
    };


    void input_names(Cit ies info[], int numb_cities) {

    for(int t=0;t<numb_citi es;t++) {
    cout<<"\n\n";
    cout<<"please enter name of city#"<<t+1<<": ";cin>>info[t].city;
    cout<<"\nplease enter temp of city "
    <<info[t].city<<": ";
    cin>>info[t].temp;
    }

    cout<<"\n\n";
    for(int t=0;t<numb_citi es;t++) {
    cout<<info[t].city<<" "<<info[t].temp<<"\n";
    }

    }//close input_names;

    void main() {
    int n,
    numb_cities=10; //number of cities

    void input_names(Cit ies[], int);

    Cities info[numb_cities];
    input_names(inf o, numb_cities);
    }



    --------------------------------------------------
    remove *batSPAM* to e-mail me
    --------------------------------------------------
  • Gianni Mariani

    #2
    Re: why do i get garbage output?

    Developwebsites wrote:[color=blue]
    > using Dev C++4
    > user can enter names of cities and temps,
    > but when it output city names i get garbage but
    > temps are fine.
    >
    >
    > struct Cities {
    > char city[];[/color]

    use
    string city;

    instead
    [color=blue]
    > int temp;
    > };
    >[/color]


    The problem is that cin >> char [] does not allocate storage, you need
    to do that. std::string does all the magic of allocating storage and
    cleaning it up.

    Comment

    • Ron Natalie

      #3
      Re: why do i get garbage output?


      "Developwebsite s" <developwebsite s@aol.combatSPA M> wrote in message news:2003092213 3828.13184.0000 0001@mb-m06.aol.com...
      [color=blue]
      > struct Cities {
      > char city[];
      > int temp;
      > };[/color]

      This shouldn't even compile. The array city needs a definite size. Frankly,
      I suspect you're new to C++. Don't beat char arrays to death. Use the string
      class.

      [color=blue]
      > cin>>info[t].temp;[/color]

      You don't test if these finputs ail.
      [color=blue]
      >
      > void main() {[/color]

      main must return int.
      [color=blue]
      > void input_names(Cit ies[], int);[/color]

      You don't need to repeat the declaration here.
      [color=blue]
      > Cities info[numb_cities];[/color]

      This is also not legal. Array declarations in C++ need sizes that are constant
      expressions (I bet you're using an anceint G++).

      I'd recommend you get a decent C++ text. I think Koenig & Moo's Accellerated C++
      will be very helpful to you.


      Comment

      • Developwebsites

        #4
        Re: why do i get garbage output?

        >>[color=blue][color=green]
        >> void main() {[/color]
        >
        >main must return int.[/color]

        why is that?
        return to where?
        the program compiles fine as it is.

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

        Comment

        • Developwebsites

          #5
          Re: why do i get garbage output?

          >[color=blue][color=green]
          >> void input_names(Cit ies[], int);[/color]
          >
          >You don't need to repeat the declaration >here.[/color]

          I am not repeating anything.
          this is a prototype.
          [color=blue][color=green]
          >> Cities info[numb_cities];[/color]
          >
          >This is also not legal. Array declarations in C++ need sizes that are
          >constant
          >expressions (I bet you're using an anceint G++).[/color]

          again, it compiled fine.
          1st sentence of my post:
          using Dev C++4
          is it old? nope, but i do have borland c++ 1.0 from 91 which does give an
          error.

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

          Comment

          • Kevin Goodsell

            #6
            Re: why do i get garbage output?

            Developwebsites wrote:[color=blue][color=green][color=darkred]
            >>>void main() {[/color]
            >>
            >>main must return int.[/color]
            >
            >
            > why is that?[/color]

            Because the standard requires it, as the ARM did before it.
            [color=blue]
            > return to where?[/color]

            The calling environment.
            [color=blue]
            > the program compiles fine as it is.[/color]

            regardless of the shortcomings of your compiler, it is required. Better
            compilers will refuse it with the wrong return type.

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

            Comment

            • Kevin Goodsell

              #7
              Re: why do i get garbage output?

              Developwebsites wrote:
              [color=blue][color=green][color=darkred]
              >>> void input_names(Cit ies[], int);[/color]
              >>
              >>You don't need to repeat the declaration >here.[/color]
              >
              >
              > I am not repeating anything.
              > this is a prototype.[/color]

              Yes, a prototype that appeared earlier in the code. Prototypes are
              declarations.
              [color=blue]
              >
              >[color=green][color=darkred]
              >>> Cities info[numb_cities];[/color]
              >>
              >>This is also not legal. Array declarations in C++ need sizes that are
              >>constant
              >>expressions (I bet you're using an anceint G++).[/color]
              >
              >
              > again, it compiled fine.[/color]

              Again, that doesn't matter. C++ is not "Whatever your compiler accepts".
              All compilers accept some things that are not C++. gcc in particular
              supports several language extensions by default. Learn how to invoke it
              in standard compliant mode and it will diagnose your errors.
              [color=blue]
              > 1st sentence of my post:
              > using Dev C++4[/color]

              That comes with an old version of gcc.

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

              Comment

              • Ron Natalie

                #8
                Re: why do i get garbage output?


                "Developwebsite s" <developwebsite s@aol.combatSPA M> wrote in message news:2003092300 0349.01515.0000 0026@mb-m19.aol.com...[color=blue][color=green][color=darkred]
                > >>
                > >> void main() {[/color]
                > >
                > >main must return int.[/color]
                >
                > why is that?[/color]

                The standard says so.
                [color=blue]
                > return to where?[/color]

                The runtime environment.
                [color=blue]
                > the program compiles fine as it is.
                >[/color]
                Do not confuse tolerance with correctness.


                Comment

                • Developwebsites

                  #9
                  Re: why do i get garbage output?

                  >>>> Cities info[numb_cities];[color=blue][color=green][color=darkred]
                  >>>
                  >>>This is also not legal. Array declarations in C++ need sizes that are
                  >>>constant[/color][/color][/color]

                  the point of my program, and the function before the above statement, was to
                  have the user input the number of cities.
                  then, the user inputs city names and temps.
                  how can i have a constant if i dont know the number of cities the user will
                  enter.
                  it is only logical(as my code is), it makes no sense to have a constant if the
                  number of cities or employees etc. is unknown.
                  if it could be done in BASIC why not C++?

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

                  Comment

                  • Jerry Coffin

                    #10
                    Re: why do i get garbage output?

                    In article <20030923121722 .01706.00000055 @mb-m29.aol.com>,
                    developwebsites @aol.combatSPAM says...[color=blue][color=green][color=darkred]
                    > >>>> Cities info[numb_cities];
                    > >>>
                    > >>>This is also not legal. Array declarations in C++ need sizes that are
                    > >>>constant[/color][/color]
                    >
                    > the point of my program, and the function before the above statement, was to
                    > have the user input the number of cities.
                    > then, the user inputs city names and temps.
                    > how can i have a constant if i dont know the number of cities the user will
                    > enter.
                    > it is only logical(as my code is), it makes no sense to have a constant if the
                    > number of cities or employees etc. is unknown.
                    > if it could be done in BASIC why not C++?[/color]

                    It _can_ be done in C++, but it's done somewhat differently. First of
                    all, until you know what you're doing, it's probably just about as well
                    to forget that C++ has arrays at all, and instead always use std::vector
                    for the kinds of problems where you'd use arrays in BASIC.

                    Using std::vector, you don't need to pre-specify the number of cities at
                    all -- you can simply use push_back to add each city as it's entered,
                    and when the user enters a blank line (or some specific string like
                    "end", etc.) you quit adding cities to the collection.

                    As far as your code being logical, well, coming from BASIC it probably
                    seems that way -- but to the user it will undoubtedly seem supremely
                    ILlogical that you demand that s/he count the cities before entering the
                    data. In fact, counting items in a list is exactly the sort of thing
                    the computer is good at, and the use should NEVER have to do!

                    --
                    Later,
                    Jerry.

                    The universe is a figment of its own imagination.

                    Comment

                    • Default User

                      #11
                      Re: why do i get garbage output?

                      Developwebsites wrote:[color=blue]
                      >[color=green][color=darkred]
                      > >>>> Cities info[numb_cities];
                      > >>>
                      > >>>This is also not legal. Array declarations in C++ need sizes that are
                      > >>>constant[/color][/color]
                      >
                      > the point of my program, and the function before the above statement, was to
                      > have the user input the number of cities.[/color]


                      Then you either need to use dynamic arrays (as in pointers to memory
                      allocated by new) or much much much preferably with std::vector.


                      If you want help on this newsgroup, you are going to have to stop
                      arguing with people and things that you don't about. There a number of
                      very knowledgable programmers here, so follow their advice. Don't use
                      the platform-specific extensions that you are told not to, instead use
                      the proper standard forms.



                      Brian Rodenborn

                      Comment

                      Working...