Can´t delete value from vector

I seem to do this right but I can´t delete the desired value from vector by cin. Don´t know why. What have I done wrong?

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
77
78
79
80
81
82
83
84
85
86
87
  #include <iostream>
#include <vector>
#include <numeric>

using namespace std;

int sizeOfVector = 10;

int main()
{
	vector<int>vectorValues(sizeOfVector);

	int i = 0;
	double sum = 0; 
	double averageValue = 0;
	int deleteFromVector;

	while(true)
	{
		cout << " - Press 1 to add a temperature measurement" << endl;
		cout << " - Press 2 to print all temeratures and average temperature" << endl;
		cout << " - Press 3 to delete temperature measurement" << endl;
		cout << " - Press 4 to quit" << endl;

		int choice;
		cin >> choice;

		if (choice == 1)
		{
				// Initiating loop, vector size is zero
				// if i less than ten count up

				for (vector<int>::size_type i=0; i < sizeOfVector; i++)
				{
		
				// Flush makes sure that the buffer is cleared and the characters are written to their destination
				cout << "Enter temperature to vector #" << i+1 << ": " << flush;
		
				// Enter values into vector
				cin >> vectorValues[i];	
				}
		}
		
		else if (choice == 2)
		{
				for (int i = 0; i < sizeOfVector; ++i)
					{				
						cout << "The measured values are: " << vectorValues[i] << endl;
						sum = sum + vectorValues[i];
					}

					cout << "The sum is: " << sum << endl;
					
					averageValue = (sum / sizeOfVector);

					cout << "Average temperature is: " << averageValue << endl;

					
		}
		else if (choice == 3)
		{
					cout << "Which index do you want to delete? (1-10)" << endl;

					for (int i = 0; i < sizeOfVector; ++i)
					{				
						cout << i << "The measured values are: " << vectorValues[i] << endl;
					}

					cin >> deleteFromVector;

					if(deleteFromVector == vectorValues[i])
					{
						// erase the element
						vectorValues.erase(vectorValues.begin() + deleteFromVector);
						
						cout << "vectorValues:";
						cout << i << " " << vectorValues[i] << endl;
					}
		}
		else if (choice == 4) 
		{
			cout << "Out of measurement vector limit!" << endl;
		}
	}

	return 0;
}
Maky your mind, do you want user to input value to delete, or index?
What is the value of i on line 71? Is that a valid index?

You ask for index, but
a) do not validate (I could write 42)
b) compare "index" to "value" (If I want element 7 dead, but some value is 104 degrees ...)
closed account (SNybRXSz)
After the for loop things don't seem to make sense. It looks like you are asking for the value that you want deleted but it looks like i would just be the index of the last number in the vector.
It looks like you might want to put that whole thing into a for loop and test for each index. But of you did that any duplicates would all be erased. So you might want to make it so it deletes the specific index which would be simpler to code anyway.

I will look again when I get on my laptop (I am viewing this from a phone...)
Last edited on
I want to compare the cin to the same index-value I want to delete. So my thought is that I input the same index that I want to delete. And delete the value on the index I want.
index-value
So index or value?
And delete the value on the index I want.
You cannot delete a value. Or index. You delete element on index X or element which contains value Y.

As you stated that you want user to input index:
1) Delete line 71. It is useless
2) You might want to check if entered index is within vector
3) Lines 76-77 should be in the loop like lines 64-67.
4) Remove line 13. If not for it, compilation error would point your mistakes earlier.
closed account (SNybRXSz)
Okay so I just looked over the code again on my laptop.

At the end of the for loop (64-67) you end up with i equal to the size of your vector.

71 is giving you problems. You are comparing the index you want to delete with the value of the last number in the vector. These obviously don't match up, this means none of that crucial code gets run.

If you want the input to be the index number rather than the value at the index you should change the if statement on line 71 to something like this:

 
if(deleteFromVector <= sizeOfVector)


This way you are checking to make sure its within the range of sizes.
If you do this you will want to remove the two i related lines. They wont be necessary.

If you want to compare the input to the actual values of the vector then you would need a loop like the for loop from before to check each entry for a match.
You might want something like this:

1
2
3
4
5
6
7
for (int i = 0; i < sizeOfVector; ++i)
{
	if(deleteFromVector == vectorValues[i])
	{
		//element erasing code goes here (probably what you had before)
	}
}


Hope this helps. I am pretty new to all this myself so if I did something stupid thats why...
closed account (SNybRXSz)
I re did that whole section so it works a bit better. This should work fine for you and it's a bit more polished looking as well. You will want to add 2 more functions though (unless this is homework and you don't care :D), one to view one entry, and one to change one entry. Those would be easy to add and very useful. Without them deleting one entry makes no sense.

