473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

why does it crash?

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
6 3030
"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
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

"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
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
>> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Eric Brunel | last post by:
Hi all, I was creating a Tkinter widget in the style of the reversed tabs below Excel worksheets and I stepped in a serious problem: the code I made makes python crash with a seg fault, bus...
6
by: Andrew Thompson | last post by:
My brother was saying his friend running IE4 (apparently he's a stubborn techno-geek) has 'severe troubles' with the site I am designing at http://www.lensescapes.com/ The site has been changed...
0
by: roni | last post by:
hi. i have application written in vb.net + managed c++ dll that call also to unmanaged c++ function. the application crash. i open the dump file of the crash with WinDbg and that's is the...
15
by: greenflame | last post by:
First of all I only have IE for testing. Ok. I have a script that is supposed to show, not evaluate, the indefinite integral of something. When I run the script it just craches IE. I have tried...
10
by: xixi | last post by:
i have db2 udb v8.1 on windows 64 bit 2003 server, after db2 server start , i found this in the db2diag.log, is this error? 2004-05-05-15.28.30.780000 Instance:DB2 Node:000...
8
by: Adam Louis | last post by:
I would like help resolving this problem. I'm a novice who's been hired to query a hospital database and extract useful information, available to me only in a dynamically generated, downloadable...
10
by: shiry | last post by:
Hi, I need to do some important cleanup before my console application exists. I used the console ctrl event. This is working well and it fires for all cases, including the CTRL_CLOSE_EVENT (if I...
6
by: junw2000 | last post by:
Below is a simple code: #include <iostream> class base{ public: base(): i(11){std::cout<<"base constructor"<<'\n';} virtual void f() = 0; virtual ~base(){ std::cout<<"base...
34
by: NewToCPP | last post by:
Hi, Why does a C/C++ programs crash? When there is access to a null pointer or some thing like that programs crash, but why do they crash? Thanks.
12
by: benjamin.krulewitch | last post by:
I'm debugging an issue with a C program that causes the computer to crash, and I'm attempting to log information immediately before the crash occurs. I us my BKprintLog function (see below) to...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.