Thread: Adding records to a 2 dimensional database.

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    33

    Adding records to a 2 dimensional database.

    Hi Guys,

    Im working on an exersiz that requires the user input a name , 4 scores and an ID number for the record being added.

    I had it all going but when I try to add the ID numbers taken from the user and do a check to see if the number is taken ive got a little lost.....

    I wonder if anyone can point out the flaws in my function below?

    Regards as ever
    Colin

    Code:
    void add_new_name(void)
    
    {
    
    int found=0;
    int record_id;
    int loop=0;
    clrscr();
    
    /* Add the name and DOB & ID to the database*/
    
    printf("\n\n\t\t   Enter new name and Grades\n\n");
    printf("Enter new name: ");
    gets(names_database[index].name);
    flush(stdin);
    
    printf("Please enter 4 grade marks in as a percentage value:\n\n");
    scanf("%d%d%d%d",&names_database[index].grade1, &names_database[index].grade2,&names_database[index].grade3,&names_database[index].grade4);
    fflush(stdin);
    
    printf("Please enter an ID number between 100-999\n");
    scanf("%d",&record_id);
    fflush(stdin);
    
    
    while(found=0 && loop < MAX)
    {
    
     if(record_id == names_database[loop].id)
    
    {
    printf("This ID number is taken");
    printf("Please Re-enter an ID number between 100-999\n");
    scanf("%d",&record_id);
    fflush(stdin);
    }
    
    loop++;
    
    }
    
    if(found=0)
    {
    names_database[loop].id = record_id;
    }
    
    /* Increment the student array */
    index++;
    
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> gets(names_database[index].name);
    >> flush(stdin);

    1) don't use gets() - use fgets() to prevent buffer overflow.
    2) fflush() is not intended for input streams - read the FAQ for more reliable ways to do that.
    3) what happens when you enter the function and index > MAX?


    >> while(found=0 && loop < MAX)

    I don't see where 'found' is actually used, but at any rate, you're setting it - not testing it - with the value of zero each iteration. and there's no sense in testing data from 'index' through 'MAX' since there isn't any valid data there anyway.

    >> if(found=0)

    again, this should be ==.

    >> if(record_id == names_database[loop].id)

    what happens if the new 'record_id' corresponds to a value already passed in previous iterations of the loop? for instance, let's say the first 4 id's are:

    98
    21
    72
    35

    suppose the user chooses '72'. when you find that value at position three you prompt for a new number and the user chooses '98' - since you didn't restart loop you'll never know that number is already taken, too.

    >> names_database[loop].id = record_id;

    'loop' is going to be MAX - 1, what you want is the element at 'index'.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    33
    Hi Sebastiani

    Thanks for your reply,

    Im still a little stuck, forgive me Im a newbie to C.....

    Im sure its my loop thats wrong but I cant puzzle it out as yet.

    Here is what I tried after your suggestion, I cant think how to recheck the ID once its has been entered again??

    Perhaps you couls assist me again.

    Thanks again

    Code:
    while(found==0 && loop < MAX)  /*Max is defined as 999*/
    {
    
    if(record_id == names_database[loop].id)
    
    {
    found=1;
    printf("This ID number is taken");
    printf("Please Re-enter an ID number between 100-999\n");
    scanf("%d",&record_id);
    fflush(stdin);
    }
    
    loop++;
    
    }
    
    if(found==0)
    {
    names_database[loop].id = record_id;
    }
    
    /* Increment the student array */
    index++;

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    something like:

    Code:
     int retry = 1; 
     while(retry) 
     {
      retry = 0; 
      loop = 0;
      while(loop < index)  
      {
       if(record_id == names_database[loop].id)
       {
        printf("This ID number is taken");
        printf("Please Re-enter an ID number between 100-999\n");
        scanf("%d",&record_id);
        fflush(stdin);
        retry = 1;
        break; 
       }
      loop++;
      }
     }
    >> names_database[loop].id = record_id;

    again, you're not assigning the id to the proper array element.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    33
    Hi there,
    Thanks for your quick reply,

    Can you tell me what "while(retry)" means??

    Thanks again

  6. #6
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    It is a variable that is set to 1 when the user has to enter the data.

    That way the user will keep getting that message and have to enter the data until they enter valid input.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    conditionals always evaluate the non-zero-ness of a condition. so:

    while(retry)

    is semantically the same as:

    while(retry != 0)
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Registered User
    Join Date
    Nov 2004
    Posts
    33
    Thanks Guys,

    I will see how I get on....Your comments are very appreciated.

    Regards as ever.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading a file into a block
    By mickey0 in forum C++ Programming
    Replies: 19
    Last Post: 05-03-2008, 05:53 AM
  2. SVN Import Causes Crash
    By Tonto in forum Tech Board
    Replies: 6
    Last Post: 11-01-2006, 03:44 PM
  3. Database entry - avoiding duplicate records
    By ChadJohnson in forum Tech Board
    Replies: 2
    Last Post: 02-04-2006, 12:43 AM
  4. Adding Students to a database
    By lofty in forum C++ Programming
    Replies: 3
    Last Post: 01-10-2005, 02:51 PM
  5. Adding new records to a file using a pointer
    By Emporio in forum C Programming
    Replies: 1
    Last Post: 05-03-2002, 01:53 AM