How to improve code

This is a discussion on How to improve code within the C Programming forums, part of the General Programming Boards category; hi guys looking to improve/check some code! trying to build a structure called read data void read_data (struct comp_detail * ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    4

    Exclamation How to improve code

    hi guys looking to improve/check some code!
    trying to build a structure called read data

    void read_data (struct comp_detail * pt_comp_detail)
    {
    struct comp_detail new;
    printf("\nEnter Competitors Name: ");
    gets(name);
    printf("\nEnter Elapsed Time: ");
    scanf("%.2f", &new.etime);
    printf("\nEnter The number of faults: ");
    scanf("%d", &new.faults);
    return(new);
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    4
    sorry newly taged version


    void read_data (struct comp_detail * pt_comp_detail)
    {
    struct comp_detail new;
    printf("\nEnter Competitors Name: ");
    gets(name);

    printf("\nEnter Elapsed Time: ");
    scanf("%.2f", &new.etime);

    printf("\nEnter The number of faults: ");
    scanf("%d", &new.faults);

    return(new);
    }

  3. #3
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    void read_data (struct comp_detail * pt_comp_detail)
    You return a value at the end, void is not the right return type. If it is then you shouldn't return anything. I'm not sure if you want to make a new comp_detail and return that, or just put data into pt_comp_detail, so I'll answer assuming it's the latter.
    gets(name);
    Where is name defined? I'll assume it's a part of struct comp_detail since that makes the most sense. Also, don't use gets, there's no way to make it safe. Use fgets instead to get a line.
    scanf("%.2f", &new.etime);
    Always check the return value of input functions, especially scanf since it's so easy to break and when you do, the brown stuff really hits the fan unless you're ready for it. You also can't use the .2 modifier for scanf, that's only for printing with printf. Try this instead
    Code:
    int read_data (struct comp_detail *pt_comp_detail)
    { 
        printf("Enter Competitors Name: ");
        fflush(stdout);
    
        if (fgets(pt_comp_detail->name, sizeof(pt_comp_detail->name), stdin) != NULL)
        {
            printf("Enter Elapsed Time and number of faults: ");
            fflush(stdout);
    
            if (scanf("%f %d", &pt_comp_detail->etime, &pt_comp_detail->faults) != 2)
            {
                fprintf(stderr, "Some error message\n");
                return 0;
            }
        }
    
        return 1;
    }
    p.s. What the alphabet would look like without q and r.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    4

    cheers mate

    i have posted the whole code on this forum! thanks for the hints!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 01:48 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  4. Replies: 0
    Last Post: 02-21-2002, 06:05 PM
  5. help improve my code
    By lambs4 in forum C Programming
    Replies: 3
    Last Post: 11-21-2001, 11:33 AM

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