Thread: Review and suggestions of my C program code

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    1

    Review and suggestions of my C program code

    Hello everyone, this is my first post on the forum and I'm starting to write basic C programs. I want to hear feedback on a source code I written and compiled. I want to read if there are any improvements I can make with it.

    Here is my source code:
    Code:
    /**************************************************************
    * Converts yearly fixed income to current income to-year      *
    **************************************************************/
    #include <stdio.h>
    
    
    int main(void)
    {
        float annual, by_week, to_date;
    
    
        printf("How much do you earn annually? ");
        scanf("%f", &annual);
        printf("How many weeks have you worked this so far in this year? [Tip: 12 months = 52 years] ");
        scanf("%f", &by_week);
    
    
        to_date = by_week * (annual / 52);
        printf("Your current income-to-year is $%.2f.\n", to_date);
    
    
        return 0;
    }
    Guys, what do you think?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The main improvements that come to mind would be input validation: check the return value of scanf and check that the values make sense (e.g., that they are positive).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
    12 months = 52 years

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Click_here View Post
    Code:
    12 months = 52 years
    Hey, I think I worked there; it did seem almost like a lifetime the last year.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by stahta01 View Post
    Hey, I think I worked there; it did seem almost like a lifetime the last year.

    Tim S.
    Haha!
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By bennywhere in forum C Programming
    Replies: 16
    Last Post: 10-20-2009, 09:00 PM
  2. code review...
    By dar32 in forum C Programming
    Replies: 10
    Last Post: 10-26-2008, 05:53 AM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. review this code
    By KIBO in forum C Programming
    Replies: 12
    Last Post: 08-14-2007, 02:28 PM
  5. Code review please
    By Brighteyes in forum C Programming
    Replies: 9
    Last Post: 03-29-2003, 06:28 PM

Tags for this Thread