I am submitting my assignment for review by pro's to see if you think this code is clean and follows standard programming. The criteria of my assignment are met with my code, but I know someone would have a good suggestion for cleaning it up a bit.
Assignment:
Prompt for clerck to enter current prices for products.
Then display those product prices back.
Use a customer checkout function to prompt the customer for how much of each
item he or she would like to purchase.
If the total is more than $50, a 5% discount is applied.
Prompt user to enter the amount of money the customer has to spend.
Compare that with the total of the purchase and display change or amount due.
My Code
#include<time.h>
#include <stdio.h>
int main(void) //Beginning of program
{
time_t timer;
float flbGrapes1;
float flbPears2;
float flbBellpeppers3;
float flbNectarines4;
float flbPotatoes5;
float fMoney;
float fMoney2;
float ftotLbs1;
float ftotLbs2;
float ftotLbs3;
float ftotLbs4;
float ftotLbs5;
float fTotGrapes1;
float fTotPears2;
float fTotBellpeppers3;
float fTotNectarines4;
float fTotPotatoes5;
float ftotPurch;
float fdisc;
float ftotDollar;
//initialize variables
flbGrapes1 = 0.00;
flbPears2 = 0.00;
flbBellpeppers3 = 0.00;
flbNectarines4 = 0.00;
flbPotatoes5 = 0.00;
fMoney = 0.00;
fMoney2 = 0.00;
ftotLbs1 = 0.00;
ftotLbs2 = 0.00;
ftotLbs3 = 0.00;
ftotLbs4 = 0.00;
ftotLbs5 = 0.00;
timer=time(NULL);
ftotDollar = (flbGrapes1 * ftotLbs1) + (flbPears2 * ftotLbs2) + (flbBellpeppers3 * ftotLbs3) + (flbNectarines4 * ftotLbs4) + (flbPotatoes5 * ftotLbs5);
fdisc = (ftotPurch * .05);
//Greeting and time
printf("Welcome to Reggie's Friendly Neighborhood Grocery Store!\n");
printf("\n");
printf("The current time is %s\n",asctime(localtime(&timer)));
printf("\n");
printf("\n");
//Prompt user to enter price per pound
printf("Please enter the price per pound for your Grapes: $");
scanf("%f", &flbGrapes1);
printf("\n");
printf("Please enter the price per pound for your Pears: $");
scanf("%f", &flbPears2);
printf("\n");
printf("Please enter the price per pound for your Bell peppers: $");
scanf("%f", &flbBellpeppers3);
printf("\n");
printf("Please enter the price per pound for your Nectarines: $");
scanf("%f", &flbNectarines4);
printf("\n");
printf("Please enter the price per pound for your Potatoes: $");
scanf("%f", &flbPotatoes5);
printf("\n");
printf("\n");
//Display price per pound entered
printf("You have entered the following:\n");
printf("\n");
printf("$%.2f a pound for grapes.\n", flbGrapes1);
printf("\n");
printf("$%.2f a pound for pears.\n", flbPears2);
printf("\n");
printf("$%.2f a pound for bell peppers.\n", flbBellpeppers3);
printf("\n");
printf("$%.2f a pound for nectarines.\n", flbNectarines4);
printf("\n");
printf("$%.2f a pound for potatoes.\n", flbPotatoes5);
printf("\n");
printf("\n");
//Ask for the total pounds
printf("How many pounds of grapes would you like to purchase?\n", ftotLbs1);
printf("\n");
scanf("%f", &ftotLbs1);
printf("\n");
printf("The total amount for the grapes is $%.2f: \n", flbGrapes1 * ftotLbs1);
printf("\n");
printf("\n");
printf("How many pounds of pears would you like to purchase?\n", ftotLbs2);
printf("\n");
scanf("%f", &ftotLbs2);
printf("\n");
printf("The total amount for the pears is $%.2f: \n", flbPears2 * ftotLbs2);
printf("\n");
printf("\n");
printf("How many pounds of bell peppers would you like to purchase?\n", ftotLbs3);
printf("\n");
scanf("%f", &ftotLbs3);
printf("\n");
printf("The total amount for the bell peppers is $%.2f: \n", flbBellpeppers3 * ftotLbs3);
printf("\n");
printf("\n");
printf("How many pounds of nectarines would you like to purchase?\n", flbNectarines4 * ftotLbs4);
printf("\n");
scanf("%f", &ftotLbs4);
printf("\n");
printf("The total amount for the nectarines is $%.2f: \n", flbNectarines4 * ftotLbs4);
printf("\n");
printf("\n");
printf("How many pounds of potatoes would you like to purchase?\n", flbPotatoes5 * ftotLbs5);
printf("\n");
scanf("%f", &ftotLbs5);
printf("\n");
printf("The total amount for the potatoes is $%.2f: \n", flbPotatoes5 * ftotLbs5);
printf("\n");
printf("\n");
ftotDollar = (flbGrapes1 * ftotLbs1) + (flbPears2 * ftotLbs2) + (flbBellpeppers3 * ftotLbs3) + (flbNectarines4 * ftotLbs4) + (flbPotatoes5 * ftotLbs5);
//Total dollar amount of all products entered
printf("The total of these products is $%.2f \n", ftotDollar);
printf("\n");
fdisc = (ftotDollar * .05);
ftotPurch = (ftotDollar - fdisc);
ftotDollar = (flbGrapes1 * ftotLbs1) + (flbPears2 * ftotLbs2) + (flbBellpeppers3 * ftotLbs3) + (flbNectarines4 * ftotLbs4) + (flbPotatoes5 * ftotLbs5);
//If total amount is over $50
{
if (ftotDollar > 50.00)
printf("You will receive a 5 percent discount, which totals: $%.2f \n", fdisc);
printf("\n");
if (ftotDollar > 50.00)
printf("The total amount of your purchase is $%.2f \n", ftotPurch);
printf("\n");
}
printf("Enter the amount of money that you will be spending: $");
scanf("%f", &fMoney);
printf("\n");
printf("\n");
{
if (fMoney < ftotDollar)
printf("Amount entered is not enough for purchase, please input a larger amount: $");
scanf("%f", &fMoney);
printf("\n");
if (fMoney > ftotDollar)
printf("Your change is $%.2f \n", fMoney-ftotDollar);
else
printf("Your change is $%.2f \n", fMoney-ftotPurch);
printf("\n");
printf("Thank you for shopping at Reggie's! Have a good day and please visit us again!");
}
{
if (fMoney < ftotPurch)
printf("Amount entered is not enough for purchase, please input a larger amount: $");
scanf("%f", &fMoney);
printf("\n");
if (fMoney > ftotPurch)
printf("Your change is $%.2f \n", fMoney-ftotPurch);
printf("\n");
}
printf("Thank you for shopping at Reggie's! Have a good day and please visit us again!");
getchar();
return 0;
}
by: fridomPosted on 2008-03-17 at 10:09:59ID: 21144106
scanf for user input is most of the time not a very good idea. Use a combination out of fgets + sscanf.
You code does not use any structuring in form of eithed Datastructures for functions. So you have copy+paste code which nearly always means that something is wrong with your code.
You also use hardcoded numbers like 50 but for what good is that? What happens if the limit will change to 100?
Still worse you have bugs in your if /else branches. You can not write
if (a < b)
statement a;
statement b;
you have to put it into {}
I don't know wheter you've heard or learned about structures, and or array. But it seems it would be a good time starting to learn about it:
And It would be also a good idea not to use
printf("\n"); if you can add the newlines to the printfs before. A simply putc would be fine.
So in short I doubt your code will run as you expected, start structuring your code and start thinking about how you might test what you've written. If you doin't you will be very upset soon.
Regards
Friedrich