classes and using *

  • Thread starter Developwebsites
  • Start date
D

Developwebsites

/*
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;
}
 
T

Thomas Matthews

Developwebsites said:
/*
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 said:
#include<iomanip.h>
#include said:
#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
 
R

Ron Natalie

Developwebsites said:
/*
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>
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.
 
J

jeffc

Developwebsites said:
/*
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?
 
R

Ron Natalie

jeffc said:
Developwebsites said:
/*
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?
 
K

Kevin Goodsell

Developwebsites said:
/*
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-lite/how-to-post.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
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
 
J

jeffc

Ron Natalie said:
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++.
 
J

Jonathan Mcdougall

/*
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
 
D

Default User

Developwebsites said:
/*
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
 
D

Developwebsites

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.
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.
 
D

Developwebsites

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.
 
K

Kevin Goodsell

Developwebsites said:
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
 
K

Karl Heinz Buchegger

Developwebsites said:
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!
 
K

Karl Heinz Buchegger

Developwebsites said:
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)
 
U

Uwe Schnitker

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
 
D

Default User

Developwebsites said:
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
 
D

Developwebsites

* 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.
 
W

WW

Developwebsites said:
because thats what we covered in class, not vectors, or something
else. I could use pointers though.

And capitals in the beginning of sentences...
 
D

Default User

Developwebsites said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top