trouble removing element of index

This program asked me to be able to add, remove, and display elements of an array. I am having trouble removing the elements in the remove_codes() function. In the for loop I have an if/else statement but it always tells me that the code was invalid even though it was an actual element. I need help in my if statement because I believe what I have is incorrect.

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  #include<iostream>
#include<string>
#include<iomanip>
using namespace std;


void displayMenu();
int getChoice();
void add_codes(int products[], int size, int& number_codes_entered);
void remove_codes(int products[], int& number_codes_entered);
void display_codes(int products[], int& number_codes_entered);

/*

*/
int main()
{
	const int SIZE = 1000;
	int products[SIZE];
	int choice,
		number_codes_entered;
	//char answer;
	do
	{
		displayMenu();
		choice = getChoice();
		switch (choice)
		{
		case 1: add_codes(products, SIZE, number_codes_entered);	//function call to function add_codes
			break;
		case 2: remove_codes(products, number_codes_entered);
			break;
		case 3: display_codes(products, number_codes_entered);
			break;
		default:
			break;
		}
	} while (choice != 4);
	return 0;
}

void displayMenu()
{
	// Display menu of choices
	cout << "\n\t\tThe X, Y & Zilch Company Product Codes Menu Selection" << endl;
	cout << "\n\tPlease make your selection<<endl";
	cout << "\n\t1.  Add Codes";
	cout << "\n\t2.  Remove Codes";
	cout << "\n\t3.  Display Product Codes";
	cout << "\n\tEnter your choice(1-3) or 4 to quit: ";
}

int getChoice()
{
	int choice;
	// Get and validate user's choice
	cin >> choice;
	while (choice < 1 || choice > 4)
	{
		cout << "Choice must be between 1 and 3. Please re-enter: ";
		cin >> choice;
	}
	//Return the choice
	return choice;
}

void add_codes(int products[], int size, int& number_codes_entered) {
	char answer;
	int counter = 0,
		index = 0;
	cout << "\n\t===========================================\n";
	cout << "\n\tAdding Product Codes to X, Y & Zilch Company Database : \n";
	cout << "\n\n\tEnter \" Y \" to add product codes to Database  or \" N \" to quit: ";
	cin >> answer;
	while (answer == 'y' || answer == 'Y') {
		counter++;
		cout << "\tEnter Product " << (counter) << " Code: ";
		cin >> products[index];
		index++;
		number_codes_entered = index;
		cout << "\n\tEnter \" Y \" to add more product codes to Database or \"N \" to quit adding codes :  ";
		cin >> answer;
	}
	cout << "\n\t===========================================\n";
}

void remove_codes(int products[], int & number_codes_entered) {
	int product_code_number;
	int index;
	cout << "What product code number do you want to remove?" << endl;
	cout << "Enter the product code number: ";
	cin >> product_code_number;
	for (int index = 0; index < number_codes_entered; index++) 
	{
		if (products[index] == product_code_number)
		{
			products[index] = products[index + 1];
			number_codes_entered--;
			break;
		}
		else
		{
			cout << "Invalid Product Code Entered." << endl;
			cout << "Re-Enter Valid Product code to be removed: ";
			cin >> product_code_number;
		}
	}
	

}

void display_codes(int products[], int& number_codes_entered) {
	cout << "\n\t===========================================\n";
	cout << "\n\tList of X, Y & Zilch Company Product Codes: \n";
	for (int index = 0; index < number_codes_entered; index++)
	{
		cout << "\t Product Code: " << products[index] << endl;
	}
	cout << "\n\t===========================================\n";
}
Can you have same code more than once?
If yes, then should the remove take out only one of them or all?
If no, you should enforce that in the addition.

Note that you don't "add" as in "append", you "set" as in "overwrite".
Call add_codes() twice. Do you now have codes from both calls, or just from that last call?
Is that how it should be?


Lets look at std::remove http://www.cplusplus.com/reference/algorithm/remove/
- It does remove all occurrences of a value.
- It does move values to new elements to keep the remaining values in continuous range.


If you want to remove just one instance, then.
1. Check whether there is element with that value. For example http://www.cplusplus.com/reference/algorithm/find/
2. If there is, then move remaining elements "one left"
3. Adjust size (the --)
1
2
3
4
5
6
7
8
9
10
auto it = std::find( products, products+number_codes_entered, product_code_number );
if (it != products+number_codes_entered) {
  auto next  = it + 1;
  while ( next != products+number_codes_entered ) {
    *it = *next;
    ++it;
    ++next;
  }
  --number_codes_entered;
}
Last edited on
Hello fonzeyy,

