Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working on a database in C that prompts the user to enter a unique identifier code (UID) for a product e.g. an int value of 1, for the first item, 2 for the next, 3...etc..etc...

However, when I try compare each UID the if statement doesn't catch the "error" that another item has the same UID.

E.g.

Item 1 : UID 1
Item 2 : UID 1 // this is an error that I'm working on at the moment

The program should then warn the user that item 2's UID is not unique and to enter a "correct" value until they do so. (this is the recursive call to the function).

The result should then be:

Item 1 : UID 1
Item 2 : UID 2 etc...

I would appreciate any help greatly

What I have tried:

// struct templates omitted
int num_of_items = 0;
// enter item details below
fprintf(stdout, "Enter unique identifier for item %d\n", num_of_items);
scanf("%d", &unique_ID);
fflush(stdin);

inventory[num_of_items].item_ID = unique_ID;

// check if ID is unique for each inventory item
if (inventory[num_of_items + 1].item_ID == inventory[num_of_items].item_ID)
{
    fprintf(stderr, "%s\n", "You must enter a unique identifier");
    add_new_item(inventory);
}
else
{
    // prompt user for values
}
Posted
Updated 15-Apr-17 5:46am
v5

Well no, it won't.
It only checks the element after the one you just inserted to see if that is the same as the one you just stuck in there!

If you want to check then you are going about it the wrong way: check that the ID doesn't exist before you put it in there!

Start by writing a method to locate an ID is in the list.
C++
MyStruct* Find(int ID)
   {
   int i;
   for (i = 0; i < num_of_items; i++)
      {
      if (inventory[i].item_ID == ID)
         {
         return &(inventory[i]);
         }
      }
   return (MyStruct*) 0;   
   }
Now call that, passing it the new "unique" ID and see what it returns. If it returns a null pointer, it's not in the list, and you can add it:
C++
MyStruct* pms = Find(unique_ID);
if (pms == (MyStruct*) 0)
   {
   inventory[num_of_items++] = ... // your new element with the ID.
   }
else
   {
   // Tell him it exists.
   }
 
Share this answer
 
Comments
Konahrik16 15-Apr-17 11:20am    
That makes sense! Thanks!
OriginalGriff 15-Apr-17 11:36am    
The nice bit is that because you return a pointer to any existing item with a given ID, you can use the same method later when you want to locate a record for a user.
Your code is plain wrong, but since it look like HomeWork, no full blowup solution.
Here is how you would organize your code
loop until user have entered a UID
    prompt user for new ID
    get user input
    loop for every item in inventory
        compare item ID with new ID
        if it match, new ID is not unique, ask again
    end of loop
end of loop
insert the new ID in inventory


When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900