By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

why does it crash?

P: n/a
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::output()
{
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[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
--------------------------------------------------
Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Developwebsites" <de*************@aol.combatSPAM> wrote...
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::output()
{
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[b]) {
tempt = temp[a];
temp[a] = temp[b];
temp[b] = 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[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
--------------------------------------------------


HTH

Victor
Jul 19 '05 #2

P: n/a
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
------------------------------------------------------------

"Developwebsites" <de*************@aol.combatSPAM> wrote in message
news:20***************************@mb-m03.aol.com...
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::output()
{
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[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
--------------------------------------------------

Jul 19 '05 #3

P: n/a

"Developwebsites" <de*************@aol.combatSPAM> wrote in message
news:20***************************@mb-m03.aol.com...
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::output()
{
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[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];
you cannot move char array strings around using the assignment operator.

}//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
--------------------------------------------------

Jul 19 '05 #4

P: n/a
Xenos wrote:
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
Jul 19 '05 #5

P: n/a
>> 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.


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
--------------------------------------------------
Jul 19 '05 #6

P: n/a
In article <20***************************@mb-m03.aol.com>,
de*************@aol.combatSPAM says...
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 <vector> and <string> as well.
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[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(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::output()
{
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.