473,508 Members | 2,267 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3039
"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
3193
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
2148
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
2229
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
2127
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
9500
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
6430
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
8444
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
2010
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
4426
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
5544
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
7224
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
7120
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7380
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...
1
7039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.