Thread: A little help here

  1. #31
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    Dave, that is very nice. Can you explain how that formatting works and why that "== 3" at the end? Thanks.

  2. #32
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by caduardo21
    Dave, that is very nice. Can you explain how that formatting works and why that "== 3" at the end? Thanks.
    Let's see (this may take a few edits)...
    Code:
    return sscanf ( line, "%39[^,],%39[^,],%d", last, first, age ) == 3;
    Attempt to read up to 39 characters (saving room for the null terminator) delimited by a comma from line to where last points.
    Then read and discard the comma.
    Then attempt to read up to 39 characters (again saving room for the null terminator) delimited by a comma to where first points.
    Then read and discard the comma.
    Then attempt to read an integer to where age points.
    If everything were successful, three items were assigned (last, first, and age).

    The return value is then a boolean resulting from the comparison of the number of items assigned (converted) with the number of items expected. If you got 3, 3 == 3 will return 1; otherwise it returns 0.
    Last edited by Dave_Sinkula; 01-28-2005 at 11:36 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #33
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    Thanks, now I get it. Take a look at this, please:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    int spaces(char *);
    void flush_newline(char *);
    int validate_name(char *);
    
    int main()
    {
        char name[40];
        int number_of_names = 5;
    
        printf("Now, start inputing the names. They have to be in the format\n");
        printf("\"last,first,age\" with no spaces and the name string cannot\n");
        printf("exceed 40 characters.\n\n");
    
        while(number_of_names > 0){
            do{
                printf("Name: (%d left) ", number_of_names);
                fgets(name, sizeof(name), stdin);
                flush_newline(name);
            }while(validate_name(name) == 1);
    
            number_of_names--;
        }
    
        return 0;
    }
    
    void flush_newline(char *swap)
    {
        char *p;
    
        if((p = strchr(swap, '\n')) != NULL)
            *p = '\0';
    }
    
    int spaces(char *string)
    {
        int i;
        
        if(string[0] == '\0'){
            printf("Invalid entry.\n\n");
            return 1;
        }
    
        for(i = 0; i < strlen(string); i++){
            if(isspace(string[i])){
                printf("Invalid entry.\n\n");
                return 1;
            }
        }
    
        return 0;
    }
    
    int validate_name(char *name)
    {
        int i;
        int age = 0;
        int comma_count = 0;
        char last_name[40];
        char first_name[40];
        
        if(spaces(name) == 1)
            return 1;
    
        else{
            if(strlen(name) > 40){
                printf("Invalid entry.\n\n");
                return 1;
            }
        }
    
        for(i = 0; i < strlen(name); i++){
            if(name[i] == ',')
                comma_count++;
        }
    
        if((comma_count < 2) || (comma_count > 2)){
            printf("Invalid entry.\n\n");
            return 1;
        }
    
        if((sscanf(name, "%39[^,],%39[^,],%d", last_name, first_name, &age) == 3))
            /* Here I'll start putting the names and ages in some kind of data structure
               but for now I'll just print them out to see if it's working */
            printf("First: %s\nLast: %s\nAge: %d\n", first_name,last_name,age);
    
        else{
            printf("Invalid entry.\n\n");
            return 1;
        }
    
        return 0;
    }
    Is it safe?

  4. #34
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    Ok, I am almost "done" here, but there's this other problem now. When I ask the user how he wants to sort the names, even if he types a valid option (last, first or age), the error message is displayed. Can somebody tell me why this is happening? Thanks.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    int spaces(char *);
    void flush_newline(char *);
    int validate_name(char *);
    int numbers_in_name(char *);
    int validate_sort_by(char *);
    
    int main()
    {
        char name[40];
        char sort_by[6];
        int number_of_names = 1;
    
        printf("Now, start inputing the names. They have to be in the format\n");
        printf("\"last,first,age\" with no spaces and the name string cannot\n");
        printf("exceed 40 characters.\n\n");
    
        while(number_of_names > 0){
            do{
                printf("Name: (%d left) ", number_of_names);
                fgets(name, sizeof(name), stdin);
                flush_newline(name);
            }while(validate_name(name) == 1);
    
            number_of_names--;
        }
    
        do{
            printf("\nThe names can be sorted by either last, first or age. How\n");
            printf("would you like them to be sorted? ");
            fgets(sort_by, sizeof(sort_by), stdin);
            flush_newline(sort_by);
        }while(validate_sort_by(sort_by) == 1);
    
        return 0;
    }
    
    void flush_newline(char *swap)
    {
        char *p;
    
        if((p = strchr(swap, '\n')) != NULL)
            *p = '\0';
    }
    
    int spaces(char *string)
    {
        size_t i;
        
        if(string[0] == '\0'){
            printf("Invalid entry.\n\n");
            return 1;
        }
    
        for(i = 0; i < strlen(string); i++){
            if(isspace(string[i])){
                printf("Invalid entry.\n\n");
                return 1;
            }
        }
    
        return 0;
    }
    
    int validate_name(char *name)
    {
        size_t i;
        int age = 0;
        int comma_count = 0;
        char last_name[40];
        char first_name[40];
        
        if(spaces(name) == 1)
            return 1;
    
        else{
            if(strlen(name) > 40){
                printf("Invalid entry.\n\n");
                return 1;
            }
        }
    
        for(i = 0; i < strlen(name); i++){
            if(name[i] == ',')
                comma_count++;
        }
    
        if((comma_count < 2) || (comma_count > 2)){
            printf("Invalid entry.\n\n");
            return 1;
        }
    
        if((sscanf(name, "%39[^,],%39[^,],%d", last_name, first_name, &age) != 3)){
            printf("Invalid entry.\n\n");
            return 1;
        }
    
        if(numbers_in_name(last_name) == 1)
            return 1;
    
        if(numbers_in_name(first_name) == 1)
            return 1;
    
        if(age > 100){
            printf("Wow, you're old!\n\n");
            return 1;
        }
    
        return 0;
    }
    
    int numbers_in_name(char *name)
    {
        size_t i;
    
        for(i = 0; i < strlen(name); i++){
            if(isdigit(name[i])){
                printf("Invalid entry.\n\n");
                return 1;
            }
        }
    
        return 0;
    }
    
    int validate_sort_by(char *sort_by)
    {
        if(strcmp(sort_by, "last") != 0){
            printf("Invalid entry.\n");
            return 1;
        }
    
         if(strcmp(sort_by, "first") != 0){
            printf("Invalid entry.\n");
            return 1;
        }
    
        if(strcmp(sort_by, "age") != 0){
            printf("Invalid entry.\n");
            return 1;
        }
    
        return 0;
    }

  5. #35
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    Nevermind. I got it

Popular pages Recent additions subscribe to a feed