He's initializing them after the declaration
Oh yeah, I see that now. Problem is he's doing it in the wrong order. The weights are getting replaced with zeroes right before the costs are calculated and the receipt is printed on the screen. It should be done at the same time the variables are declared.
I replaced your flushkeyboard with getchar
Doesn't make a difference in the code for me. I actually like the original function a bit better because it should ignore any other junk data before reaching the newline. The actual culprit for his problem was the getWeight() function lacked flushKeyboard() after it's scanf() call. I corrected the problems and this code works for me:
#include <stdio.h>
int getCustomer();
void flushKeyboard(void);
float custOrder(int custNo, float ttlArt, float ttlBeet, float ttlCar, float ttlOrd);
float calcDiscount(float costO);
char showMenu(int custNo);
float getWeight();
float calcShipping(float shipWeight);
int main(void) {
int custNo;
float ttlArt, ttlBeet, ttlCar, ttlOrd;
custNo = getCustomer();
printf("Customer number: %d\n", custNo);
while(custNo != 0) {
custOrder(custNo, ttlArt, ttlBeet, ttlCar, ttlOrd);
}
custNo = getCustomer();
}
// This function is designed to prompt a user for their customer number.
int getCustomer() {
int custNo;
// Local Variable
printf("Enter the customer number (0 to quit):\n");
scanf("%d",&custNo);
flushKeyboard();
while(custNo<0) {
printf("Invalid customer number, please re-enter.");
scanf("%d", &custNo);
flushKeyboard();
}
return custNo;
}
//This function is where the user enters the quantities and calculates the cost of their order.
float custOrder(int custNo, float ttlArt, float ttlBeet, float ttlCar, float ttlOrd) {
float weightA = 0, weightB = 0, weightC = 0, shipCost = 0, shipWeight = 0, discount = 0, w = 0, costA = 0, costB = 0, costC = 0, costO = 0;
char option;
option = showMenu(custNo);
printf("You chose: %c\n", option);
while(option!='Q') {
switch(option) {
case ('A'): {
printf("Enter the weight of artichokes ordered:\n");
w = getWeight();
weightA = weightA + w;
ttlArt = ttlArt + w;
break;
}
case ('B'): {
printf("Enter the weight of beets ordered:\n");
w = getWeight();
weightB = weightB + w;
ttlBeet = ttlBeet + w;
break;
}
case ('C'): {
printf("Enter the weight of carrots ordered:\n");
w = getWeight();
weightC = weightC + w;
ttlCar = ttlCar + w;
break;
}
case ('Q'): {
break;
}
default:
printf("Invalid Option.\n");
option = showMenu(custNo);
option = showMenu(custNo);
}
option = showMenu(custNo);
printf("You chose: %c\n", option);
}
costA = weightA * 1.25;
costB = weightB * 0.65;
costC = weightC * 0.89;
costO = costA + costB + costC;
discount = calcDiscount(costO);
costO = costO - discount;
shipWeight = weightA + weightB + weightC;
shipCost = calcShipping(shipWeight);
costO = costO + shipCost;
printf("===============================================\n");
printf("CUSTOMER NUMBER: %d\n", custNo);
printf("ITEM COST/KG ORDERED COST\n");
printf("===============================================\n");
if(costA>0) {
printf("Artichokes 1.25/kg %.2f kg $ %.2f\n", weightA, costA);
}
if(costB > 0)
printf("Beets 0.65/kg %.2f kg $ %.2f\n", weightB, costB);
if(costC > 0)
printf("Carrots 0.89/kg %.2f kg $ %.2f\n", weightC, costC);
printf("-----------------------------------------------\n");
if(discount > 0)
printf("Discount : $ %.2f", discount);
printf("Shipping Weight : %.2f kg \nShipping Cost : $%.2f\nTotal Order : $%.2f\n\n\n", shipWeight, shipCost, costO);
ttlOrd = ttlOrd + costO;
}
// This function calculates the discount (if any).
float calcDiscount(float costO) {
float discount;
discount = 0;
if(costO > 100.00)
discount = costO *0.05;
return discount;
}
// This function gets the weight of an order (in kg) from the user.
float getWeight() {
float w;
// local Variable
scanf("%f", &w);
flushKeyboard();
while(w<0) {
printf("Invalid Weight, please re-enter.\n");
scanf("%f", &w);
}
return w;
}
// This function displays a menu to the user asking them to select an item to order or exit the program.
char showMenu(int custNo) {
char option;
printf("A - Enter weight of artichokes\n");
printf("B - Enter weight of beets\n");
printf("C - Enter weight of carrots\n");
printf("Q - Exit the ordering process for customer %d\n", custNo);
printf("Enter option:\n");
scanf("%c", &option);
flushKeyboard();
option = toupper(option);
while(option!='A' && option!='B' && option!='C' && option!='Q') {
printf("Invalid option, please re-enter.\n");
scanf("%c", &option);
option = toupper(option);
}
return option;
}
//This function calculates the shipping cost of the order.
float calcShipping(float shipWeight) {
float shipCost;
shipCost = 0.00;
if(shipWeight>20.0)
shipCost = 8.00 + (0.1 * shipWeight);
if(shipWeight>5.0)
shipCost = 10.00;
if(shipWeight>0.0)
shipCost = 3.50;
return shipCost;
}
//This module flushes the keyboard buffer to allow the user to enter multiple consecutive values into a scanf statement.
void flushKeyboard(void) {
int ch; // Dummy variable to read data into.
while((ch = getc(stdin)) != EOF && ch != '\n');
}
Edit: Also, just an exercise for imustnotfear, it would be a better program if you format the printed data nicely, by spacing out weights, costs, etc. evenly. There should also be a way to exit gracefully. I've been forcing it closed with ^C.
Edited by Guest, 09 March 2013 - 03:48 PM.