Take a good look at the function:
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
void remove_codes(int products[], int & number_codes_entered)
{
    int product_code_number;
    //int index;  // <--- Not used.
    
    std::cout << "\n\n";

    display_codes(products, number_codes_entered);  // <--- Added.

    cout << "\nWhat product code number do you want to remove?\n";  // <--- Changed.
    cout << "  Enter the product code number: ";
    cin >> product_code_number;

    for (int index = 0; index < number_codes_entered; index++)
    {
        if (products[index] == product_code_number)
        {
            products[index] = products[index + 1];

            number_codes_entered--;

            break;
        }
        else
        {
            cout <<   // <--- Changed.
                "Invalid Product Code Entered.\n"
                "Re-Enter Valid Product code to be removed: ";
            cin >> product_code_number;
        }
    }
}

Notice how the blank lines make a big difference in how the code reads. You are not writing the code for the compiler, which BTW does not cast about spaces, blank lines or comments, you are writing this for someone to read especially you.

Look at the if/else statements and walk through them. Given an array of 5 elements 1 - 5 and say that you want to remove 3. The if statement will compare "products[index] to product_code_number, e.g. it will compare "1 to 3" and return false. So where does it go from there?

Then why do you have the user reenter the same number until the if statement becomes true. And when it does you are just making a copy of the next number not actually deleting that element.

You could set that element to (0) zero and deal with it later or use "std::remove" as keskiverto suggested or write your own code to shift everything to the left.

Another option would be to use a "std::vector" who's "erase" function will remove the element and keep the vector in the correct order. If that is possible to use a vector.

When I put a comment on the else part the function worked. Starting with an array of "1 2 3 4 5" the remove result is "1 2 4 4 5". It works, but I do not believe that is what you want and also "number_codes_entered" is now 1 less than it should be.

Andy
Thanks for the help! I followed your instructions with std::find. Have a good rest of your day.
There is also an issue with add_codes() if it is called more than once. Subsequent uses will overwrite data already entered. Consider:

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
102
103
104
105
106
107
108
109
110
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

void displayMenu();
int getChoice();
void add_codes(int products[], int size, int& number_codes_entered);
void remove_codes(int products[], int& number_codes_entered);
void display_codes(int products[], int number_codes_entered);

int main()
{
	const int SIZE = 1000;

	int products[SIZE];
	int choice {}, number_codes_entered {};

	do {
		displayMenu();
		choice = getChoice();

		switch (choice) {
			case 1: add_codes(products, SIZE, number_codes_entered);
				break;

			case 2: remove_codes(products, number_codes_entered);
				break;

			case 3: display_codes(products, number_codes_entered);
				break;
		}
	} while (choice != 4);
}

void displayMenu()
{
	// Display menu of choices
	cout << "\n\t\tThe X, Y & Zilch Company Product Codes Menu Selection\n";
	cout << "\n\tPlease make your selection\n";
	cout << "\n\t1.  Add Codes";
	cout << "\n\t2.  Remove Codes";
	cout << "\n\t3.  Display Product Codes";
	cout << "\n\tEnter your choice (1-3) or 4 to quit: ";
}

int getChoice()
{
	int choice {};

	// Get and validate user's choice
	while (cin >> choice && choice < 1 || choice > 4)
		cout << "Choice must be between 1 and 4. Please re-enter: ";

	//Return the choice
	return choice;
}

void add_codes(int products[], int size, int& number_codes_entered) {
	char answer {};

	cout << "\n\t===========================================\n";
	cout << "\n\tAdding Product Codes to X, Y & Zilch Company Database : \n";
	cout << "\n\n\tEnter \" Y \" to add product codes to Database  or \" N \" to quit: ";

	while (cin >> answer && answer == 'y' || answer == 'Y') {
		cout << "\tEnter Product " << (number_codes_entered + 1) << " Code: ";
		cin >> products[number_codes_entered++];
		cout << "\n\tEnter \" Y \" to add more product codes to Database or \"N \" to quit adding codes :  ";
	}

	cout << "\n\t===========================================\n";
}

void remove_codes(int products[], int& number_codes_entered)
{
	int product_code_number {};

	cout << "What product code number do you want to remove?" << endl;
	cout << "Enter the product code number: ";

	do {
		cin >> product_code_number;

		for (int index = 0; index < number_codes_entered; index++) {
			if (products[index] == product_code_number) {
				for (int i = index; i < number_codes_entered - 1; ++i)
					products[i] = products[i + 1];

				number_codes_entered--;
				return;
			}
		}

		cout << "Invalid Product Code Entered." << endl;
		cout << "Re-Enter Valid Product code to be removed: ";
	} while (true);
}

void display_codes(int products[], int number_codes_entered) {
	cout << "\n\t===========================================\n";
	cout << "\n\tList of X, Y & Zilch Company Product Codes: \n";

	for (int index = 0; index < number_codes_entered; index++)
	{
		cout << "\t Product Code: " << products[index] << endl;
	}

	cout << "\n\t===========================================\n";
}


Topic archived. No new replies allowed.