J
Jeffrey Barrett
Can someone tell me why I'm having this problem:
When I select drink 5 and deposit too little money, there's a problem
with the 'difference' variable. Try to deposit $.80 instead of the
full $.85 and see what happens if you then keep entering .01 until you
have added the full .85. The program still asks the user to deposit
$0.00. It is telling me for some reason that 0.850000 - 0.850000 =
5.96046e-008.
Any suggestions?
/*
machine.txt:
------------------
Cola
0.75 20
Ruby Red Blast
1.00 10
Lemon Fizz
0.75 8
Grape Soda
0.90 5
Citrus Flip
0.85 0
Habanero Surprise
0.80 11
-------------------
*/
/* ================================================================ */
#include <stdio.h>
#include <string.h>
#include <math.h>
/* ================================================================ */
#define MAXNUMDRINKS 6
#define STRINGSIZE 25
#define noGood 0
/* ================================================================ */
typedef char String [STRINGSIZE];
typedef enum bool { false, true } bool;
typedef struct drinkRec
{
String brand;
float price;
int quantity;
} drinkRec;
typedef drinkRec drinkList [ MAXNUMDRINKS ];
/* ================================================================ */
void ReadAllDrinks ( int* numDrinks, drinkList drinks );
void PrintDrinkMenu (int* menuChoice, int numDrinks, drinkList
drinks);
void GetNewQuantity (int menuChoice, drinkList drinks);
void GetMoney (int menuChoice, drinkList drinks);
/* ================================================================ */
int main ( )
{
drinkList drinks;
int numDrinks, menuChoice;
double profit = 0;
printf ("Welcome to the Soda Machine.\n");
ReadAllDrinks ( &numDrinks, drinks );
do
{
PrintDrinkMenu (&menuChoice, numDrinks, drinks);
if (menuChoice != 0){
GetMoney (menuChoice, drinks);
if (drinks[(menuChoice - 1)].quantity > 0)
profit = profit + .25;
GetNewQuantity (menuChoice, drinks);
}
}
while (menuChoice != 0);
printf("\n%s%.2f\n","Current Run's Profit: $", profit);
printf("\nBYE BYE\n\n");
return ( 0 );
}
/* ================================================================ */
void ReadAllDrinks(int *numDrinks, drinkList drinks)
{
String dummy;
float dummy2 = 0;
char *line;
FILE *inFile;
int count = 0;
if ((inFile = fopen("machine.txt", "r"))) {
while ((line = fgets(drinks[count].brand, STRINGSIZE, inFile))) {
drinks[count].brand[strlen(drinks[count].brand) - 1] = '\0';
fscanf(inFile, "%f%d",
&drinks[count].price,&drinks[count].quantity);
fgets(dummy, STRINGSIZE, inFile);
count++;
}
*numDrinks = count;
fclose(inFile);
}
else printf( "machine.txt not read\n");
}
/* ================================================================ */
void PrintDrinkMenu (int* menuChoice, int numDrinks, drinkList drinks)
{
int count, result;
String dummy;
printf("\n Drink Price Amount Left");
printf("\n ------------- ----- -----------\n");
for (count = 0; count < numDrinks; count++){
printf("%d. %-19s$%4.2f", (count + 1),
drinks[count].brand,drinks[count].price);
if (drinks[count].quantity < 1)
printf(" %8s","SOLD OUT\n");
else
printf(" %8d\n",drinks[count].quantity);
}
printf("\n0. Exit\n");
do
{
printf("\nPlease enter your selection: ");
result = scanf("%d", menuChoice);
if (result == noGood) /* This error checks for any input other
than an integer*/
scanf("%s", &dummy); /* If input is non-integer, force scanf to
stop looping */
}
while ((*menuChoice > numDrinks) || (*menuChoice < 0)|| (result ==
noGood));
}
/* ================================================================ */
void GetNewQuantity (int menuChoice, drinkList drinks)
{
int prevQuantity;
prevQuantity = drinks[(menuChoice - 1)].quantity;
drinks[(menuChoice - 1)].quantity = prevQuantity - 1;
}
/* ================================================================ */
void GetMoney (int menuChoice, drinkList drinks)
{
int result;
String dummy;
float moreMoneyIn, moneyIn, difference;
if (drinks[menuChoice - 1].quantity > 0){
do
{
printf("\n%s%.2f: $","Please deposit $",drinks[menuChoice -
1].price );
result = scanf("%f",&moneyIn);
if (result == noGood) /* This error checks for any input other
than an float*/
scanf("%s", &dummy); /* If input is non-float, force scanf to
stop looping */
}
while (result == noGood);
printf("\n%s$%.2f\n","You put in ",moneyIn);
difference = (drinks[menuChoice - 1].price) - moneyIn;
while (difference != 0){
if (difference > 0){
do
{
printf("%s%.2f: ","Please deposit an additional $",
fabs(difference));
result = scanf("%f", &moreMoneyIn);
if (result == noGood) /* This error checks for any input
other than an float*/
scanf("%s", &dummy); /* If input is non-float, force scanf
to stop looping */
}
while (result == noGood);
printf("\n%s$%.2f.\n","You put in ",moreMoneyIn);
moneyIn = moneyIn + moreMoneyIn;
difference = ((drinks[menuChoice - 1].price) - moneyIn);
}
if (difference < 0){
printf("%s%.2f","Your change is $", fabs(difference));
difference = 0;
}
}
printf("\n\nThank you, money accepted. Enjoy your drink!\n");
}
else printf("\n%s %s\n",drinks[menuChoice - 1].brand, "is sold out.
Please choose another.");
}
When I select drink 5 and deposit too little money, there's a problem
with the 'difference' variable. Try to deposit $.80 instead of the
full $.85 and see what happens if you then keep entering .01 until you
have added the full .85. The program still asks the user to deposit
$0.00. It is telling me for some reason that 0.850000 - 0.850000 =
5.96046e-008.
Any suggestions?
/*
machine.txt:
------------------
Cola
0.75 20
Ruby Red Blast
1.00 10
Lemon Fizz
0.75 8
Grape Soda
0.90 5
Citrus Flip
0.85 0
Habanero Surprise
0.80 11
-------------------
*/
/* ================================================================ */
#include <stdio.h>
#include <string.h>
#include <math.h>
/* ================================================================ */
#define MAXNUMDRINKS 6
#define STRINGSIZE 25
#define noGood 0
/* ================================================================ */
typedef char String [STRINGSIZE];
typedef enum bool { false, true } bool;
typedef struct drinkRec
{
String brand;
float price;
int quantity;
} drinkRec;
typedef drinkRec drinkList [ MAXNUMDRINKS ];
/* ================================================================ */
void ReadAllDrinks ( int* numDrinks, drinkList drinks );
void PrintDrinkMenu (int* menuChoice, int numDrinks, drinkList
drinks);
void GetNewQuantity (int menuChoice, drinkList drinks);
void GetMoney (int menuChoice, drinkList drinks);
/* ================================================================ */
int main ( )
{
drinkList drinks;
int numDrinks, menuChoice;
double profit = 0;
printf ("Welcome to the Soda Machine.\n");
ReadAllDrinks ( &numDrinks, drinks );
do
{
PrintDrinkMenu (&menuChoice, numDrinks, drinks);
if (menuChoice != 0){
GetMoney (menuChoice, drinks);
if (drinks[(menuChoice - 1)].quantity > 0)
profit = profit + .25;
GetNewQuantity (menuChoice, drinks);
}
}
while (menuChoice != 0);
printf("\n%s%.2f\n","Current Run's Profit: $", profit);
printf("\nBYE BYE\n\n");
return ( 0 );
}
/* ================================================================ */
void ReadAllDrinks(int *numDrinks, drinkList drinks)
{
String dummy;
float dummy2 = 0;
char *line;
FILE *inFile;
int count = 0;
if ((inFile = fopen("machine.txt", "r"))) {
while ((line = fgets(drinks[count].brand, STRINGSIZE, inFile))) {
drinks[count].brand[strlen(drinks[count].brand) - 1] = '\0';
fscanf(inFile, "%f%d",
&drinks[count].price,&drinks[count].quantity);
fgets(dummy, STRINGSIZE, inFile);
count++;
}
*numDrinks = count;
fclose(inFile);
}
else printf( "machine.txt not read\n");
}
/* ================================================================ */
void PrintDrinkMenu (int* menuChoice, int numDrinks, drinkList drinks)
{
int count, result;
String dummy;
printf("\n Drink Price Amount Left");
printf("\n ------------- ----- -----------\n");
for (count = 0; count < numDrinks; count++){
printf("%d. %-19s$%4.2f", (count + 1),
drinks[count].brand,drinks[count].price);
if (drinks[count].quantity < 1)
printf(" %8s","SOLD OUT\n");
else
printf(" %8d\n",drinks[count].quantity);
}
printf("\n0. Exit\n");
do
{
printf("\nPlease enter your selection: ");
result = scanf("%d", menuChoice);
if (result == noGood) /* This error checks for any input other
than an integer*/
scanf("%s", &dummy); /* If input is non-integer, force scanf to
stop looping */
}
while ((*menuChoice > numDrinks) || (*menuChoice < 0)|| (result ==
noGood));
}
/* ================================================================ */
void GetNewQuantity (int menuChoice, drinkList drinks)
{
int prevQuantity;
prevQuantity = drinks[(menuChoice - 1)].quantity;
drinks[(menuChoice - 1)].quantity = prevQuantity - 1;
}
/* ================================================================ */
void GetMoney (int menuChoice, drinkList drinks)
{
int result;
String dummy;
float moreMoneyIn, moneyIn, difference;
if (drinks[menuChoice - 1].quantity > 0){
do
{
printf("\n%s%.2f: $","Please deposit $",drinks[menuChoice -
1].price );
result = scanf("%f",&moneyIn);
if (result == noGood) /* This error checks for any input other
than an float*/
scanf("%s", &dummy); /* If input is non-float, force scanf to
stop looping */
}
while (result == noGood);
printf("\n%s$%.2f\n","You put in ",moneyIn);
difference = (drinks[menuChoice - 1].price) - moneyIn;
while (difference != 0){
if (difference > 0){
do
{
printf("%s%.2f: ","Please deposit an additional $",
fabs(difference));
result = scanf("%f", &moreMoneyIn);
if (result == noGood) /* This error checks for any input
other than an float*/
scanf("%s", &dummy); /* If input is non-float, force scanf
to stop looping */
}
while (result == noGood);
printf("\n%s$%.2f.\n","You put in ",moreMoneyIn);
moneyIn = moneyIn + moreMoneyIn;
difference = ((drinks[menuChoice - 1].price) - moneyIn);
}
if (difference < 0){
printf("%s%.2f","Your change is $", fabs(difference));
difference = 0;
}
}
printf("\n\nThank you, money accepted. Enjoy your drink!\n");
}
else printf("\n%s %s\n",drinks[menuChoice - 1].brand, "is sold out.
Please choose another.");
}