why does it crash?

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

    why does it crash?

    this program compiles, but crashes when run.
    whats wrong with it?

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

    int numb_cities=0;
    const int MAX = 120;

    class Cities {
    private:
    char cityname[MAX][MAX];//an array of 120 cities.
    int temp[MAX];

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


    void Cities::input()
    {
    char y_n;
    do{
    cout<<"\n\n";
    cout<<"Please enter name of city#"<<numb_ci ties+1<<": ";
    cin>>cityname[numb_cities];
    cout<<"\nPlease enter "<<cityname[numb_cities]<<"'s temp: ";
    cin>>temp[numb_cities];
    numb_cities++;
    cout<<"\nAnothe r city?(Y/N)";
    cin>>y_n;
    }while(y_n =='y' || y_n =='Y');
    cout<<"\n\n";
    }

    void Cities::output( )
    {
    cout<<"\n"<<"na me of city"
    <<" temperature"
    <<"\n";
    for(int t=0;t<numb_citi es;t++)
    {
    cout<<setw(5)<< cityname[t]<<setw(15)<<tem p[t]<<"\n";
    }
    }//close output

    void Cities::sort()
    {
    char citynametemp[MAX][MAX];
    int tempt;
    for(int a=0;a<numb_citi es;a++) {
    for(int b=a+1; b<numb_cities;b ++) {
    if (temp[a]<temp[b]) {
    tempt = temp[a];
    temp[a] = temp[b];
    temp[b] = tempt;
    citynametemp[MAX][MAX] = cityname[a][MAX];
    cityname[a][MAX] = cityname[b][MAX];
    cityname[b][MAX] = citynametemp[MAX][MAX];
    }//close if
    }//close b loop
    }//close a loop
    }//close sort


    int main()
    {
    Cities info;

    info.input();
    info.output();
    info.sort();
    info.output();
    return 0;
    }
    --------------------------------------------------
    remove *batSPAM* to e-mail me
    --------------------------------------------------
  • Victor Bazarov

    #2
    Re: why does it crash?

    "Developwebsite s" <developwebsite s@aol.combatSPA M> wrote...[color=blue]
    > this program compiles, but crashes when run.
    > whats wrong with it?
    >
    > #include<iostre am.h>
    > #include<iomani p.h>
    >
    > int numb_cities=0;
    > const int MAX = 120;
    >
    > class Cities {
    > private:
    > char cityname[MAX][MAX];//an array of 120 cities.
    > int temp[MAX];
    >
    > public:
    > void input();
    > void output();
    > void sort();
    > };
    >
    >
    > void Cities::input()
    > {
    > char y_n;
    > do{
    > cout<<"\n\n";
    > cout<<"Please enter name of city#"<<numb_ci ties+1<<": ";
    > cin>>cityname[numb_cities];
    > cout<<"\nPlease enter "<<cityname[numb_cities]<<"'s temp: ";
    > cin>>temp[numb_cities];
    > numb_cities++;
    > cout<<"\nAnothe r city?(Y/N)";
    > cin>>y_n;
    > }while(y_n =='y' || y_n =='Y');
    > cout<<"\n\n";
    > }
    >
    > void Cities::output( )
    > {
    > cout<<"\n"<<"na me of city"
    > <<" temperature"
    > <<"\n";
    > for(int t=0;t<numb_citi es;t++)
    > {
    > cout<<setw(5)<< cityname[t]<<setw(15)<<tem p[t]<<"\n";
    > }
    > }//close output
    >
    > void Cities::sort()
    > {
    > char citynametemp[MAX][MAX];[/color]


    When declaring an array of MAX by MAX size, you have to remember
    that indices of the elements of the array only go from 0 to MAX-1.

    [color=blue]
    > int tempt;
    > for(int a=0;a<numb_citi es;a++) {
    > for(int b=a+1; b<numb_cities;b ++) {
    > if (temp[a]<temp[b]) {
    > tempt = temp[a];
    > temp[a] = temp[b];
    > temp[b] = tempt;
    > citynametemp[MAX][MAX] = cityname[a][MAX];[/color]


    There is no element with index [MAX][MAX] in the array 'citynametemp',
    as there isn't an element with any index [MAX] in any array of size
    'MAX'. The index spans from 0 to MAX-1.

    Now, you're trying to copy the contents of 'cityname[a]' to
    'citynametemp[a]'. You should probably use memcpy() for that:

    memcpy(cityname temp[a], cityname[a], MAX);

    and not the assignment.

    [color=blue]
    > cityname[a][MAX] = cityname[b][MAX];
    > cityname[b][MAX] = citynametemp[MAX][MAX];
    > }//close if
    > }//close b loop
    > }//close a loop
    > }//close sort
    >
    >
    > int main()
    > {
    > Cities info;
    >
    > info.input();
    > info.output();
    > info.sort();
    > info.output();
    > return 0;
    > }
    > --------------------------------------------------
    > remove *batSPAM* to e-mail me
    > --------------------------------------------------[/color]

    HTH

    Victor


    Comment

    • Moonlit

      #3
      Re: why does it crash?

      Hi,

      Could you be more specific at which line it crashes, I compiled it and it
      runs for me (that doesn't say it is correct though).

      BTW Using vectors and STL stuff would have reduced your code to a few lines.
      To get some ideas:

      #include <algorithm>
      #include <string>

      using namespace std;

      class CCityInfo
      {
      string CityName;
      int Temp;
      // Constructor stuff and init etc public keywords Get and Put functions
      etc..
      bool operator <( const CCityInfo& City1 ) const
      {
      return Temp < City1.Temp;
      }
      };
      vector<CCityInf o> Cities;

      sort( Cities.begin(), Cities.end() );

      -------------------------------------------------------
      [root@moonlit root]# ./r


      Please enter name of city#1: 1

      Please enter 1's temp: 2

      Another city?(Y/N)y


      Please enter name of city#2: 3

      Please enter 3's temp: 2

      Another city?(Y/N)n



      name of city temperature
      1 2
      3 2

      name of city temperature
      1 2
      3 2
      ------------------------------------------------------------



      "Developwebsite s" <developwebsite s@aol.combatSPA M> wrote in message
      news:2003101015 0519.17356.0000 0063@mb-m03.aol.com...[color=blue]
      > this program compiles, but crashes when run.
      > whats wrong with it?
      >
      > #include<iostre am.h>
      > #include<iomani p.h>
      >
      > int numb_cities=0;
      > const int MAX = 120;
      >
      > class Cities {
      > private:
      > char cityname[MAX][MAX];//an array of 120 cities.
      > int temp[MAX];
      >
      > public:
      > void input();
      > void output();
      > void sort();
      > };
      >
      >
      > void Cities::input()
      > {
      > char y_n;
      > do{
      > cout<<"\n\n";
      > cout<<"Please enter name of city#"<<numb_ci ties+1<<": ";
      > cin>>cityname[numb_cities];
      > cout<<"\nPlease enter "<<cityname[numb_cities]<<"'s temp: ";
      > cin>>temp[numb_cities];
      > numb_cities++;
      > cout<<"\nAnothe r city?(Y/N)";
      > cin>>y_n;
      > }while(y_n =='y' || y_n =='Y');
      > cout<<"\n\n";
      > }
      >
      > void Cities::output( )
      > {
      > cout<<"\n"<<"na me of city"
      > <<" temperature"
      > <<"\n";
      > for(int t=0;t<numb_citi es;t++)
      > {
      > cout<<setw(5)<< cityname[t]<<setw(15)<<tem p[t]<<"\n";
      > }
      > }//close output
      >
      > void Cities::sort()
      > {
      > char citynametemp[MAX][MAX];
      > int tempt;
      > for(int a=0;a<numb_citi es;a++) {
      > for(int b=a+1; b<numb_cities;b ++) {
      > if (temp[a]<temp[b]) {
      > tempt = temp[a];
      > temp[a] = temp[b];
      > temp[b] = tempt;
      > citynametemp[MAX][MAX] = cityname[a][MAX];
      > cityname[a][MAX] = cityname[b][MAX];
      > cityname[b][MAX] = citynametemp[MAX][MAX];
      > }//close if
      > }//close b loop
      > }//close a loop
      > }//close sort
      >
      >
      > int main()
      > {
      > Cities info;
      >
      > info.input();
      > info.output();
      > info.sort();
      > info.output();
      > return 0;
      > }
      > --------------------------------------------------
      > remove *batSPAM* to e-mail me
      > --------------------------------------------------[/color]


      Comment

      • Xenos

        #4
        Re: why does it crash?


        "Developwebsite s" <developwebsite s@aol.combatSPA M> wrote in message
        news:2003101015 0519.17356.0000 0063@mb-m03.aol.com...[color=blue]
        > this program compiles, but crashes when run.
        > whats wrong with it?
        >
        > #include<iostre am.h>
        > #include<iomani p.h>
        >
        > int numb_cities=0;
        > const int MAX = 120;
        >
        > class Cities {
        > private:
        > char cityname[MAX][MAX];//an array of 120 cities.
        > int temp[MAX];[/color]

        Why do people still insist on writing code that can buffer overrun???
        One (maybe two?) word: std::string
        [color=blue]
        >
        > public:
        > void input();
        > void output();
        > void sort();
        > };
        >
        >
        > void Cities::input()
        > {
        > char y_n;
        > do{
        > cout<<"\n\n";
        > cout<<"Please enter name of city#"<<numb_ci ties+1<<": ";
        > cin>>cityname[numb_cities];
        > cout<<"\nPlease enter "<<cityname[numb_cities]<<"'s temp: ";
        > cin>>temp[numb_cities];
        > numb_cities++;
        > cout<<"\nAnothe r city?(Y/N)";
        > cin>>y_n;
        > }while(y_n =='y' || y_n =='Y');
        > cout<<"\n\n";
        > }[/color]

        see above

        [color=blue]
        >
        > void Cities::output( )
        > {
        > cout<<"\n"<<"na me of city"
        > <<" temperature"
        > <<"\n";
        > for(int t=0;t<numb_citi es;t++)
        > {
        > cout<<setw(5)<< cityname[t]<<setw(15)<<tem p[t]<<"\n";
        > }
        > }//close output
        >
        > void Cities::sort()
        > {
        > char citynametemp[MAX][MAX];
        > int tempt;
        > for(int a=0;a<numb_citi es;a++) {
        > for(int b=a+1; b<numb_cities;b ++) {
        > if (temp[a]<temp[b]) {
        > tempt = temp[a];
        > temp[a] = temp[b];
        > temp[b] = tempt;
        > citynametemp[MAX][MAX] = cityname[a][MAX];
        > cityname[a][MAX] = cityname[b][MAX];
        > cityname[b][MAX] = citynametemp[MAX][MAX];[/color]

        you cannot move char array strings around using the assignment operator.

        [color=blue]
        > }//close if
        > }//close b loop
        > }//close a loop
        > }//close sort
        >
        >
        > int main()
        > {
        > Cities info;
        >
        > info.input();
        > info.output();
        > info.sort();
        > info.output();
        > return 0;
        > }
        > --------------------------------------------------
        > remove *batSPAM* to e-mail me
        > --------------------------------------------------[/color]


        Comment

        • Default User

          #5
          Re: why does it crash?

          Xenos wrote:
          [color=blue]
          > Why do people still insist on writing code that can buffer overrun???
          > One (maybe two?) word: std::string[/color]


          The idiot has been told this many times. He reacted so poorly to past
          advice that he landed in my killfile.



          Brian Rodenborn

          Comment

          • Developwebsites

            #6
            Re: why does it crash?

            >> Why do people still insist on writing code that can buffer overrun???[color=blue][color=green]
            >> One (maybe two?) word: std::string[/color]
            >
            >
            >The idiot has been told this many times. He reacted so poorly to past
            >advice that he landed in my killfile.
            >[/color]

            you are the one who is an idiot. I have said on numerous occassions that we
            havent covered std:: or vectors yet in class, therefore I cannot and will not
            use them eventhough I know how to.

            read people's posts next time and answer the question asked, instead of making
            yourself look smart in this ng.

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

            Comment

            • Jerry Coffin

              #7
              Re: why does it crash?

              In article <20031010150519 .17356.00000063 @mb-m03.aol.com>,
              developwebsites @aol.combatSPAM says...[color=blue]
              > this program compiles, but crashes when run.
              > whats wrong with it?[/color]

              Quite a bit, if you'll pardon my being a bit blunt about it.
              [color=blue]
              > #include<iostre am.h>
              > #include<iomani p.h>[/color]

              For standard code, you want <iostream> and <iomanip>. I'd suggest
              adding <vector> and <string> as well.
              [color=blue]
              > int numb_cities=0;[/color]

              I don't think this should be a global.
              [color=blue]
              > const int MAX = 120;[/color]

              And I don't think you should have this here at all.
              [color=blue]
              >
              > class Cities {
              > private:
              > char cityname[MAX][MAX];//an array of 120 cities.
              > int temp[MAX];
              >
              > public:
              > void input();
              > void output();
              > void sort();
              > };[/color]

              I think you should break things down a bit differently -- Cities isn't a
              very good class, IMO. I'd think about having City as a class, and then
              creating an array (or better yet, a vector) of City objects, with
              (perhaps) a bit of extra code to manage the collection as a whole. If
              City is well defined, however, the latter often becomes quite trivial.
              [color=blue]
              > void Cities::input()
              > {
              > char y_n;
              > do{
              > cout<<"\n\n";
              > cout<<"Please enter name of city#"<<numb_ci ties+1<<": ";
              > cin>>cityname[numb_cities];[/color]

              When you declare cityname[numb_cities], that means the valid subscripts
              run from 0 through (numb_cities-1). Right here you're trying to read
              data into cityname[numb_cities], which results in undefined behavior.
              [color=blue]
              > cout<<"\nPlease enter "<<cityname[numb_cities]<<"'s temp: ";
              > cin>>temp[numb_cities];[/color]

              Here you're doing pretty much the same thing. What it looks to me like
              you want to do is something like:

              for (i=0;i<numb_cit ies;i++)
              // leaving out the prompts and such for now...
              cin >> cityname[i];
              cin >> temp[i];
              }

              As I said before, however, I'd define City as a class, so you'd have
              something like this:

              class City {
              std::string name;
              int temp;

              public:

              void read() {
              std::cout << "Please enter city name: ";
              std::readline(s td::cin, name);
              std::cout << "Please enter temperature: ";
              std::cin >> temp;
              }
              };

              class cities {
              std::vector<Cit y> citynames;
              public:

              void read() {
              do {
              City x;
              x.read();
              citynames.push_ back(x);
              std::cout << "Another City?(Y/N)";
              std::cin >> y_n;
              while (std::tolower(y _n) == 'y');
              }
              };

              Now cities only deals with storing data for as many cities as the user
              enters, and leaves it to each individual City to read its own data.
              [color=blue]
              > void Cities::output( )
              > {
              > cout<<"\n"<<"na me of city"
              > <<" temperature"
              > <<"\n";
              > for(int t=0;t<numb_citi es;t++)
              > {
              > cout<<setw(5)<< cityname[t]<<setw(15)<<tem p[t]<<"\n";
              > }
              > }//close output[/color]

              Again, each City should know how to display its own data, and the cities
              collection should only deal with having each City in the collection
              display itself.
              [color=blue]
              > void Cities::sort()[/color]

              std::vector (as well as all the other standard collections) can be
              sorted without your writing a lousy sort routine for the ten billionth
              time.

              --
              Later,
              Jerry.

              The universe is a figment of its own imagination.

              Comment

              Working...