'Issue with Fabs() and Abs()

My program checks to see if the max price is bigger than the price you spent. If the max price is bigger than the price you spent it turns the absolute value of two floats; but when I use fabs(), returns "Wow, you overspent on that first cupcake...you owe $0.00. Bye!". I also tried abs(), but that didn't work.

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main(int argc, char**argv)
{
    float maxPrice;
    char flavor[20];
    char flavor2[20];
    float price;
    double price2;
    char anotherCupcake[20];
    double newPrice;
    newPrice=maxPrice-price;

   // int idxToDel = 2;
    //memove(&word[idxToDel], &word[idxToDel + 1], strlen(word) - idxToDel);

    printf("Please enter starting budget: ");
    scanf("%f", &maxPrice);
    printf("Enter the flavor of cupcake 1: ");
    scanf("%s", &flavor);
    printf("Enter the price of %s cupcake: $", flavor);
    scanf("%f", &price);

    //determining if you have enough money for a second cupcake
    if(price == maxPrice)
    {
        printf("\nOops! No more money left to spend on anymore cupcakes...enjoy the %s one! Bye!", flavor);
    }
    else if(price > maxPrice)
    {

        printf("\nWow, you overspent on that first cupcake...you owe $%.2lf. Bye!", fabs(newPrice)*1.0);
    }
    else
    {
        printf("Ok, looks like you you have $.2lf left to spend on cupcakes...  Would you like to get another one?", maxPrice - price);
        scanf("%s", &anotherCupcake);
        if(anotherCupcake == "yes")
        {
           printf("Enter the flavor of cupcake 2: ");
           scanf("%s", &flavor2);
           printf("Enter the price of cupcake 2 $: ");
           scanf("%f", &price2);
           printf("Done! Enjoy your %s and %s cupcakes! Bye!", flavor, flavor2);

        }
        else
        {
            printf("Ok then...Enjoy that one %s cupcake! Bye!", flavor);
        }
    }
    return 0;
}


Solution 1:[1]

You have to calculate newPrice with current values of maxPrice-price, not at the beginning when neither maxPrice nor price are initialized. Like that:

      newPrice = maxPrice - price;
      printf("\nWow, you overspent on that first cupcake...you owe $%.2lf. Bye!\n", fabs(newPrice)*1.0);

It'll work like that:

$ ./main
Please enter starting budget: 10
Enter the flavor of cupcake 1: 1
Enter the price of 1 cupcake: $18

Wow, you overspent on that first cupcake...you owe $8.00. Bye!

As a sidenote, you didn't specify what compiler you're using but the minimum set of flags you should use with gcc/clang is -Wall -Wextra -pedantic and fix all warnings.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Arkadiusz Drabczyk