Search

This is a discussion on Search within the C Programming forums, part of the General Programming Boards category; Help me please: Let's say I have this struct Code: struct Cars { char brand[30]; char model[30]; char serial_number[30]; } ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    19

    Search

    Help me please:

    Let's say I have this struct

    Code:
    struct Cars {
           
           char brand[30];
           char model[30];  
           char serial_number[30];
           } car[30];
    I add the number of cars that i want...(...)

    After, i want to make a search by brand, let's say I want to search for all the Ferrari cars that I added and I want that appear in the search all the model and serial numbers of the Ferraris that I've searched.

    If there is not any car of the brand that I've searched I wan't that appear on the screen "car not found" or something like that.

    I tried a couple of things but i still don't know how to do this kind of things.

    I can't use pointers.

    PS:I invented this example right now.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Code:
    for(i = 0;i < num_cars;++i)
      if(!strcmp(car[i].brand, "Ferrari"))
      {
        // Show details for this car
      }
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Very similar to searching through an array of strings, but in this case, you need to bring the specific struct member name, into it:

    Code:
    struct Cars {
           
           char brand[30];
           char model[30];  
           char serial_number[30];
           } car[30];
    
    for(i=0;i<numOfCars;i++) {
       if((strcmp(car[i].brand, "Ford"))==0)
          printf("Ford: %s %s\n",car[i].model, car[i].serial_number);
    }
    Comparisons will be case sensitive, of course.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    19
    that "Ferrari" that i said was an example.

    I have to input the name that i want to search.

    i tried this before but didn't worked

    Code:
    printf("\nBrand: ");
    scanf("%s",search);
      
    for(j=0;j<i;j++)
      {
            if(strcmp(search,car[j].brand)==0)
            {
              //car details
            }
    Last edited by Gotze; 12-28-2011 at 12:41 PM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need TWO strings in your call to strcmp(). Naturally, the name of the string is also a pointer to the base of the string, but pointers are everywhere in your system anyway, under the hood.

    Code:
     if((strcmp(car[i].brand, "Ford"))==0)   //car[i].brand AND "Ford" are the two strings in my example. 
                                       //your call to strcmp() needs two strings, not one.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    19
    Well the problem wasn't in this piece of code...i had the j(number of cars) declared at 0 and that was why the programm didn't run because it was assuming that there were no cars.
    Stupid mistake

    Thank you to all of you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advanced Search -> Search Multiple Content Types
    By phantomotap in forum Tech Board
    Replies: 2
    Last Post: 05-21-2011, 07:28 AM
  2. Difference Between A Linear Search And Binary Search
    By ImBack92 in forum C Programming
    Replies: 4
    Last Post: 05-12-2011, 08:47 AM
  3. Allowing my search function to search sub directories!
    By Queatrix in forum Windows Programming
    Replies: 10
    Last Post: 09-30-2005, 04:54 PM
  4. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  5. binary search or linear search
    By vajira in forum C Programming
    Replies: 0
    Last Post: 06-05-2003, 12:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21