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
-------------------------------------------------- 6 3047
"Developwebsite s" <de************ *@aol.combatSPA M> wrote... 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];
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_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];
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.
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
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" <de************ *@aol.combatSPA M> wrote in message
news:20******** *************** ****@mb-m03.aol.com... 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 --------------------------------------------------
"Developwebsite s" <de************ *@aol.combatSPA M> wrote in message
news:20******** *************** ****@mb-m03.aol.com... 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];
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_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"; }
see above 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];
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 --------------------------------------------------
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
>> 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
--------------------------------------------------
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<iostre am.h> #include<iomani p.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_ci ties+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_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.
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 error or X11 BadGC error on both Solaris (2.6 and
2.7) and Linux (Mandrake 8.0); it doesn't crash on Windows. I tried to simplify
the script, but I couldn't reproduce the crash with a simpler code. So the code
below is somewhat long; sorry for that.
...
|
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 recently (a Java applet
that was causing a 'focus' problem was removed).
I was wonderring if there anybody out there with
access to IE4 could cast a quick browser over it
|
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 call stack
(using the sos.dll) :
=====================================
|
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 to get all the obvious bugs out... Um... The script is
at: http://ali.freezope.org/idf test and what it is supposed to show is
at: http://ali.freezope.org/idf result
|
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
PID:1692(db2syscs.exe) TID:2860 Appid:AC10040A.GD5F.00FC56D8BEC5
base sys utilities sqledint Probe:30
Crash Recovery is needed.
2004-05-05-15.28.31.890000 Instance:DB2 Node:000
| |
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 .mdb.
The query below query runs correctly and without error, but any
attempt to save it causes Access to crash without a message, leaving
the .ldb file. Opening the DB reveals it saved a blank "query1".
I've upgraded to Jet SP 8, and I'm running...
|
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 close the
application from the X botton). But on
the contrary to what the MSDN indicates
(ms-help://MS.VSCC.2003/MS.MSDNQTR.2003OCT.1033/dllproc/base/handlerroutine.htm),
ending my process from the task manager did not fire the
CTRL_CLOSE_EVENT....
|
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 destructor"<<'\n';}
int i;
|
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.
|
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 write
information into a log file. The problem is, i'm not confident that
information i write to the log gets saved onto the hard drive before
the crash occurs.
My understanding of hard drives and OS are that because hard drives are
so slow,...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |