Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am here with a doubt to create a function to check if a string entered by the user already exists in a struct.

I have the following struct to store cars data:

C++
typedef struct
    {
        char brand;
        char car_plate[10];
        char model;
    } stCar cars;


For example when asked to enter the car_plate, i need a function to check if that car_plate already exists in the stCar struct because can not exist two cars with the same car_plate.


I don't know if this will work:

C++
int main()
{
  stCar cars;
  int countCars =0;



C++
void entercar(stCars cars[], int *countCars)
   {

       char car_plate_Aux[10];
       int position;
           do
           {

               printf("\nEnter car plate:");
               fgets(car_plate_Aux);
               findcar_plate(cars,*countCars, car_plate_Aux)
             if (position != -1)
             {
               printf("This car already exists!\n");
           }
       }
       while(position != -1);

       cars[*countcars].car_plate =car_plate_Aux;
   }




C++
char findcar_plate(stCars cars[], int countCars, char car_platetofind)
    {
        int i, position;
    
        position = -1;
    
        for(i = 0 ; i < coutCars ; i++)
        {
            if (cars[i].car_plate == car_platetofind)
            {
                position=i;
                i=countCars;
            }
        }
        return position;


Someone can please help me?
Posted
Comments
PIEBALDconsult 30-Jun-15 17:26pm    
You might want a hash table.
Member 11401516 30-Jun-15 17:29pm    
PIEBALDconsult who i do this? please help me!!!!
Thanks
[no name] 30-Jun-15 18:01pm    
What do you mean "don't know if this will work", try it and find out!
[no name] 30-Jun-15 20:11pm    
You could have worded the question differently. "if a string exists" comes down to string comparison. Test your solution as suggested and then Google "comparing 2 strings in c".

since the length of string only 10 , it is not worth to use hash, try to compare string to string.
instead of using
C++
if (cars[i].car_plate == car_platetofind)


use strcmp to compare the 2 string.

last note (related to security) when you assign the car_plate be sure that you assign only 10 chars not more.
 
Share this answer
 
That wouldn't work for several reasons.
As already noted you cannot compare strings that way, you have to use strcmp[^] (or strncmp) instead.
You never allocate memory for items of the cars array (you didn't even declare it as an array).
As already noted you didn't check the user input (it must fit in the array).
Quote:
cars[*countcars].car_plate =car_plate_Aux
Here you are setting a not-existing array item with the address of a temporary variable (you should have copied the array).
...
 
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