classes and using *

  • Thread starter Developwebsites
  • Start date
D

Developwebsites

/*
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::eek:utput()
{
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;
}
 
J

Jonathan Mcdougall

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

Karl Heinz Buchegger

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

Karl Heinz Buchegger

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

Developwebsites

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

Kevin Goodsell

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

Kevin Goodsell

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

Default User

Kevin said:
Developwebsites wrote:


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
 

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

Forum statistics

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

Latest Threads

Top