Home > Back-end >  How come C Lang keeps returning a very small negative number
How come C Lang keeps returning a very small negative number

Time:04-30

I have tried %.02d and d to format the numerical data format but it keeps giving me 6 decimal places to the right. I have searched on the internet but I keep getting the same negative number. Please help. Here is what I have:

  /*
  *Standard C libraries
 */
 #include <stdio.h>
 #include <string.h>
 
 /*
  * Custom Headers
  */
  
  /*
   *Declarations
   */
   char mealChoice[100];
   double total = 0.0;
   double subTotal = 0.0;
   double mealCost = 0.0;
   const double HST = 0.15;
   const double ECO_DEPOSIT = 0.10;
   
   //void calcTotal();
   //void displayMenu(void);
   //double getOrder();
   
  int main(int argc, char *argv[])
  {
       printf("There is one size of hot/cold beverage: 435ml\n\n");
       printf("Choices for hot beverages are: tea, cappachino, coffee\n\n");
       printf("Choices for cold beverages are: orange juice, milk, chocolate milk, bottled water\n\n");
       printf("Food choices for Breakfast: eggs, ham, bacon, toast with jam, and hasbrowns");
       printf("Food choices for Lunch: Pizza, lasagna, mac n cheese with bacon\n\n");
       printf("Food Choices for Dinner: Fish n' chips, sub with onion rings, fries, poutine, shephards pie, chicken alfredo \n\n");
       printf("Prices for Breakfast: $12.50, Lunch: $15.00 and Dinner $35.00\n");
       printf("Enter the customer's choice of Meal \n\n");
       gets(mealChoice);
       printf("Enter the cost of meal: \n");
       scanf("%lf", &mealCost);
       subTotal = mealCost   (mealCost * HST);
       total = subTotal   ECO_DEPOSIT;
       printf("\nYour order:  %s", mealChoice, "\n");
       printf("\nTotal cost:  $ %lf", total, "\n");
       printf("\nThank you for your support!\n");
       printf("Come again! \n");
       
      return 0;
  }

CodePudding user response:

There are some issues in your code:

  1. Never use gets() again in any of your programs. fgets() is a good alternative. I suggest you read this article.
  2. Avoid global variables unless you really need to. In your case, you don't.
  3. The proper format specifier to parse floats is %f, and %lf for doubles (lf stands for long float, if I still remeber well).
  4. Don't use doubles or floats to represent monetary values because of their imprecision. In your use case, it might not have an impact because: (1) you are dealing with small amount of money and (2) you are not doing something serious. But for production code, no. See here.

Here is a working version of your code (using doubles):

int main(void)
{
    char mealChoice[100];
    double total = 0.0;
    double subTotal = 0.0;
    double mealCost = 0.0;
    const double HST = 0.15;
    const double ECO_DEPOSIT = 0.10;
    
    const char *menu =
    "There is one size of hot/cold beverage: 435ml\n"
    "Choices for hot beverages are: tea, cappachino, coffee\n"
    "Choices for cold beverages are: orange juice, milk, chocolate milk, bottled water\n"
    "Food choices for Breakfast: eggs, ham, bacon, toast with jam, and hasbrowns\n"
    "Food choices for Lunch: Pizza, lasagna, mac n cheese with bacon\n"
    "Food Choices for Dinner: Fish n' chips, sub with onion rings, fries, poutine, shephards pie, chicken alfredo\n"
    "Prices for Breakfast: $12.50, Lunch: $15.00 and Dinner $35.00\n";
    
    printf("%s\n", menu); // No need to call printf mutiple times.
    
    printf("Enter the customer's choice of Meal: ");
    get_string(mealChoice, sizeof mealChoice); // scanf("            
  •  Tags:  
  • c
  • Related