Jalcock501
Jalcock501

Reputation: 399

Float output is incorrect

I have created a program that takes an input for a user then lets them enter the amount they want to give that input, however when I print the total and amounts. However this is returing the same value across the board: i.e

Totals: 5.00 food: 5.00 bills: 5.00 travel: 5.00 fags: 5.00

instead of:

Totals: 14.00 food: 2.00 bills: 3.00 travel: 4.00 fags: 5.00

int main(void)
{

float food = 0.00;
float travel = 0.00;
float bills = 0.00;
float fags = 0.00;
float total = 0.00;

float t_food, t_travel, t_bills, t_fags;

char userInput[3];

while(userInput[0] != 'X')
{

    printf("Select option,\nA: Food\nB: Travel\nC: Bills\nD: Fags\nX: Exit\n");
    scanf("%s", userInput);
    if((userInput[0] == 'A') || (userInput[0] =='a'))
    {
        printf("Please enter an amount: ");
        scanf("%f", &food);
        printf("You have entered: %.2f\n", food);
        t_food += food;

    }
    if((userInput[0] == 'B') || (userInput[0] =='b'))
    {
        printf("Please enter an amount: ");
        scanf("%f", &travel);
        printf("You have entered: %.2f\n", travel);
        t_travel += travel;

    }
    if((userInput[0] == 'C') || (userInput[0] =='c'))
    {
        printf("Please enter an amount: ");
        scanf("%f", &bills);
        printf("You have entered: %.2f\n", bills);
        t_bills += bills;

    }
    if((userInput[0] == 'D') || (userInput[0] =='d'))
    {
        printf("Please enter an amount: ");
        scanf("%f", &fags);
        printf("You have entered: %.2f\n", fags);
        t_fags += fags;

    }
    if((userInput[0] =='X') || (userInput[0] =='x'))
    {
        total = t_food + t_fags + t_travel + t_bills;

        printf("Total: %.2f\n", &total);
        printf("Food: %.2f\n", &t_food);
        printf("Travel: %.2f\n", &t_travel);
        printf("Bills: %.2f\n", &t_bills);
        printf("Fags: %.2f\n", &t_fags);
        break;
    }
}
return 0;

}

Any ideas?

Upvotes: 1

Views: 1490

Answers (5)

Sean Reid
Sean Reid

Reputation: 921

In addition to the previous answers regarding printf, you also need to initialise the float variables containing the total values. Something like:

float t_food=0, t_travel=0, t_bills=0, t_fags=0;
...
printf("Total: %.2f\n", total);
printf("Food: %.2f\n", t_food);
printf("Travel: %.2f\n", t_travel);
printf("Bills: %.2f\n", t_bills);
printf("Fags: %.2f\n", t_fags);

Upvotes: 0

milssky
milssky

Reputation: 241

If you delete & in last if code block, all will work fine

Upvotes: 0

Matthias
Matthias

Reputation: 8180

You should use the values in the printf, not the addresses.

Upvotes: 1

Barath Ravikumar
Barath Ravikumar

Reputation: 5836

Change

    printf("Total: %.2f\n", &total);
    printf("Food: %.2f\n", &t_food);
    printf("Travel: %.2f\n", &t_travel);
    printf("Bills: %.2f\n", &t_bills);
    printf("Fags: %.2f\n", &t_fags);

to

    printf("Total: %.2f\n", total);
    printf("Food: %.2f\n", t_food);
    printf("Travel: %.2f\n", t_travel);
    printf("Bills: %.2f\n", t_bills);
    printf("Fags: %.2f\n", t_fags); 

Listen to the compiler when it says,

warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘float *’ [-Wformat]

Upvotes: 7

Lindydancer
Lindydancer

Reputation: 26094

Drop the & in printf, it means that you pass the location of the values, not the values themselves.

Upvotes: 1

Related Questions