program isn't catching words

Hi peoples

I'm learning c++ as a college course. This class strictly prohibits using strings, so arrays only

Part of our homework was to write a function that reads user input and deletes that item and its corresponding price from their arrays.

The function that I wrote can only tell that an entry needs to be deleted if the entry matches the first one on the list, and I can't figure out why

here is the prototype for the function:
bool removeItem(char itemNames[][MAXCHAR], float itemPrice[], int& size, const char itemName[]);

and here is the function so far:
bool
removeItem (char itemNames[][MAXCHAR], float itemPrice[], int &size,
const char itemName[])
{
int i = 0;
int j = 0;
int remove = 0;
char delItem[CAPACITY];

cout << "please enter the name of the item to be deleted from inventory: ";
cin.getline (delItem, CAPACITY);

for (i = 0; i < size; ++i)
{
remove = (strcmp (itemNames[i], delItem));
if (remove == 0)
{
j = i;
break;
}
else if (remove != 0)
{
cout << delItem << " is not currently in inventory" << endl;
return false;
}
}

for (i = j; i < size; ++i)
{
strcpy (itemNames[i], itemNames[i + 1]);
itemPrice[i] = itemPrice[i + 1];
}
if (remove == 0)
{
size = size - 1;
}
cout << delItem << " has been removed from inventory." << endl;
return true;
}

any assistance would be appreciated!
If you need to see anything else from the program to know where I went wrong, please let me know!

Thanks!
The problem is the return false; inside the loop. You need that outside. Like so:
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
bool
removeItem (char itemNames[][MAXCHAR], float itemPrice[], int &size,
const char itemName[])
{
int i = 0;
int j = 0;
int remove = 0;
char delItem[CAPACITY];

cout << "please enter the name of the item to be deleted from inventory: ";
cin.getline (delItem, CAPACITY);

for (i = 0; i < size; ++i)
{
remove = (strcmp (itemNames[i], delItem));
if (remove == 0)
{
j = i;
break;
}
}

if (remove != 0)
{
cout << delItem << " is not currently in inventory" << endl;
return false;
}
else
{
for (i = j; i < size; ++i)
{
strcpy (itemNames[i], itemNames[i + 1]);
itemPrice[i] = itemPrice[i + 1];
}
size = size - 1;
}
cout << delItem << " has been removed from inventory." << endl;
return true;
}
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
#include <iostream>
#include <cstring>
#include <iterator>

constexpr size_t MAXCHAR {200};
constexpr size_t CAPACITY {20};

bool removeItem(char itemNames[][MAXCHAR], float itemPrice[], size_t& size, const char delItem[]) {
	size_t i {};
	bool remove {};

	for (i = 0; i < size; ++i)
		if (std::strcmp(itemNames[i], delItem) == 0) {
			remove = true;
			break;
		}

	if (!remove) {
		std::cout << delItem << " is not currently in inventory\n";
		return false;
	}

	--size;

	for (size_t j = i; j < size; ++j) {
		strcpy(itemNames[j], itemNames[j + 1]);
		itemPrice[j] = itemPrice[j + 1];
	}

	std::cout << delItem << " has been removed from inventory.\n";
	return true;
}

int main() {
	char items[][MAXCHAR] {"it", "was", "the", "best", "of", "times"};
	float price[] {1, 2, 3, 4, 5, 6};
	auto sz {std::size(items)};
	char delItem[CAPACITY] {};

	static_assert(std::size(items) == std::size(price));

	std::cout << "Please enter the name of the item to be deleted from inventory: ";
	std::cin.getline(delItem, CAPACITY);

	removeItem(items, price, sz, delItem);

	for (size_t i = 0; i < sz; ++i)
		std::cout << items[i] << "  " << price[i] << '\n';

	std::cout << '\n';
}



Please enter the name of the item to be deleted from inventory: was
was has been removed from inventory.
it  1
the  3
best  4
of  5
times  6

Please enter the name of the item to be deleted from inventory: worst
worst is not currently in inventory
it  1
was  2
the  3
best  4
of  5
times  6

> This class strictly prohibits using strings, so arrays only
There's nothing stopping you from using std::string to make the initial prototype.

Solving the problem using std::string, then converting that working program into char arrays is likely simpler than fighting a battle on two fronts at once.

For a start, your half-way point is a working program.
So anything that breaks trying to convert to a char array is purely a char array problem, and nothing to do with the logic in the rest of your program.
Topic archived. No new replies allowed.