HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMajor

Total income program

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
totalprogramincome

Problem

I have my program working. I just need to redo it a little bit, and it could use some improvements.

I got different % rates depending on what the income and status is.

```
#include
#include
#include
#include

//functions called
float wages_loop();
float other_loop();
float interest_loop();
float dividends_loop();

int dependatnts_loop();

void check_status();
float get_total_income(float wage, float div, float intre, float other, int dep);

int single_total = 0;
int mj_total = 0;
int ms_total = 0;
int sh_total = 0;

//start main
int main(void)
{
char another[10];
char buffer[80][90];

float wages, other_income, interest, dividends, income_tax;
int dependents;

printf("Would you like to start: ");
gets_s(another);

if (another[0] != 'y' && another[0] != 'n')
{
while (another[0] != 'y' && another[0] != 'n')
{
printf("\n\n INCORRECT ANSWER. \n\n");
printf("\n Would you like to start. (y or n)");
gets_s(another);
}
}
while (another[0] == 'y')
{

//add all info together.
wages = wages_loop();
other_income = other_loop();
interest = interest_loop();
dividends = dividends_loop();

//enter dependats
dependents = dependatnts_loop();

//function to indicate the status and other things.
income_tax = get_total_income(wages, other_income, dividends, interest, dependents);

if (income_tax = 0)
{
printf("\n\n\t\t Your income tax OWED is: %.2f \n", income_tax);
}

printf("Would you like to do anoter: ");
gets_s(another);

if (another[0] != 'y' && another[0] != 'n')
{
while (another[0] != 'y' && another[0] != 'n')
{
printf("\n\n INCORRECT ANSWER. \n\n");
printf("\n Would you like anoter. (y or n)");
gets_s(another);
}//end if
}
} //e

Solution

Things you did well:

-
You used the function gets_s, which is an optional C11 function from Annex K. Not many people use this standard yet because it is newer. I was surprised to see it in your code.

-
Your organization of the prototype functions is good.

-
You initialize your variables as soon as you create them in some areas.

Things you could improve:

There is a lot that could be improved in this code, so I doubt I will be able to mention them all.

Preprocessor

-
You include both ` and .

#include 
#include 
#include 
#include 


I couldn't get the code to compile as C code with the
#include in there, so it should be removed.

User-experience

-
You ask the user if he is ready to start.

printf("Would you like to start: ");
gets_s(another);

if (another[0] != 'y' && another[0] != 'n')
{
    while (another[0] != 'y' && another[0] != 'n')
    {
        printf("\n\n INCORRECT ANSWER. \n\n");
        printf("\n Would you like to start. (y or n)");
        gets_s(another);
    }
}


The user initiated your program for a reason. Asking him if he would like start is useless, and can be frustrating to a user. To add onto the annoyance, you then tell the user that his input is "incorrect", if he doesn't input
'y', and you then loop the question. I would remove the whole thing.

Logic

-
Some of your logic can be simplified.

if (status[0] == 'S' && status[1] == 'H')
{
    printf("\n\n CORRECT ANSWER. SH \n\n");
    single_total = single_total + 1;
    check = 1;
}
else if (status[0] == 'S' && status[1] == '\0')
{
    printf("\n\n CORRECT ANSWER. S \n\n");
    single_total = single_total + 1;
    check = 1;
}


Both times you are checking if
status[0] == 'S', so use that as the "master" if test condition. Use the other tests as "children" tests.

if (status[0] == 'S')
{
    if (status[1] == 'H') puts("Correct answer: SH");
    if (status[1] == '\0') puts("Correct answer: S");
    single_total = single_total + 1;
    check = 1;
}


Pull out the code that the original test conditions had in common to the "master" condition, and you have a refined test condition statement!

-
You sometimes have logic that will print out to the console that the logic in the code is wrong.

else
{
    printf("\n\n INCORRECT ANSWER. CODE IS WRONG. \n\n");
}


I would use
assert() instead. If this expression evaluates to 0, this causes an assertion failure that terminates the program. Also, assertions are the right mechanism to use, since those positions in the code would only be reachable due to programmer error, not due to unanticipated runtime conditions.

else
{
    assert(income > 30000);
    adjusted_income = income * .35;
}


Variables

-
You have a lot of "magic numbers" in your code.

if (income  6000 && income  9000 && income  15000 && income  21000 && income  25000 && income  30000)
{
    adjusted_income = income * .35;
}


You should extract all of those numbers to variables in case you have to change them later. Then you only have to change one number in one place, instead of changing the number in multiple different places. What if you missed a place?

-
Your variable
char buffer[80][90] is unused and should be removed.

-
It's generally good practice to initialize all of your non-static variables when possible.

-
Don't use global variables.

int single_total = 0;
int mj_total = 0;
int ms_total = 0;
int sh_total = 0;


The problem with global variables is that since every function has access to these, it becomes increasingly hard to figure out which functions actually read and write these variables.

If you don't rely on global variables, you can pass state around between different functions as needed. That way you stand a much better chance of understanding what each function does, as you don't need to take the global state into account.

Syntax

-
You use too much space when printing to the console. Also, use
puts() instead of printf() in some cases where you are not actually formatting the string but just printing it with a newline character at the end.

-
mj_total = mj_total + 1 can be simplified to mj_total += 1.

Input

-
Your code can't handle the input of a string properly.

How much in other income. ten


It should tell the user that it is unacceptable input and ask for re-entry of the data.

-
Your program can't handle the input of a malformed string properly.

How much in other income. 107t


It should tell the user that it is unacceptable input and ask for re-entry of the data.

-
Your program can't handle the input of a
\n (new-line) character (pressing enter).

How much in other income.


It should tell the user that it is unacceptable input and ask for re-entry of the data.

-
Your program is very unforgiving when asking the status of the user.

printf("\n\nWhat is your Status: ");
gets_s(status);


If you input a lower-case character, it won't be accepted. Use the

Code Snippets

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <cstdio>
printf("Would you like to start: ");
gets_s(another);

if (another[0] != 'y' && another[0] != 'n')
{
    while (another[0] != 'y' && another[0] != 'n')
    {
        printf("\n\n INCORRECT ANSWER. \n\n");
        printf("\n Would you like to start. (y or n)");
        gets_s(another);
    }
}
if (status[0] == 'S' && status[1] == 'H')
{
    printf("\n\n CORRECT ANSWER. SH \n\n");
    single_total = single_total + 1;
    check = 1;
}
else if (status[0] == 'S' && status[1] == '\0')
{
    printf("\n\n CORRECT ANSWER. S \n\n");
    single_total = single_total + 1;
    check = 1;
}
if (status[0] == 'S')
{
    if (status[1] == 'H') puts("Correct answer: SH");
    if (status[1] == '\0') puts("Correct answer: S");
    single_total = single_total + 1;
    check = 1;
}
else
{
    printf("\n\n INCORRECT ANSWER. CODE IS WRONG. \n\n");
}

Context

StackExchange Code Review Q#42387, answer score: 23

Revisions (0)

No revisions yet.