I am writing a program which reads in a database of albums into an array of structs, the data can then be deleted, modified etc. The problem i am having is that when i try to modify a specific record using a search term and then strcmp to the album name in the array, the only time i can modify is if i enter the name of the first record. The loop dosnt seem to traverse the array past the first record compare the input string to the strings in the array.

Without posting all of my code, here is the function

void YourModifyStockLevel()
{
	char searchName[31];
	int stock = 0;

	cout << "Enter album to modify: ";
	cin.clear();
	cin.ignore(100, '\n');
	cin.getline(searchName, 32);
	cout << endl;

	
	for(int a = 0; a < numrec -1; a++)
	{
		if(strcasecmp(cdstock[a].albumName, searchName) == 0 )
		{
			cout << "Enter new stock level: ";
			cin >> stock;
			if (stock < 0)
			{
				cout << "Error! stock level cannot go below zero" << endl;
				cout << "The stock level has been set to 0" << endl;
				cdstock[a].stock = 0;
				return;
	
			}
			else
			{
				cdstock[a].stock = stock;
				return;
				
			}
			return;	
		}
		else
		{
			cout << "Album is not in the database" << endl;
			return;
		}
		
	}
		
}

I cant see where the problem is, and i have used the same style of code, but in different parts of the program to do things like print and modify different parts of the records.

Recommended Answers

All 2 Replies

Here's your loop with all of the extra stuff removed. The problem is easier to see here:

for(int a = 0; a < numrec -1; a++)
{
	if(strcasecmp(cdstock[a].albumName, searchName) == 0 )
	{
		...
		return;	
	}
	else
	{
		...
		return;
	}
}

The loop never goes beyond the first element of the array because you return from the function in all paths of the if statement.

thanx, that worked!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.