I have this program below, it works fine until it comes to the while loop, it asks me if I want to direct the output to a text file and the program doesnt let me enter a character in this case either yes or no. Can someone tell me whats wrong. Thanks
Code:
#include <stdio.h>
#include <stdlib.h>
#define carrots 1.09
#define artichokes 2.05
#define onions 1.15
int main(void)
{
char ans;
char result;
float nocarrots, noartichokes, noonions, totalcarrots = 0, totalartichokes = 0, totalonions = 0, totalall = 0, onionsweight = 0, artichokesweight = 0, carrotsweight = 0, weight = 0;
float discount = 0;
float shippingCost = 0;
printf("Welcome to YourGreens.com!\n\n");
printf("Our choices & prices for today are as follows:\n\n");
printf("A.Carrots: 1.09 EUROS per KG\n");
printf("B.Artichokes: 2.05 EUROS per KG\n");
printf("C.Onions: 1.15 EUROS per KG\n\n");
do
{
printf("Enter 'a' for carrots, 'b' for artichokes, 'c' for Onions or 'q' to Checkout:\n");
scanf("%c",&ans);
switch(ans)
{
case 'a': printf("Enter amount of Carrots in KG:\n");
scanf("%f",&nocarrots);
totalcarrots = totalcarrots + (nocarrots * carrots);
carrotsweight = carrotsweight + (nocarrots);
break;
case 'b': printf("Enter amount of Artichokes in KG:\n");
scanf("%f",&noartichokes);
totalartichokes = totalartichokes + ( noartichokes * artichokes);
artichokesweight = artichokesweight + (noartichokes);
break;
case 'c': printf("Enter amount of Onions in KG:\n");
scanf("%f",&noonions);
totalonions = totalonions + ( noonions * onions);
onionsweight = onionsweight + (noonions);
break;
}
}
while ( ans != 'Q' && ans != 'q');
weight = weight + (carrotsweight + artichokesweight + onionsweight);
totalall = totalall + (totalcarrots + totalartichokes + totalonions);
printf("THE TOTAL PRICE OF GROCERIES IS: %.2f EUROS\n",totalall);
printf("TOTAL WEIGHT OF GROCERIES IS: %.2f KG\n\n",weight);
if ( totalall >= 100 )
{
discount = totalall*0.05;
totalall = totalall - ( totalall * 0.05);
printf("For orders over 100 euros a 5% discount is given\n");
printf("Your new total is: %.2f EUROS",totalall);
}
if ( weight <= 5 )
{
shippingCost = 6.5;
totalall = totalall + 6.50;
printf("For orders under 5 KG, the shipping costs come around 6.50 EUROS\n");
printf("Your new total is: %.2f EUROS",totalall);
}
if ( weight > 5 && weight < 20 )
{
shippingCost = 14.0;
totalall = totalall + 14.00;
printf("For orders over 5KG & less than 20KG, the shipping costs will come around 14.00 EUROS\n");
printf("Your new total is: %.2f EUROS",totalall);
}
if ( weight >= 20 )
{
shippingCost = 14.00 + (weight/2.0);
totalall = totalall + ( 14.00 ) + ( weight/2 );
printf("For orders of 20KG and exceeding, the shipping costs will be 14.00 EUROS and an added 0.50 EUROS per KG\n");
printf("Your new total is: %.2f EUROS\n\n",totalall);
}
printf("\n\n");
printf("Do you want to transfer output to a text file? y/n? \n");
scanf("%c",&result);
while ( result != 'n')
{
FILE* fout;
fout = fopen("checkout.txt", "w");
fprintf(fout, "bill,\tdiscount,\tshipping cost\n");
fprintf(fout, "$%.2f,\t$%.2f,\t$%.2f\n", totalall, discount, shippingCost);
fclose(fout);
}
return 0;
}