why does it crash?

D

Developwebsites

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

#include<iostream.h>
#include<iomanip.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_cities+1<<": ";
cin>>cityname[numb_cities];
cout<<"\nPlease enter "<<cityname[numb_cities]<<"'s temp: ";
cin>>temp[numb_cities];
numb_cities++;
cout<<"\nAnother city?(Y/N)";
cin>>y_n;
}while(y_n =='y' || y_n =='Y');
cout<<"\n\n";
}

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

void Cities::sort()
{
char citynametemp[MAX][MAX];
int tempt;
for(int a=0;a<numb_cities;a++) {
for(int b=a+1; b<numb_cities;b++) {
if (temp[a]<temp) {
tempt = temp[a];
temp[a] = temp;
temp = tempt;
citynametemp[MAX][MAX] = cityname[a][MAX];
cityname[a][MAX] = cityname[MAX];
cityname[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;
}
 
V

Victor Bazarov

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

#include<iostream.h>
#include<iomanip.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_cities+1<<": ";
cin>>cityname[numb_cities];
cout<<"\nPlease enter "<<cityname[numb_cities]<<"'s temp: ";
cin>>temp[numb_cities];
numb_cities++;
cout<<"\nAnother city?(Y/N)";
cin>>y_n;
}while(y_n =='y' || y_n =='Y');
cout<<"\n\n";
}

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

void Cities::sort()
{
char citynametemp[MAX][MAX];


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.

int tempt;
for(int a=0;a<numb_cities;a++) {
for(int b=a+1; b<numb_cities;b++) {
if (temp[a]<temp) {
tempt = temp[a];
temp[a] = temp;
temp = tempt;
citynametemp[MAX][MAX] = cityname[a][MAX];



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(citynametemp[a], cityname[a], MAX);

and not the assignment.

cityname[a][MAX] = cityname[MAX];
cityname[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;
}


HTH

Victor
 
M

Moonlit

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<CCityInfo> 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
 
X

Xenos

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

#include<iostream.h>
#include<iomanip.h>

int numb_cities=0;
const int MAX = 120;

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

Why do people still insist on writing code that can buffer overrun???
One (maybe two?) word: std::string
public:
void input();
void output();
void sort();
};


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

see above

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

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


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

Default User

Xenos said:
Why do people still insist on writing code that can buffer overrun???
One (maybe two?) word: std::string


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



Brian Rodenborn
 
D

Developwebsites

Why do people still insist on writing code that can buffer overrun???
The idiot has been told this many times. He reacted so poorly to past
advice that he landed in my killfile.

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

Jerry Coffin

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

Quite a bit, if you'll pardon my being a bit blunt about it.
#include<iostream.h>
#include<iomanip.h>

For standard code, you want <iostream> and <iomanip>. I'd suggest
adding said:
int numb_cities=0;

I don't think this should be a global.
const int MAX = 120;

And I don't think you should have this here at all.
class Cities {
private:
char cityname[MAX][MAX];//an array of 120 cities.
int temp[MAX];

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

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.
void Cities::input()
{
char y_n;
do{
cout<<"\n\n";
cout<<"Please enter name of city#"<<numb_cities+1<<": ";
cin>>cityname[numb_cities];

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.
cout<<"\nPlease enter "<<cityname[numb_cities]<<"'s temp: ";
cin>>temp[numb_cities];

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_cities;i++)
// leaving out the prompts and such for now...
cin >> cityname;
cin >> temp;
}

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(std::cin, name);
std::cout << "Please enter temperature: ";
std::cin >> temp;
}
};

class cities {
std::vector<City> 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.
void Cities::eek:utput()
{
cout<<"\n"<<"name of city"
<<" temperature"
<<"\n";
for(int t=0;t<numb_cities;t++)
{
cout<<setw(5)<<cityname[t]<<setw(15)<<temp[t]<<"\n";
}
}//close output

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.
void Cities::sort()

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top