Storing objects in an array

So I'm trying to make this wheaterprogram where the user can enter information about a city wich stores in an array. BUT i cant get it to work proper so now the code is slightly f-up.
plz help!


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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;

class City
{
public:
	char* name;
	float temp;
};

int main()
{
City locations[100]; // The object is created
locations[100].name;
locations[100].temp;// Classmembers defined
char selection; // for the switch
float sum = 0; //for calculation of mid-value
while (true) 
{
cout << "[A]dd a city"
		"\n[P]rint all temperatures and mid-value"
		"\n[D]elete temperature"
		"\n[Q]uiet";
cin >> selection;
switch (selection)
{
case 'a':
	for (int i = 0; i < locations [100]; i++)
	{
	cout << "Add a location: ";
	cin >> locations[i].name;
	cout << "Add a temperature: ";
	cin >> locations[i].temp;
	}
	break;



case 'p':
	for (int i = 0; i < locations[100]; i++)
			{
			cout << "The data you defines is: " << locations[i] << endl;
			}
	for (int i = 0; i < locations[100]; i++)
		{
		sum = sum + locations[i].temp;
		}
	for (int i = 0; i < locations[100]; i++)
		cout << "The mid-value of this is: " << sum / locations[100].temp << endl;
		break;

case 'd':
	for (int i = 0; i < locations[100]; i++)
		{
		cout << "Wich temperature data do you wanna erase?";
		cin >> i;
		locations[i].temp = 0;
		break;
		}
		break;

case 'q':
	return false;
	break;

default:
	cout << "Ogiltigt val!" << endl;
	break;
}
}
}
line 16 is not how you create an object of a class.

write

City myawesomecity;

and then inside your main
myawseomecity.temp = 100;
locations[100].name; This attempts to access a non-existent element in the array (the array goes from element zero to element ninety-nine; there is no element one hundred). It also does nothing with it once it tries to access it.

locations[100].temp;// Classmembers defined Likewise.

Anyway, we need to know more. We're not psychic. What does it do that you don't want it to, and/or what does it not do that you think it should.

cin >> locations[i].name; This is an attempt to write over memory that doesn't belong to you. It will cause a segFault if you 're lucky. Firstly, use a proper C++ string. Secondly, go back to the basics of pointers and learn about them. http://cplusplus.com/articles/EN3hAqkS/

for (int i = 0; i < locations [100]; i++) This is just crazy. What are you trying to do?
Last edited on
Ok the assingment is to create a code that lets the user add City (with a temperature), check all added temperatures and/or delete a temperature.
one way to do this:

use a struct with 2entries
-string cityname
-float temperatur

use a vector of the datatype from your struct

add new elements to the vector
ok, I will have to check struct (never used it) ;)
ok, I will have to check struct (never used it) ;)


A struct is identical to a class except for the default privacy level.
But shouldn't I be able to get a working code if I just fix the problem with the for-loops? As for now, this is where the compiler complains. I have to find another way to index my for-loops and wich object to change. The code looks like this now (think its the same as before?):
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;

class City
{
public:
	char* name;
	float temp;
};
City locations;

int main()
{
City locations[30];
locations[30].name;
locations[30].temp;// Classmembers defined
char selection; // for the switch
float sum = 0; //for calculation of mid-value
while (true)
{
cout << "[A]dd a city"
		"\n[P]rint all temperatures and mid-value"
		"\n[D]elete temperature"
		"\n[Q]uiet";
cin >> selection;
switch (selection)
{
case 'a':
	for (int i = 0; i < locations[30]; i++)
	{
	cout << "Add a location: ";
	cin >> locations[i].name;
	cout << "Add a temperature: ";
	cin >> locations[i].temp;
	}
	break;



case 'p':
	for (int i = 0; i < locations[30]; i++)
		{
		cout << "The data you defines is: " << locations[i] << endl;
		}
	for (int i = 0; i < locations[30]; i++)
		{
		sum = sum + locations[i].temp;
		}
	for (int i = 0; i < locations[30]; i++)
		cout << "The mid-value of this is: " << sum / locations[30].temp << endl;
		break;

case 'd':
	for (int i = 0; i < locations[30]; i++)
		{
		cout << "Wich temperature data do you wanna erase?";
		cin >> i;
		locations[i].temp = 0;
		break;
		}
		break;

case 'q':
	return false;
	break;

default:
	cout << "Ogiltigt val!" << endl;
	break;
}
}
}
for (int i = 0; i < locations[30]; i++)

This says: make a variable i, and set its value to zero, and now repeat this loop until that variable is no longer smaller than the value found in the 30th element of the array named locations.

What is the 30th element in the array named locations? Do you see why this makes no sense? If you don't understand why this makes no sense, I can explain further.
Last edited on
What is the 30th element in the array named locations? Do you see why this makes no sense? If you don't understand why this makes no sense, I can explain further.


Ah I see! Thanks =)
Topic archived. No new replies allowed.