Searching URGENT

This is a discussion on Searching URGENT within the C Programming forums, part of the General Programming Boards category; Hi i have written the code below using various sources, mainley sams teach yourself c in 21 days and basicallu ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    7

    Question Searching URGENT

    Hi i have written the code below using various sources, mainley sams teach yourself c in 21 days and basicallu im having real trouble adding a search to myprogram there are tons listed in the book but none seem tobe wot i want Iam after one which i can type in the a car name and it will show all matchs, please help. code is below:


    /* Header files defined */
    #include <stdio.h>

    /* Constants defined */
    #define makemax 50
    #define modelmax 50
    #define yearregmax 10
    #define regmarkmax 10
    #define mileagemax 10
    #define pricemax 10
    #define maxrecords 30

    /* Structed defined */
    struct carrecords
    {
    char make[makemax];
    char model[modelmax];
    char yearreg[yearregmax];
    char regmark[regmarkmax];
    char mileage[mileagemax];
    char price[pricemax];
    int used;

    }record[maxrecords];

    /* Variables defined */
    int i, k, count;
    char quit, option;
    int makel, modell, yearregl, regmarkl, mileagel, pricel;

    /* File Pointer defined */
    FILE *fp;


    /* Function to add records to that database */
    void addrecord()
    {
    if ((fp = fopen("car.data", "a+b"))==NULL) /* Opens the database file for writing to */
    {
    printf("\n\nCannot open file!\n\n"); /* Error statement if database file can not be opened */
    }

    else
    {
    char sync = getchar(); /* Captures carriage return from menu */
    fread(&record, sizeof(struct carrecords), maxrecords, fp); /* Reads database file into structure records */


    for (k=0;k <= maxrecords;k++)
    {
    if (record[k].used & 1) /* Searches through the structure for first available record */
    {
    ;
    }
    else
    {
    break; /* Breaks when it finds an available record. */
    }
    }

    for(i = k; quit != 'q'; i++) /* Starts at that available record to enter record data */
    {

    printf("\n\nEnter details for each car\n\n");

    printf("Make of Car: ");
    fgets(record[i].make, makemax, stdin);
    makel = strlen(record[i].make);
    record[i].make[makel - 1] = '\0';

    printf("Model of Car: ");
    fgets(record[i].model, modelmax, stdin);
    modell = strlen(record[i].model);
    record[i].model[modell - 1] = '\0';

    printf("Year of Registration: ");
    fgets(record[i].yearreg, yearregmax, stdin);
    yearregl = strlen(record[i].yearreg);
    record[i].yearreg[yearregl - 1] = '\0';

    printf("Registration Number: ");
    fgets(record[i].regmark, regmarkmax, stdin);
    regmarkl = strlen(record[i].regmark);
    record[i].regmark[regmarkl - 1] = '\0';

    printf("Car Mileage: ");
    fgets(record[i].mileage, mileagemax, stdin);
    mileagel = strlen(record[i].mileage);
    record[i].mileage[mileagel - 1] = '\0';

    printf("Price: ");
    fgets(record[i].price, pricemax, stdin);
    pricel = strlen(record[i].price);
    record[i].price[pricel - 1] = '\0';

    record[i].used = 1;

    printf("\nPress q to display records or Enter to enter more data:");
    scanf("%c", &quit);
    }
    }
    fwrite(&record, sizeof(struct carrecords), maxrecords, fp); /* Writes the structure to the database file */
    fclose(fp); /* Closes the file */
    return; /* Goes back to the main menu */
    }

    void browserecord()
    {
    if ((fp = fopen("car.data", "r+b"))==NULL)
    {
    printf("\n\nCannot open file!\n\n");
    main();
    }

    fread(&record, sizeof(struct carrecords), maxrecords, fp);

    for(i=0; i<=maxrecords; i++)
    {
    printf("%s, %s, %s, %s, %s, %s\n", record[i].make, record[i].model, record[i].yearreg, record[i].regmark, record[i].mileage, record[i].price);
    sleep(1);
    }

    fclose(fp);

    }


    int main()
    {
    printf("\nCar Database\n");
    printf("===============================\n");
    printf("1. Add Records\n");
    printf("2. Browse Records\n");
    printf("3. Exit Program\n");
    printf("\n");

    while(1)
    {

    printf("Please enter an option 1,2 or 3: ");
    scanf("%s", &option);

    switch(option)
    {
    case '1':
    addrecord();
    break;
    case '2':
    browserecord();
    break;
    case '3':
    exit();
    break;
    default:
    printf("\n\nBad Input. Please try again!\n\n");
    break;

    }
    }

    }
    this is pretty urgent as i need this done by friday

  2. #2
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    Wow, it really must suck when you can't find people to do your homework for you, huh?

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Just a note, urgent for you isn't urgent for us. Be patient and we'll help eventually.
    Code:
    void search ( char *key )
    {
      int i;
      for ( i = 0; i < maxrecords; i++ ) {
        if ( strcmp ( record[i].make, key ) == 0 ) {
          printRecord ( record[i] );
          break;
        }
      }
      printf ( "Record not found\n" );
    }
    Also, in browserecord you call main, you don't want to call main explicitly, it only causes problems. Use the keyword return to go back to main instead.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    7
    i compiled the program after adding in the segement of code and it now says: too few arguments to function 'search' im confused. Also if u would nt mind i would like a search which you can input the search parameters from the menu, if its to much trouble dont worry thanks for the help

    James.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >too few arguments to function 'search' im confused
    You have to pass the search function a key to search by, this is most likely an array that the user fills with a string to search by, such as "Oldsmobile" or "Toyota". The function takes that key and compares it with the data in each record and if the two match it prints the record.

    >like a search which you can input the search parameters from the menu
    Code:
    <snip>
    case 4:
      printf ( "Enter a key to search by: " );
      fgets ( keyArray, sizeof keyArray, stdin );
      search ( keyArray );
      break;
    case 5:
      exit ( 0 );
    <snip>
    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    7
    thanks for the code must appreciated, sorry to kepp bugging you but where it says Key array should i type in carrecords? cause im not usin an array im usin a structure? sorry im just a little confused. and the number, which number goes in there?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Sample output, user input is bolded:
    Code:
    1) Add
    2) Delete
    3) Search
    4) Exit
    Select an option: 3
    
    Please enter a key to search by: Toyota
    Make:  Toyota
    Model: Camry
    Year:  1997
    The key is an array that is used by the user. You ask the user to enter a word that you can compare members of each record with. You don't use the key for anything with your array of structures except to search it.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    7
    okay i sort of get what you mean but im still confused, i have tried simply whacking in the bit of code but that does nt work, i have tried using some of the names in my structure and that does nt work. please could you show me how to implement the search into my code, im having real trouble gettin it to work.
    Cheers
    James

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    10

    Delete

    Ok , That's For The Search And How Can We Delete ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 09:41 PM
  4. Searching and matching strings: segmentation fault
    By Smola in forum C Programming
    Replies: 18
    Last Post: 07-11-2005, 12:25 AM

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