'How come C Lang keeps returning a very small negative number
I have tried %.02d and %02d 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;
}
Solution 1:[1]
There are some issues in your code:
- Never use
gets()again in any of your programs.fgets()is a good alternative. I suggest you read this article. - Avoid global variables unless you really need to. In your case, you don't.
- The proper format specifier to parse
floats is%f, and%lffordoubles (lf stands for long float, if I still remeber well). - Don't use
doubles orfloats 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(" %99[^\n]", mealChoice);
printf("Enter the cost of meal: \n");
get_double(&mealCost); // scanf(" %lf", &mealCost);
subTotal = mealCost + (mealCost * HST);
total = subTotal + ECO_DEPOSIT;
printf("\nYour order: %s\n", mealChoice);
printf("\nTotal cost: $%.2f\n", total);
printf("\nThank you for your support!\nCome again!\n");
}
get_string():
bool get_string(char *in, const size_t size)
{
if (!fgets(in, size, stdin))
return false; // fgets failed.
size_t last = strcspn(in, "\n");
in[last] = '\0';
return true;
}
get_double():
bool get_double(double *d)
{
char in[100];
if (!get_string(in, sizeof in))
return false;
return sscanf(in, "%lf", d) == 1;
}
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 | Zakk |
