CS50 Week 1 "Half" practice problem is producing unexpected results. What is going wrong here?

150 Views Asked by At

I am currently working on the "Half" practice problem in CS50. I am struggling to get the program to output the correct decimal values. First of all, this is my current solution at the moment:

// Calculate your half of a restaurant bill
// Data types, operations, type casting, return value

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

float half(float bill, float tax, int tip);

int main(void)
{
    float bill_amount = get_float("Bill before tax and tip: ");
    float tax_percent = get_float("Sale Tax Percent: ");
    int tip_percent = get_int("Tip percent: ");

    printf("You will owe $%.2f each!\n", half(bill_amount, tax_percent, tip_percent));
}

// TODO: Complete the function
float half(float bill, float tax, int tip)
{
    float tax_amt = bill * (tax / 100.0);
    float tip_amt = bill * ((float)tip / 100.0);
    float total = bill + tax_amt + tip_amt;
    return total / 2;
}

This is the expected output:

Bill before tax and tip: 12.50
Sale Tax Percent: 8.875
Tip percent: 20
**You will owe $8.17 each!**

However, my program outputs this instead:

Bill before tax and tip: 12.50
Sale Tax Percent: 8.875
Tip percent: 20
**You will owe $8.05 each!**

What appears to be my problem here? Any help appreciated!

0

There are 0 best solutions below