Hi,
I'm currently working on this assingment where I will create a wheaterdatabase. The user will have the option to add and remove values from the array containing wheaterdata. For this I have created a pointer to the array so that the user can add data. But I want the user to be able to delete data and therefore I tried to delete a certain memoryblock from the array using the pointer and delete. This dont seem to work. Any ideas how I can go around this problem?
#include <iostream>
#include <cstring>
usingnamespace std;
int main()
{
char selection;
float measures [10]; // The array holding the data from the user
int number; // a variabel so that the user can change a specific slot in the array
float sum = 0;
while (selection != 'a' || selection != 'A')
{
float* pointer;
pointer = newfloat [measures];
cout << "[L]ägg till en temperaturmätning" // add temperature measuring
"\n[S]kriv ut alla temperaturer och medeltemperatur" // print all temperatures with temperaturemedium
"\n[T]ag bort temperaturmätning" // delete a temperature measuring
"\n[A]vsluta"; // quite
if (selection == 's' || selection == 'S')
{
cout << "Ange hur många mätningar som gjorts: "; // number of measurings done
cin >> number;
for (int i = 0; i < number; i++)
{
cout << "Ange de olika temperaturerna i decimaltal: " << endl; // state the temperatures in float numbers
std::cin >> measures [i];
}
for (int i = 0; i < number; i++)
{
cout << "Datan som angivits är följande: " << measures [i]; // the data you presented is following:
}
for (int i = 0; i < number; i++)
{
sum = sum + measures [i];
}
cout << "Medelvärdet av detta är: " << sum/number << endl; // the mid-value of this is:
}
elseif (selection == 'l' || selection == 'L')
{
for (int i; i == number; i++)
{
cout << "På vilken plats vill du lägga till temperaturmätningen?: "; // where do you want to add a measuring?
cin >> number;
cout << "Lägg till en temperaturmätning"; // Add a measuring
std::cin >> pointer[number];
}
}
elseif (selection == 't' || selection == 'T')
{
for (int i; i==number; i++)
{
cout << "Vilken temperaturmätning vill du ta bort?"; // Wich measuring do u wanan delete?
cin >> number;
delete pointer [number];
}
}
else
{
cout << "Ogiltigt val!"; // Not a valid choice!
}
}
}
One unrelated suggestion is that you achieve this menu through
switch (Selection)
rather than all these embedded ifs.
Also, when you are adding data to the array you are really just changing the value of the variable. instead of deleting the variable itself you could change it to a null value like 0.