Thread: How can I improve my C program?

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    18

    How can I improve my C program?

    Hi all. I am fairly new to programming. I started with C since I was a EE student. I have a simple C program that needs to be improved by I don't know what else I could do.

    Here's the code (I'm using VS C++ to compile)
    Code:
    #include "stdafx.h"
    
    int salary = 0;
    int fuel = 0;
    double toll = 0;
    int food = 0;
    double balance = 0.00;
    
    int calBalance(void);
    
    int main()
    {
        int workDays = 0;
    
        puts("My console in C to calculate my budget. Every component except salary takes DAILY use.\n");
    
        printf("Working Days: ");
        scanf("%d", &workDays);
    
        printf("Salary: ");
        scanf("%d", &salary);
    
        printf("Fuel: ");
        scanf("%d", &fuel);
    
        fuel = fuel * workDays;
    
        printf("Toll: ");
        scanf("%lf", &toll);
    
        toll = toll * workDays;
    
        printf("Food: ");
        scanf("%d", &food);
    
        food = food * workDays;
    
        calBalance();
        printf("\nBalance: %.2f", balance);
             
        puts("");    
    
        return 0;
    }
    
    int calBalance()
    {
        balance = salary - (fuel + toll + food);
      
        return balance;
    }
    I think I don't need to explain the code since it is very straightforward. I feel like it can't be improved in the context of C(not C++).

    Any suggestion?

    Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,057
    Well it can be.

    1. Remove stdafx.h and include the proper header files like stdio.h
    In conjunction with that, you probably need to turn off precompiled headers in your IDE (project->settings->compiler....)

    2. Move all your global variables into main.

    3. Make your function take parameters.

    4. Make sure your source files are named .c, and not .cpp (the default for your IDE). It the moment, it's likely you're compiling your code as if it were C++.

    5. The convention is that newlines go at the end of printf strings
    > printf("\nBalance: %.2f", balance);

    Then I suppose you could start adding error checking to all those scanf calls to make sure the user isn't being silly.
    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.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    In addition to the above pay attention to the type conversions. For example your balance variable is double, but calBalance function is returning int.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  4. #4
    Registered User
    Join Date
    Sep 2017
    Posts
    18
    Quote Originally Posted by Salem View Post
    2. Move all your global variables into main.
    If I put them into main, then my function
    Code:
    calBalance()
    would not recognize the variable.

    Quote Originally Posted by Salem View Post
    3. Make your function take parameters.
    You mean take every each of the variables? Wouldn't it be too much if I have around 10 variables?

    P/S: Sorry for the double post. When I first posted the question, it did not tell me that it needed approval. The page directed me to the forum and I didn't see my thread. I thought I did not click "Submit new thread" properly or something so I posted it again.

  5. #5
    Registered User
    Join Date
    Sep 2017
    Posts
    18
    Quote Originally Posted by vart View Post
    In addition to the above pay attention to the type conversions. For example your balance variable is double, but calBalance function is returning int.
    Thanks @vart, I just realized that.

  6. #6
    Registered User
    Join Date
    Sep 2017
    Posts
    18
    Okay so this is what I got now,
    Code:
    #include "stdio.h"
    
    double calBalance(int wsalary, int wfuel, double wtoll, int wfood);
    
    int main()
    {
        int salary = 0;
        int fuel = 0;
        double toll = 0;
        int food = 0;
        double balance = 0.00;
        int workDays = 0;
    
        puts("My console in C to calculate my budget. Every component except salary takes DAILY use.\n");
    
        printf("Working Days: ");
        scanf("%d", &workDays);
    
        printf("Salary: ");
        scanf("%d", &salary);
    
        printf("Fuel: ");
        scanf("%d", &fuel);
    
        fuel = fuel * workDays;
    
        printf("Toll: ");
        scanf("%lf", &toll);
    
        toll = toll * workDays;
    
        printf("Food: ");
        scanf("%d", &food);
    
        food = food * workDays;
    
        balance = calBalance(salary, fuel, toll, food);
        printf("\nBalance: %.2f", balance);
        
            
        puts("");    
    
    
        return 0;
    }
    
    double calBalance(int wsalary, int wfuel, double wtoll, int wfood)
    {
        double wBalance = 0.00;
        wBalance = wsalary - (wfuel + wtoll + wfood);
        
        return wBalance;
    }
    Is it even appropriate practice to put as much parameters in a function? Well, for now I have 4, but I might have around 5~6 more.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,057
    Well if you have many related variables, you would put them in a structure. But I guess you haven't reached that far yet.

    As an exercise, your function is fine, but it only has one functional line. Actual examples typically do more work.
    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.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    For simple types it is not so important, but for classes it could make a great difference - so it may be good to get into habit from the begging - prefer op= like a *= b to the a = a * b

    for example instead of
    Code:
    food = food * workDays;
    write
    Code:
    food *= workDays;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to improve this program
    By raj21 in forum C Programming
    Replies: 1
    Last Post: 11-09-2015, 02:27 PM
  2. How can I improve this program?
    By WHOLEGRAIN in forum C++ Programming
    Replies: 14
    Last Post: 03-06-2011, 02:53 AM
  3. Replies: 2
    Last Post: 05-02-2010, 01:49 AM
  4. help to improve program efficiency and speed
    By godhand in forum C Programming
    Replies: 11
    Last Post: 10-19-2003, 04:25 PM
  5. improve program.
    By emperor in forum C Programming
    Replies: 5
    Last Post: 01-05-2003, 01:34 AM

Tags for this Thread