2
\$\begingroup\$

I started my cs50 course this week and I will be trying to make little projects alongside to reaffirm what I've learned thus far. Here I have made a very basic budget and savings calculator, I am hoping to increase the complexity as I improve.

Where could I improve this in terms of simplicity & structure/slickness? Also are there ways I can make this more complex and add things into it? thanks!


#include <cs50.h>
#include <stdio.h>

int main(void)

// Prompt for Monthly Income
{
    int Monthly_Income;
    {
        Monthly_Income = get_int("Please enter your monthly income");
    }

// Prompt for total Expenditure
    int Expenditure;
    {
        Expenditure = get_int("Please enter your monthly expenditure");
    }
    int Income_Remaining = (Monthly_Income - Expenditure);
   
    printf("Income remaining %i\n", Income_Remaining);
   
// Prompt for savings per month
  
    int S; 
    {
        S = get_int("Enter your target savings contribution per month");
    }
// Calculate total Discretionary income
    int Discretionary_income = (Income_Remaining - S);
    printf("Discretionary income %i\n", Discretionary_income);
// Calculate Savings total per month
    int i = 0;
    int Total = 0;
    do
    {
        Total = Total + S;
        i++;
        printf("your total savings are %i\n", Total);
        printf("total months %i\n", i);
    }
    while (i < 24);
   

}
\$\endgroup\$
1
  • \$\begingroup\$ @G. Sliepen Ah yes I see, now declared & initialised the variables at the same time, not sure why I did it like that. The redundant comment being the // Prompt for Monthly Income or most of the // comments? I was just practicing including comments really, but fair point. That makes sense I will try splitting them up to make it more efficient. and I don't mean just for the sake of making it complex, just so that I can practice and engrain the knowledge ya know, I don't want to make an overcomplicated program if there's an easier/more efficient way however. \$\endgroup\$
    – user250888
    Oct 23 '21 at 19:06
1
\$\begingroup\$

Declare and initialize variables at the same time

You can simplify the code a lot by declaring and initializing variables at the same time, like so:

int Monthly_Income = get_int("Please enter your monthly income");
int Expenditure = get_int("Please enter your monthly expenditure");

You were already doing this for Income_Remaining and Discretionary_income.

Don't write redundant comments

Avoid writing comments that don't really add any new information. A lot of code is self-explanatory, for example if you see:

int Monthly_Income = get_int("Please enter your monthly income");

It is evident that you are prompting for the monthly income, from both the properly named variable and the text passed to get_int().

Split your program into multiple functions

For larger programs it will become a necessity, but it is good practice to also see if you can split up a small program like this into multiple functions. Even if a function is only called once, it can be beneficial to make it a separate thing with its own name. For example, printing the savings over time could be split off:

static void print_savings_over_time(int monthly_saving) {
    int total = 0;

    for (int month = 1; month <= 24; month++) {
        total += monthly_saving;
        printf("Total savings in month %d: %d\n", month, total);
    }
}

int main(void) {
    ...
    int S = get_int("Enter your target savings contribution per month");
    ...
    print_monthly_savings(S);
}

Adding complexity

Also are there ways I can make this more complex and add things into it?

Yes you can. You should not add complexity for complexity's sake though, follow the KISS principle.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.