/*
why doesnt char* city; work when entering a string,
but char city[256]; does?
I have to store city names and their temps in an array,
and then output all info. how do i utilize arrays in this
program? */
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
class Cities {
private:
char city[256];
int temp;
public:
void prclass();
Cities();
pause();
};
void Cities:: prclass()
{
cout<<'\n'<<city<<" "<<temp;
}
Cities:: Cities()
{
int t=0;
char answer;
do{
cout<<"\n\n";
cout<<"please enter name of city#"<<t+1<<": ";cin>>city;
cout<<"\nplease enter "<<city<<"'s temp: ";cin>>temp;
t++;
cout<<"\nAnother city?(Y/N)";
cin>>answer;
}while(answer =='y' || answer =='Y');
cout<<"\n\n";
}
void pause() {
cout<<"\n\n";
cout<<"Press >ENTER< to continue...";
cout<<"\n\n";
cin.get();
}//close of pause
int main()
{
clrscr();
Cities var;
var.prclass();
pause();
return 0;
}
--------------------------------------------------
remove *batSPAM* to e-mail me
-------------------------------------------------- 31 2108
Developwebsites wrote: /* why doesnt char* city; work when entering a string, but char city[256]; does?
I have to store city names and their temps in an array, and then output all info. how do i utilize arrays in this program? */
#include<iostream.h>
#include <iostream>
#include<iomanip.h>
#include <iomanip>
#include<conio.h>
Non-standard include file. Do you _really_ need it? class Cities { private: char city[256]; int temp;
public: void prclass(); Cities(); pause(); };
When using character strings (i.e. C-style strings) you
need a place for all the characters. The declaration:
char * city;
declares a pointer but does not allocate any space
for characters. See the FAQ below.
int main() { clrscr();
The clrscr() is not a standard function. Do you really
need to clear the screen before you execute a simple
program?
Cities var; var.prclass(); pause(); return 0; }
I highly recommend that you switch to using the
std::string class rather than the C-style strings.
The FAQ explains why.
--
Thomas Matthews
C++ newsgroup welcome message: http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq: http://www.raos.demon.uk/acllc-c++/faq.html
Other sites: http://www.josuttis.com -- C++ STL Library book
"Developwebsites" <de*************@aol.combatSPAM> wrote in message news:20***************************@mb-m13.aol.com... /* why doesnt char* city; work when entering a string, but char city[256]; does?
Because char* is NOT a string type. It is a poitner to char.
You pass an uninitialized pointer to cin to fill in. If you wanted
to do that, you must allocate storage
i.e., city = new char[256];
However, if you want a string, you should use the string class
rather than resorting to making mistakes using char*.
#include<iostream.h> #include<iomanip.h>
You're better advised to use the standard includes here:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Cities { private: char city[256];
string city;
char answer;
string answer;
}while(answer =='y' || answer =='Y');
If you user had typed "yes", reading only one char would leave "es" in the buffer
that would get read by the subsequent inputs.
"Developwebsites" <de*************@aol.combatSPAM> wrote in message
news:20***************************@mb-m13.aol.com... /* why doesnt char* city; work when entering a string, but char city[256]; does?
When you define an int, how much memory will be allocated? When you define
a pointer to a char (char*), how much memory will be allocated? When you
define an array of 256 characters (char city[256]), how much memory will be
allocated?
"jeffc" <no****@nowhere.com> wrote in message news:3f********@news1.prserv.net... "Developwebsites" <de*************@aol.combatSPAM> wrote in message news:20***************************@mb-m13.aol.com... /* why doesnt char* city; work when entering a string, but char city[256]; does?
When you define an int, how much memory will be allocated? When you define a pointer to a char (char*), how much memory will be allocated? When you define an array of 256 characters (char city[256]), how much memory will be allocated?
Nice think exercise, but the the real clue is what is different about
cin >>
to a char* is versus just about every other type?
Developwebsites wrote: /* why doesnt char* city; work when entering a string, but char city[256]; does?
Did you forget to allocate memory for 'city' to point to? It's hard to
say, since you apparently didn't post the code that demonstrates the
problem. You should read this: http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
The key points you are missing are 1 and 3 (compilable code, minimal code). I have to store city names and their temps in an array, and then output all info. how do i utilize arrays in this program? */
In the future, please try to use sane indenting. Random indentation
makes code very difficult to read. #include<iostream.h> #include<iomanip.h> #include<conio.h>
None of these are standard C++ headers. The first two should be
<iostream> and <iomanip>. The last one, shouldn't be. class Cities { private: char city[256]; int temp;
public: void prclass(); Cities(); pause(); };
void Cities:: prclass() { cout<<'\n'<<city<<" "<<temp; }
Cities:: Cities() { int t=0; char answer; do{ cout<<"\n\n"; cout<<"please enter name of city#"<<t+1<<": ";cin>>city;
cin >> city is dangerous. What if the user enters more than 255 characters?
cout<<"\nplease enter "<<city<<"'s temp: ";cin>>temp; t++; cout<<"\nAnother city?(Y/N)"; cin>>answer; }while(answer =='y' || answer =='Y');
This isn't a very useful loop, considering that it overwrites your data
each time it executes.
<snip>
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m... Nice think exercise, but the the real clue is what is different about cin >> to a char* is versus just about every other type?
I probably shouldn't answer questions on char* anymore anyway. I got so
sick of using them that I barely remember how anymore after switching to
C++.
> /* why doesnt char* city; work when entering a string, but char city[256]; does?
A pointer points to something. If you don`t have a something,
it cannot point nowhere. An array is a block of memory
in which you can put things.
I have to store city names and their temps in an array, and then output all info. how do i utilize arrays in this program? */
Why don`t you use std::string and std::vector? #include<iostream.h> #include<iomanip.h> #include<conio.h>
Non standard.
# include <iostream>
# include <iomanip>
# include <string>
# include <vector>
Please strip non standard code when you post here.
class Cities { private: char city[256]; int temp;
I would recommend
std::string city;
public: void prclass(); Cities(); pause(); };
Cities:: Cities() { int t=0; char answer; do{ cout<<"\n\n"; cout<<"please enter name of city#"<<t+1<<": ";cin>>city;
What if the user enters more than 255 characters? If you
use std::string, it is not a problem since it grows as
you need.
cout<<"\nplease enter "<<city<<"'s temp: ";cin>>temp; t++; cout<<"\nAnother city?(Y/N)"; cin>>answer; }while(answer =='y' || answer =='Y'); cout<<"\n\n"; }
I don't understand why you ask for another city if you are
done with initializing this one. Shouldn't the loop go
in main()?
void pause() { cout<<"\n\n"; cout<<"Press >ENTER< to continue..."; cout<<"\n\n"; cin.get(); }//close of pause
Please, too much comments can be as worst as no comments.
int main() { clrscr();
What's that?
Cities var; var.prclass(); pause(); return 0; }
I think you should have a loop in main() which creates cities
as long as the user wants it and stores them into a vector, no?
int main()
{
std::vector<Cities> cities_list;
while ( true )
{
Cities c;
cities_list.push_back(c);
// ask if the user is finished. if yes, break
}
}
Jonathan
Developwebsites wrote: /* why doesnt char* city; work when entering a string, but char city[256]; does?
What do mean, "doesn't work"? What memory does the pointer version of
city pointer to? Why aren't you using std::string as you've been told to
in the past, so you don't have to deal with memory
allocation/deallocation? Why do you refuse to study the language but
keep wasting our time with questions that have already been dealt with?
Brian Rodenborn
>cin >> city is dangerous. What if the user enters more than 255 characters?
I've never heard of any city with more than 255 characters, however, I am
having problems with cities such as New York or Los Angeles because of blank
spaces. cout<<"\nplease enter "<<city<<"'s temp: ";cin>>temp; t++; cout<<"\nAnother city?(Y/N)"; cin>>answer; }while(answer =='y' || answer =='Y');
This isn't a very useful loop, considering that it overwrites your data each time it executes.
thats why I asked how to use arrays with this code to store all the info.
--------------------------------------------------
remove *batSPAM* to e-mail me
--------------------------------------------------
>Why do you refuse to study the language but keep wasting our time with questions that have already been dealt with?
why dont you read the 2nd part of my question which was how to use arrays to
store several cities using this code.
--------------------------------------------------
remove *batSPAM* to e-mail me
--------------------------------------------------
Developwebsites wrote: cin >> city is dangerous. What if the user enters more than 255 characters?
I've never heard of any city with more than 255 characters,
That completely misses the point. Overflowing a buffer is one of the
most common exploits used to compromise computer systems. NEVER trust
the user to do what you expect, and never leave your code open to
undefined behavior if the user does something strange.
however, I am having problems with cities such as New York or Los Angeles because of blank spaces.
That's because the >> operator reads whitespace-delimited tokens. If you
want to read a line, use std::getline.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Developwebsites wrote: cin >> city is dangerous. What if the user enters more than 255 characters?
I've never heard of any city with more than 255 characters,
But I have heard of users sleeping on their keyboard and filling in
16KB of characters into a 3 character array per accident :-)
Never trust a user to input the right amount of things!
--
Karl Heinz Buchegger kb******@gascad.at
Developwebsites wrote: Why do you refuse to study the language but keep wasting our time with questions that have already been dealt with?
why dont you read the 2nd part of my question which was how to use arrays to store several cities using this code.
Start with fixing your design first.
Your program cries for 2 classes:
* one for a single city
every city has a name
* the second for the collection of cities
(an array of *cities*, not an array of strings or an array of characters)
--
Karl Heinz Buchegger kb******@gascad.at de*************@aol.combatSPAM (Developwebsites) wrote in message news:<20***************************@mb-m23.aol.com>... cin >> city is dangerous. What if the user enters more than 255 characters?
I've never heard of any city with more than 255 characters,
Depends on your definition of "city", IMHO.
I'm quite sure some Welsh towns or villages have names which are quite long.
Uwe
Developwebsites wrote: Why do you refuse to study the language but keep wasting our time with questions that have already been dealt with?
why dont you read the 2nd part of my question which was how to use arrays to store several cities using this code.
Why are you using arrays?
Brian Rodenborn
>* one for a single city every city has a name
* the second for the collection of cities (an array of *cities*, not an array of strings or an array of characters)
totally confused.
please be more specific.
all i want to do is enter several city names and store them and have them
printed out using a for loop.
--------------------------------------------------
remove *batSPAM* to e-mail me
--------------------------------------------------
> Why are you using arrays?
because thats what we covered in class, not vectors, or something else.
I could use pointers though.
--------------------------------------------------
remove *batSPAM* to e-mail me
--------------------------------------------------
Developwebsites wrote: Why are you using arrays?
because thats what we covered in class, not vectors, or something else. I could use pointers though.
And capitals in the beginning of sentences...
--
WW aka Attila
Developwebsites wrote:
Why are you using arrays?
because thats what we covered in class, not vectors, or something else. I could use pointers though.
Are you doing a school assignment? Nothing in your post indicated that.
Brian Rodenborn
> Are you doing a school assignment? Nothing in your post indicated that.
no i am doing top secret work for the CIA.
--------------------------------------------------
remove *batSPAM* to e-mail me
--------------------------------------------------
/*
I am trying to input names of 5 cities in a for loop, store them in an array,
(not a vector as we havent got there yet), and then print them out.
is this the correct way of doing it:
*/
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class Cities {
private:
string *city;
public:
Cities();
void input();
void output();
};
Cities:: Cities()
{
}
void Cities::input()
{
int total=0;
do{
cout<<"\n";
cout<<"Please enter name of city: ";
cin>>city[total];
total++;
}while(total<5);
cout<<"\n\n";
}
void Cities::output()
{
cout<<"\n";
cout<<"name of city"
<<"\n";
for(int t=0;t<5;t++)
{
cout<<setw(5)<<city[t]<<"\n";
}
}//close output
int main()
{
Cities info;
info.input();
info.output();
return 0;
}
--------------------------------------------------
remove *batSPAM* to e-mail me
--------------------------------------------------
> /* I am trying to input names of 5 cities in a for loop, store them in an
array, (not a vector as we havent got there yet), and then print them out. is this the correct way of doing it: */
Do not multipost. See my answer in alt.comp.lang.learn.c-c++ (that sure is
a long name).
Jonathan
Developwebsites wrote: * one for a single city every city has a name
* the second for the collection of cities (an array of *cities*, not an array of strings or an array of characters) totally confused. please be more specific. all i want to do is enter several city names and store them and have them printed out using a for loop.
AS I said:
class City
{
public:
private:
string name;
};
class Cities
{
public:
private:
vector< City > AllCities;
};
There is one class for a single city.
And then there is another class which holds
all those cities.
You say: all i want to do is enter several city names and store them and have them printed out using a for loop.
Fine. But:
a city name is a property of a single city. Thus you don't want to store
names, you want to store cities. In the near future you might want to
expand your example to: a city has a name *and* an inhabitants number.
Then you are prepared: just add the number of inhabitants to a single
city:
class City
{
public:
private:
string name;
long inhabitants;
};
No need to fiddle with the vector (array) in Cities. Ciities still holds
multiple cities, but a city now contains more information.
--
Karl Heinz Buchegger kb******@gascad.at
Developwebsites wrote: /* I am trying to input names of 5 cities in a for loop, store them in an array, (not a vector as we havent got there yet), and then print them out. is this the correct way of doing it: */
#include<iostream> #include<iomanip> #include<string> using namespace std;
class Cities { private: string *city;
That's not an array. It is a pointer to a string. But
at the moment this pointer doesn't even point to 1 string,
it points to nowehere.
If you want an array, you have to create one:
string city[5]; // array to store 5 cities
better would be to use a vector
vector<string> city;
becuase you don't have to keep track about how many city names are stored
by yourself any longer. Also: vector does the dynamic memory management for you.
--
Karl Heinz Buchegger kb******@gascad.at
Developwebsites wrote: Are you doing a school assignment? Nothing in your post indicated that.
no i am doing top secret work for the CIA.
Overdue, but:
*plonk*
Brian Rodenborn
> Overdue, but:
*plonk*
Brian Rodenborn
is the only answer you will get from people like him.
*plonk* right back at you shemp lover.
--------------------------------------------------
remove *batSPAM* to e-mail me
--------------------------------------------------
>If you want an array, you have to create one: string city[5]; // array to store 5 cities
what if the user enters more than 5 cities?
number of cities is unknown, and is set at 0, so string *city seemed to solve
the problem, compiles fine, but yes it does crash.
better would be to use a vector
vector<string> city;
as I've said we did not cover vector and until we do i am not going to use it.
--------------------------------------------------
remove *batSPAM* to e-mail me
--------------------------------------------------
Developwebsites wrote: If you want an array, you have to create one:
string city[5]; // array to store 5 cities
what if the user enters more than 5 cities?
What if? What if you expand the array to 5 billion and they enter 5
billion and one? This is a limitation of arrays. If you use arrays, you
have to deal with it.
number of cities is unknown, and is set at 0, so string *city seemed to solve the problem, compiles fine, but yes it does crash.
string *city is far worse than string city[5] in that there is NO SPACE
for storing the cities. At least with the array you had room for 5 of
them - with the pointer you don't have room for any. OF COURSE it
crashes when you try to store data through a wild pointer. What did you
expect?
Pointers aren't magical things that point to as much space as you want
or need. They point to exactly what you tell them too, no more, no less.
If you don't tell them to point to anything, they don't. If you try to
access the "thing" that an uninitialized pointer points to, the
program's behavior is undefined and it will probably crash (on most
modern desktop systems).
If you are going to use a pointer to string, you first have to make it
point to the strings you want to use. You could do this:
string *cities;
string some_cities[20];
cities = some_cities;
But obviously you'd be better off just using
string cities[20];
The other alternative is to determine the number of cities at run time:
int num_cities = GetNumberOfCities();
string *cities = new string[num_cities];
But, you'd be better off using a vector, which manages the dynamic array
for you.
as I've said we did not cover vector and until we do i am not going to use it.
You can do it however you want. We're just telling you what would be best.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Developwebsites wrote: Overdue, but:
*plonk*
Brian Rodenborn
is the only answer you will get from people like him. *plonk* right back at you shemp lover.
I've seen a lot of people get good answers from him. He got tired of
your attitude. He's not alone.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Kevin Goodsell wrote: Developwebsites wrote:
*plonk* right back at you shemp lover.
While Shemp was no Curl(e)y, there were some pretty good Shemp episodes.
I've seen a lot of people get good answers from him.
Eh, I try. I'm most helpful with the C and C++ crossover area, as I'm a
more experienced C programmer than C++. I mostly read to sharpen my
knowledge of weak points, like templates and some of the more obscure
features of standard containers.
He got tired of your attitude. He's not alone.
It's been my experience that people who behave like jackasses when the
first start posting rarely get better, but sometimes they "see the
light". I always give them a chance by pointing out their behavior. If
it continues, then why waste my time with that person?
The best would be for a mass plonking of the offender, but that's a
personal choice of group participants.
Brian Rodenborn This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: David MacQuigg |
last post by:
I have what looks like a bug trying to generate new style classes with
a factory function.
class Animal(object): pass
class Mammal(Animal): pass
def newAnimal(bases=(Animal,), dict={}):...
|
by: Simon Wittber |
last post by:
I've noticed that a few ASPN cookbook recipes, which are recent
additions, use classic classes.
I've also noticed classic classes are used in many places in the
standard library.
I've been...
|
by: Frank Rizzo |
last post by:
We are having one of those religious debates at work: Interfaces vs
Classes. My take is that Classes give you more flexibility. You can
enforce a contract on the descendant classes by marking...
|
by: Robert W. |
last post by:
I've almost completed building a Model-View-Controller but have run into a
snag. When an event is fired on a form control I want to automatically
updated the "connnected" property in the Model. ...
|
by: joye |
last post by:
Hello,
My question is how to use C# to call the existing libraries containing
unmanaged C++ classes directly, but not use C# or managed C++ wrappers
unmanaged C++ classes?
Does anyone know how...
|
by: ivan.leben |
last post by:
I want to write a Mesh class using half-edges. This class uses three
other classes: Vertex, HalfEdge and Face. These classes should be
linked properly in the process of building up the mesh by...
|
by: ivan.leben |
last post by:
I am writing this in a new thread to alert that I found a solution to
the problem mentioned here:
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7970afaa089fd5b8
and to avoid...
|
by: Zytan |
last post by:
I know that WebRequest.GetResponse can throw WebException from
internet tutorials. However in the MSDN docs:
http://msdn2.microsoft.com/en-us/library/system.net.webrequest.getresponse.aspx
It...
|
by: weaknessforcats |
last post by:
Handle Classes
Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
|
by: raylopez99 |
last post by:
Keywords: scope resolution, passing classes between parent and child
forms, parameter constructor method, normal constructor, default
constructor, forward reference, sharing classes between forms....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |