Objects in array

So i searched and watched all other threads and didnt find a proper solution.

I'm trying to store my objects in an array in some way.

Okey so the code lets a user input the name and temperature of a city.

It creates the object newCity with the data.

Now i want a way to store "newCity" in an array so i can list all citys and temperatures.

Also, I'm supposed to be able to add more citys, BUT it shall not be defined how many and im supposed to use arrays and not vectors.

And only include <iostream> and use std namespace.

Any solution to this would be greatful.

I also wonder if theres anyway to have the char array at line 38 work without having to give it a maximum size.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using namespace std;

//Klass
class City
{
public:
	char* name;
	float temperature;

	void Name();
	void Temp();

	City(char* _name, float _temperature);

};

//Konstruktor
City::City(char* _name, float _temperature)
{
	name = _name;
	temperature = _temperature;
}

//Metoder
void City::Name()
{
	cout << endl << name;
}

void City::Temp()
{
	cout << endl << temperature;
}

int main()
{
	char name[256];
	float temperature;

	cout << "Input city name: ";
	cin >> name;
	cout << "Input city temperature: ";
	cin >> temperature;

	City newCity(name, temperature);

}
Declare the array of type City:


City arr[std::size_t];  // Create a city array of a specified size.
arr[0] = newCity; // Copy the data into the array at index 0
Thanks i will try this but i dont understand what size_t is, i checked the documentation and dont really know.

How does it work?

the problem is that the array cant have a maximum size, since they should always be able to add more and more citys.

cause right now it sais "type name is not allowed" on the size_t part
Last edited on
size_t is a typedef for an unsigned integral type. I think kefin meant that you have to put an integer greater than 0 where he wrote size_t.

Arrays have a fixed size. If you want to put a new city in a full array you have to create a new larger array, copy over the old cities, store the new city and delete the old array. That's basically what std::vector does for you.

I also wonder if theres anyway to have the char array at line 38 work without having to give it a maximum size.

Nope. Even using dynamic memory allocation, where you don't hardcode the size of the array, you still have to provide a number of bytes to reserve.
Or you can use std::string that does it for you.
If you can't place a hard limit on the size of the array at compile time, then you'll need to dynamically allocate it.

Are you allowed to ask the user how many cities they want, and then allocate the array to a specific size? If not, then you'll have to keep re-allocating new memory for the array when a new city as added, and then copy the contents of the old, smaller array, into the new, larger one. It might be more efficient to do this in blocks of, say, 10 objects, rather than for every new object.

Do you need to store City objects in the array? Or can you just store pointers to them? If you need to store actual objects, then you'll be copying them into the array, which means you'll need to worry about deep-copying the City object. You'll need to create a copy constructor for your class, and make sure that it properly copies the name string rather than just copying the pointer.

Thanks i will try this but i dont understand what size_t is, i checked the documentation and dont really know.

[...]
cause right now it sais "type name is not allowed" on the size_t part

size_t is a type. It's almost always an integer of some kind. The code kefin posted isn't legal C++. I think what he was trying to say that you need to specify the size of the array as an integer.
Last edited on
Okey so i got it to working like 50%

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
using namespace std;

//Klass
class City
{
public:
	char* name;
	float temperature;

	
};

int main()
{
	char name[256];
	float temperature;
	const size_t s = 100;

	City cityArray[s];


	for(int i = 0; i < 3; i++)
	{
		cout << "Input city name: ";
		cin >> name;
		cout << "Input city temperature: ";
		cin >> temperature;

		City newCity;
		newCity.name = name;
		newCity.temperature = temperature;

		cityArray[i] = newCity;
	}

	for(int i = 0; i < 3; i++)
	{
		cout << cityArray[i].name << " Temperatur: " << cityArray[i].temperature << endl;
	}

	cin.get();
	cin.get();

}


Now it does list all the temperatures after ive provided all 3 citys BUT it only saves the last city name and list that for all citys.
Last edited on
You need to read up a bit more on C-style string handling. Are not allowed to use std::string instead?
Dont really know if im allowed to use string or not, i assume i should use char* since thats what weve been reading about in the book. we never talked about string.


EDIT !!!

Okey so i fixed the default.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
using namespace std;

//Klass
class City
{
public:
	char* name;
	float temperature;
	City()
	{
		name = "";
		temperature = 0;
	}
	City(char* _name, float _temperature);
};
//Konstruktor

City::City(char* _name, float _temperature)
{
	name = _name;
	temperature = _temperature;
}



int main()
{
	char name[256];
	float temperature;
	const size_t s = 100;

	City cityArray[s];


	for(int i = 0; i < 3; i++)
	{
		cout << "Input city name: ";
		cin >> name;
		cout << "Input city temperature: ";
		cin >> temperature;

		City newCity(name, temperature);

		cityArray[i] = newCity;
	}

	for(int i = 0; i < 3; i++)
	{
		cout << cityArray[i].name << " Temperatur: " << cityArray[i].temperature << endl;
	}

	cin.get();
	cin.get();

}


But it still only saves temperature and not city name, the city name gets replaced by the last one entered.

So my question is, why dosnt it save the name too?
Last edited on
Dont really know if im allowed to use string or not, i assume i should use char* since thats what weve been reading about in the book. we never talked about string.

Well, in that case, as I said, you really need to go back to your books and read up on using C-style strings, because you're making a lot of mistakes at the moment.

But then it sais that theres no default constructor.

A default constructor is just a constructor that doesn't take any arguments. You need one, if you're creating an array of objects.

If you don't define any constructor for your class - which you didn't, in your first version - then the compiler creates a default constructor for you. But in your newest version, you've defined a constructor, so the compiler doesn't create a default one. You'll need to define it yourself.

Do you need to store an array of actual City objects? Or can you use an array of pointers to City objects?
Last edited on
Okey so i contacted my teacher and we were allowed to use string.

So this is what i ended up with.

And it seems to be working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
using namespace std;

//Klass
class City
{
public:
	string name;
	float temperature;
	City()
	{
		name = "";
		temperature = 0;
	}
	City(string _name, float _temperature);
};
//Konstruktor

City::City(string _name, float _temperature)
{
	name = _name;
	temperature = _temperature;
}



int main()
{
	string name;
	float temperature;
	const size_t s = 100;

	City cityArray[s];


	for(int i = 0; i < 3; i++)
	{
		cout << "Input city name: ";
		cin >> name;
		cout << "Input city temperature: ";
		cin >> temperature;

		City newCity(name, temperature);

		cityArray[i] = newCity;
	}

	for(int i = 0; i < 3; i++)
	{
		cout << cityArray[i].name << " Temperatur: " << cityArray[i].temperature << endl;
	}

	cin.get();
	cin.get();

}
Cool. It's much easier working with std::string than with C-style char arrays.
Topic archived. No new replies allowed.