Thread: validation of a string/check if exists

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

    validation of a string/check if exists

    Hey Guys,

    I find myself at your mercy again...

    My function takes a name and some other values along with a registration No, this is in the form of a string of 4 chars the first letter is a char and the following 3 are numeric eg:w123

    I have this in the function checking the users input adn it seems fine.

    Once I have validated that the user input is in the correct format I want to check if the input has already been used and stored in the database, this is where the code runs but does nothing???

    I wonder if any one can see where im going wrong.

    Thanks again
    C

    Code:
    void add_new_student(void)
    
    {
    
    char record_id[4];
    int check_reg=1;
    int result;
    int loop=0;
    
    clrscr();
    
    /* Add the name & ID to the database*/
    
    printf("\n\n\t\t   Enter New Name And Reg No\n\n");
    printf("Enter First Name: ");
    gets(students_database[index].fname);
    fflush(stdin);
    
    printf("Enter Last Name: ");
    gets(students_database[index].sname);
    flush(stdin);
    
    printf("Please enter an 4 letter Reg No eg: w123 \n");
    gets(record_id);
    fflush(stdin);
    
    
    /* Validate Reg Number */
    while(check_reg)
    {
    
    while(!isalpha(record_id[0]) || !isdigit(record_id[1]) || !isdigit(record_id[2]) || !isdigit(record_id[3]))
    {
    printf("Please RE-Enter A Reg No In The Correct Format eg: w123 \n");
    gets(record_id);
    fflush(stdin);
    check_reg = 1;
    }
    
    check_reg= 0;
    
    /* Check if the Reg is taken*/
    
    while(loop < MAX)
    {
    result = strcmp(students_database[loop].reg_no,record_id);
    
    
    while(result==0)
    {
    printf("This Registration Number Is Taken");
    printf("Please Re-enter A Registration Number\n");
    scanf("%c",&record_id);
    fflush(stdin);
    
    
    }
    loop++;
    
    }
    
     if(result==1)
    {
    strcpy(students_database[index].reg_no, record_id);
    
     }
    
    }
    
    printf("Please Enter The Credits \n");
    scanf("%d",&students_database[index].credit);
    fflush(stdin);
    
    
    while(students_database[index].credit <10)
    {
    printf("Credits are between 10-15 please choose again!");
    scanf("%d",&students_database[index].credit);
    fflush(stdin);
    }
    
    while(students_database[index].credit >15)
    {
    printf("Credits are between 10-15 please choose again!");
    scanf("%d",&students_database[index].credit);
    fflush(stdin);
    }
    
    		
    
     /* Increment the array */
    index++;
    menu_controller();
    }

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    check the faq for why gets is bad and also dont use fflush(stdin)
    now i presume that your data is in a txt file.
    if you want to check whether the name already exists, u might want to store the names one on a line.
    try this out.. check the name on one line.. use fread for this. and take in 4 characters at a time..
    do this till it is end of file..if the name is found before, break and return that the name exists. else, you can take in the name..
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You need a heavy dose of FAQ
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    Learning how to indent code is a must as well - say for example pretty much every code post I've made is better formatted than that.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    33
    Quote Originally Posted by PING
    check the faq for why gets is bad and also dont use fflush(stdin)
    now i presume that your data is in a txt file.
    if you want to check whether the name already exists, u might want to store the names one on a line.
    try this out.. check the name on one line.. use fread for this. and take in 4 characters at a time..
    do this till it is end of file..if the name is found before, break and return that the name exists. else, you can take in the name..
    Hey Ping,

    The records are stored in a Struct not an external file.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    33
    Quote Originally Posted by Salem
    You need a heavy dose of FAQ
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    Learning how to indent code is a must as well - say for example pretty much every code post I've made is better formatted than that.
    Hey Salem,

    I keep seeing posts like yours saying dont use fflush or gets

    I showed my lecturer the posts in question and was told to ignore the FAQ as what else could i use to clear the buffer but fflush?

    I aint gonna argue with my tutor as I am a newbie so I have to use it.

    Thanks again

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The give this to your instructor:
    Code:
    void stdin_flush(void)
    {
      int c;
    
      do
      {
        c = getchar();
      } while(c != '\n' && c != EOF);
    }
    Now you can call stdin_flush() every time you need to flush it.
    If you understand what you're doing, you're not learning anything.

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

    still stuck

    Hey Guys,

    Im still stuck with this,

    I have found that when this line runs I check the value of "result " afterwards and it has a value of -119 ,



    Code:
    result = strcmp(students_database[loop].reg_no,record_id);
    Any ideas are welcome.

    Regards as ever
    C

  8. #8
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Things in memory might be a little messed up, try out this to see what crazy stuff might come up:
    Code:
    printf("students_database[loop].reg_no = %s\n", students_database[loop].reg_no);
    printf("record_id = %s\n", record_id);
    result = strcmp(students_database[loop].reg_no,record_id);
    ----EDIT----
    Well maybe things aren't as bad as you thought:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        // Compare 'z' and '0' as strings
        printf("strcmp(\"0\", \"z\") = %d\n", strcmp("0", "z"));
        return 0;
    }
    Yeilds:
    Code:
    -74
    Last edited by Kleid-0; 01-26-2005 at 09:18 PM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > as what else could i use to clear the buffer but fflush?
    If your lecturer is suggesting the use of gets() and scanf() for reading input, then they're creating a problem which justifies their "solution".

    If you ONLY use fgets() for reading input, the need to fflush() pretty much goes away.

    How many years of commercial C programming experience does your lecturer have?
    How many different OS has your lecturer programmed for?
    How many different Compilers has your lecturer used?

    If the answers are 0,1,1, then I guess that answers your questions.

    Has your lecturer even heard about the ANSI-C standard, let alone actually read it?

    Lemme guess, they also use void main
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Nov 2004
    Posts
    33
    Quote Originally Posted by Kleid-0
    Things in memory might be a little messed up, try out this to see what crazy stuff might come up:
    Code:
    printf("students_database[loop].reg_no = %s\n", students_database[loop].reg_no);
    printf("record_id = %s\n", record_id);
    result = strcmp(students_database[loop].reg_no,record_id);
    ----EDIT----
    Well maybe things aren't as bad as you thought:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        // Compare 'z' and '0' as strings
        printf("strcmp(\"0\", \"z\") = %d\n", strcmp("0", "z"));
        return 0;
    }
    Yeilds:
    Code:
    -74

    Hey Kleid,

    Thanks for your reply,

    But sadly im not following .........can you explain a little more .

    Thanks again.

  11. #11
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I shall explain

    The reason you might be getting -119 from this:
    Code:
    result = strcmp(students_database[loop].reg_no,record_id);
    might be because you might be comparing garbage, uninitialized strings. To make sure you aren't comparing garbage, print out strings on output like so:
    Code:
    printf("students_database[loop].reg_no = %s\n", students_database[loop].reg_no);
    printf("record_id = %s\n", record_id);
    result = strcmp(students_database[loop].reg_no,record_id);
    Once the output comes, you will know how the string
    students_database[loop].reg_no
    compares with
    record_id
    It won't fix anything, but you'll know a little more on why result is -119.

    If the strings are not garbage, then it probably means that students_database[loop].reg_no starts with a character that is much less in the ASCII Table than record_id. Make sure you're getting this though, if you're still having problems be sure to think it out, and come back if things need to still get untangled.

  12. #12
    Registered User
    Join Date
    Nov 2004
    Posts
    33
    Quote Originally Posted by Kleid-0
    I shall explain

    The reason you might be getting -119 from this:
    Code:
    result = strcmp(students_database[loop].reg_no,record_id);
    might be because you might be comparing garbage, uninitialized strings. To make sure you aren't comparing garbage, print out strings on output like so:
    Code:
    printf("students_database[loop].reg_no = %s\n", students_database[loop].reg_no);
    printf("record_id = %s\n", record_id);
    result = strcmp(students_database[loop].reg_no,record_id);
    Once the output comes, you will know how the string
    students_database[loop].reg_no
    compares with
    record_id
    It won't fix anything, but you'll know a little more on why result is -119.

    If the strings are not garbage, then it probably means that students_database[loop].reg_no starts with a character that is much less in the ASCII Table than record_id. Make sure you're getting this though, if you're still having problems be sure to think it out, and come back if things need to still get untangled.
    Ok im with you there:

    when i put that code in place it yeilds

    Code:
    students_database[loop].reg_no =  "Nothing/No value appears"
    
    
    printf("record_id = %s\n", record_id); "Shows the correct value"
    
    
    printf("result = %d\n", result);  "Shows -119"
    I thought when i used strcmp that the returned values would be 0 for a match or 1 if not??

    When the program runs the 2 dimentional database set up by the struct is empty so im realy not sure what to do to remedy this.

    so where the code says

    Code:
    result = strcmp(students_database[loop].reg_no,record_id);
    I expected the first run to produce a value of 1 for result??


    Thanks again

  13. #13
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    0 = Exact matching strings
    Positive = The first string is higher on the ASCII table
    Negative = The second string is higher on the ASCII table

    So basically
    Code:
    char *a = "Birds";
    char *b = "Birds";
    if(strcmp(a, b) == 0)
        puts("Strings match");
    else
        puts("Strings do not match");
    Quote Originally Posted by colinuk
    when i put that code in place it yeilds

    Code:
    students_database[loop].reg_no =  "Nothing/No value appears"
    That is bad, no wonder you're getting -119! lol. There is no string is compare. I'm not sure how to help you on why the string students_database[loop].reg_no is empty.

  14. #14
    Registered User
    Join Date
    Nov 2004
    Posts
    33
    That is bad, no wonder you're getting -119! lol. There is no string is compare. I'm not sure how to help you on why the string students_database[loop].reg_no is empty. [/QUOTE]



    Thanks Kleid,

    If you guys cant help it seems I am goosed!!

    Thanks again

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> scanf("%c",&record_id);

    you're passing a char** (the address of a char*) to scanf() and using the format specifier for a single char. also, you're not taking into account the null-terminator for record_id - the array should have at least 5 elements.

    >> When the program runs the 2 dimentional database set up by the struct is empty so im realy not sure what to do to remedy this.

    well, if you have an empty record then you shouldn't be processing it anyway. I'm not sure I understand what you're saying there...
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. set validation
    By jmarsh56 in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2005, 08:49 AM
  2. DATE <TIME.H> data validation
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 08-07-2005, 01:10 AM
  3. File question-Testing if file exists
    By fuh in forum C++ Programming
    Replies: 2
    Last Post: 01-28-2003, 07:11 PM
  4. [C#] Validation class for events?
    By gicio in forum C# Programming
    Replies: 4
    Last Post: 01-03-2003, 12:19 PM
  5. How to see if a file exists with a different extension
    By PCJason in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2001, 07:33 PM