Letting the user add and remove data from an array

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?

cheers!

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
#include <iostream>
#include <cstring>
using namespace 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 = new float [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: 
	}

	else if (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];
			}
	}

	else if (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.
Topic archived. No new replies allowed.