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!
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <ctime>
usingnamespace 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':
returnfalse;
break;
default:
cout << "Ogiltigt val!" << endl;
break;
}
}
}
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?
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?):
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <ctime>
usingnamespace 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':
returnfalse;
break;
default:
cout << "Ogiltigt val!" << endl;
break;
}
}
}
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.
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.