0

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.

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by Cosa
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.