Here is my version of the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
else if (choice == 3)
{
      cout << "Which index do you want to delete? (1-10)" << endl;
      
      for (int i = 0; i < sizeOfVector; ++i)
      {
            cout << i+1 << ": The measured values are: " << vectorValues[i] << endl;
      }
            
      cin >> deleteFromVector;
            
            
      if(deleteFromVector <= sizeOfVector)
      {
            // erase the element
            cout << "Deleted index " << deleteFromVector << ": " << vectorValues[deleteFromVector-1] << endl << endl;
                
            vectorValues.erase(vectorValues.begin() + deleteFromVector);
                
      }
}
Thank you, now I know what i did wrong and thanks for the Little code fix. A problem might be Printing the vector after deleting values. Don´t know how to fix this.

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <vector>
#include <numeric>

using namespace std;

int sizeOfVector = 10;

int main()
{
	vector<int>vectorValues(sizeOfVector);
	int i = 0;
	double sum = 0; 
	double averageValue = 0;
	int deleteFromVector;

	while(true)
	{
		cout << " - Press 1 to add a temperature measurement" << endl;
		cout << " - Press 2 to print all temeratures and average temperature" << endl;
		cout << " - Press 3 to delete temperature measurement" << endl;
		cout << " - Press 4 to quit" << endl;

		int choice;
		cin >> choice;

	
		switch(choice)
		{

		case 1:
		{
				// Initiating loop, vector size is zero
				// if i less than ten count up

				for (vector<int>::size_type i=0; i < sizeOfVector; i++)
				{
		
				// Flush makes sure that the buffer is cleared and the characters are written to their destination
				cout << "Enter temperature to vector #" << i+1 << ": " << flush;
		
				// Enter values into vector
				cin >> vectorValues[i];	
				}
		
		}
		break;		

		case 2:
		{
				for (int i = 0; i < sizeOfVector; ++i)
					{				
						cout << i+1 << ": The measured values are: " << vectorValues[i] << endl;
						sum = sum + vectorValues[i];
					}

					cout << "The sum is: " << sum << endl;
					
					averageValue = (sum / sizeOfVector);

					cout << "Average temperature is: " << averageValue << endl;

					
		}
		break;
		case 3:
		{
			cout << "Which index do you want to delete? (1-10)" << endl;
      
			for (int i = 0; i < sizeOfVector; ++i)
			{
				cout << i+1 << ": The measured values are: " << vectorValues[i] << endl;
			}
            
			cin >> deleteFromVector;
            
            
			if(deleteFromVector <= sizeOfVector)
			{
				// erase the element
				cout << "Deleted index " << deleteFromVector << ": " << vectorValues[deleteFromVector-1] << endl << endl;
                
				vectorValues.erase(vectorValues.begin() + deleteFromVector);
                
				}

		}
		break;

	    default:
		{
			cout << "Out of measurement vector limit!" << endl;
		}
		break;

		}
		
	}

	return 0;
}
closed account (SNybRXSz)
I am not sure what problem you mean, but the only one I can see is that when I compile the program it does not delete the vector. This could be made a lot easier by making it replace the vector rather then erasing it. This way you also maintain your 10 variables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
for (int i = 0; i < sizeOfVector; ++i)
{
	cout << i+1 << ": The measured values are: " << vectorValues[i] << endl;
}
			
cout << "Which index do you want to change? (1-10)" << endl;
      
cin >> deleteFromVector;
			
cout << "What number do you want to replace it with?" << endl;
			
cin >> replacementNumber;
            
            
if(deleteFromVector <= sizeOfVector)
{
	cout << "Replaced index " << deleteFromVector << ": " << vectorValues[deleteFromVector-1] << " with " << replacementNumber << endl << endl;
                
	// replace the element
	vectorValues[deleteFromVector-1] = replacementNumber;
                
}

You would also need to add int replacementNumber; and you would want to change line 21 (22 with the new int) to cout << " - Press 3 to change a temperature measurement" << endl; so it matches up.
The entire separate sizeOfVector is an unnecessary hazard. The std::vector has 'size' property. When you erase or insert, that size does change automatically.

Furthermore, the logic of the program ...
Option 1 assigns sizeOfVector values to vector. Set all, not add.
Option 3 removes single value.

Is that some kind of "calculate average, but be able to drop outlier(s)" routine?
No, its just an exercise i found in my book. Yes it calculates the average value and should be able to drop out a value. But the program quits when I use Case 3. After it seems to quit and I can´t print the new average value. Why I don´t know.
Topic archived. No new replies allowed.