Home > Back-end >  How to get the total?
How to get the total?

Time:04-30

Good day, I am practicing about Arithmetic and started to experiment a bit and got confused. How can I total the liter, refueled, and tendered?

#include <stdio.h>
int main() 
{
    int liter, refueled, tendered, changed;
  
    printf("Enter the price of the fuel per liter: ");
    scanf("%f",&liter);
  
    printf("Enter the number of liters the customer wants refueled: ");
    scanf("%f",&refueled);
  
    printf("Enter the amount tendered: ");
    scanf("%f",&tendered);
  
  
    printf("your change is %.2f pesos. Thank you and come again!",  changed = liter * refueled - tendered);
  
    return 0;
}

CodePudding user response:

  1. It seems like float is more appropriate for all your variables (liter, refueled, tendered, changed). There's no reason to allow only integer amounts of fuel etc. You also scanfed them using "%f" which is used for floats.
  2. It's better to assign changed in a separate line, or alternatively get rid of it altogether and simply put the mathematical expression in the printf call.
  3. You inverted the value of changed. Should be tendered - liter * refueled.

Better version:

#include <stdio.h>

int error_handler(/*error params*/)
{
    int errCode{ 1 };
    // Error handling based on the params, set errCode ...
    return errCode;
}

float get_change(float liter, float refueled, float tendered)
{
    return tendered - liter * refueled;
}

int main()
{
    float liter{ 0 }, refueled{ 0 }, tendered{ 0 };
    printf("Enter the price of the fuel per liter: ");
    if (scanf("%f", &liter) != 1) {
        return error_handler(/*error params*/);
    }
    printf("Enter the number of liters the customer wants refueled: ");
    if (scanf("%f", &refueled) != 1) {
        return error_handler(/*error params*/);
    }
    printf("Enter the amount tendered: ");
    if (scanf("%f", &tendered) != 1) {
        return error_handler(/*error params*/);
    }
    printf("your change is %.2f pesos. Thank you and come again!\n", get_change(liter, refueled, tendered));
    return 0;
}

Update: Following the comments below I updated my solution with:

  1. Skeleton for error handling for scanf.
  2. A separate function for the change calculation. In this case it seems a bit contrived, but it's correct from a methodical software engineering point of view (imagine this calculation getting a lot more complex in some real life scenario). Note: you can further apply this principle and extract for example the code for getting input from the user to a separate function. In my code above I decided it is enough to demonstrate it with get_change and error_handler.

CodePudding user response:

I took the answer of wohlstad, which was nearly perfect and added a function for your calculation...

#include <stdio.h>

fload calculation(int tendered, int liter, int refueled);

int main()
{
    float liter, refueled, tendered, changed;
    printf("Enter the price of the fuel per liter: ");
    scanf("%f", &liter);
    printf("Enter the number of liters the customer wants refueled: ");
    scanf("%f", &refueled);
    printf("Enter the amount tendered: ");
    scanf("%f", &tendered);

    changed = calculation(tendered, liter, refueled);
    
    printf("your change is %.2f pesos. Thank you and come again!", changed);
    return 0;

}


fload calculation(int tendered, int liter, int refueled){
    fload calcVal = tendered - liter * refueled;

    return calcVal;
}

Now you can edit your calculation like you need it, adding more or less things etc...

CodePudding user response:

Your scanning attempts, e.g. scanf("%f",&liter); uses the format specifier for float, but gives the address of an integer.
That causes undefined behaviour, which means "do not do this" for practicals (otherwise look up the term, or for fun "nasal demons").

The simplest fix (assuming you input integer values) is to switch to "%d".

If you instead switch to using float for the variable (probably necessary), you should know, for the cases of currency (e.g. tendered), that a very recommended best practice is to then convert to an integer variable which counts the smallest denomination, e.g. cents.

  •  Tags:  
  • c
  • Related