Question

I am looking for advice on my project..

Asked by: jasmith71

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;
}


This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2008-03-17 at 09:42:47ID23247589
Tags

C

,

IE 7.0

,

Advice

Topic

C Programming Language

Participating Experts
4
Points
500
Comments
4

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. _printf error with visual c 1.52
    What causes this link problem ( _printf ) with Visual C 1.52 in this windows project & how can it be solved ? #include <stdio.h> #include <windows.h> void main() { printf("This is just a test"); } Linking... TRY.OBJ(c:\project\vc-c1_de.kit\vcc\...
  2. printf implementation
    Sample program: ------------------------------------------------- #include <stdio.h> void main() { printf("%d"); } -------------------------------------------------- This program prints junk value. I'm curious, how printf is implemented. How it interp...
  3. Printf and Scanf
    Can anybody tell me the difference between specific printf() and scanf() functions? For example, what are the difference between wsprintf() and sprintf()? I just need a list of detailed specifics on some of these functions. Thanks!
  4. printf
    void split(char *pMessage) { int slen, leng; char *pE, *pF; slen = strlen(pMessage) ; pE = pMessage; do { pF = pE; leng = slen > MAXLEN ? MAXLEN : slen ; pE = pF + leng; while (*pE != ' ') { if (*pE...
  5. where is implementation of printf() in stdio.h?
    i use visual studio c++ 6.0 i went into the stdio.h to take a look at printf(). here is the function prototype but where is the code that shows HOW the program prints something to the screen? _CRTIMP int __cdecl printf(const char *, ...); what does _CRTIMP mean? does __cdecl...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

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

 

by: itsmeandnobodyelsePosted on 2008-03-17 at 11:26:03ID: 21144752

To add to the above comment:

The scanf isn't good as any wrong input will spoil your input stream and make your program hang. A fgets reads a textline (closed with ENTER) to a char buffer and hardly could be spoiled. Then you could convert the char buffer to a float or double by sscanf or - maybe better - by strtod. The latter shows you whether the user has entered garbage characters.

If you would make a input function with a prototype like the following

float input(const char * pszPrompt);

which would repeat until the user made a valid input, your prog would  look like

 flbGrapes1 = input("Please enter the price per pound for your Grapes: $");
 flbPears2 = input("Please enter the price per pound for your Pears: $");
 flbBellpeppers3 = input("Please enter the price per pound for your Bell peppers: $");
 ...

and you easily can make sure in the input function that only valid inputs were returned. You even could check whether the user inputted 'quit' and call the exit(1); function to quit in that case.

 

by: peetmPosted on 2008-03-17 at 11:29:40ID: 21144776

I always think using printf() for simple text+crlf output is bad - I always consider it demeaning to printf() !  Perhaps use puts("Welcome to Reggie's Friendly Neighborhood Grocery Store!");

Far to many printf("\n"); for my liking, use puts("Text\n\n\n\");

Magic numbers aren't good - use #define, or better still 'const', e.g., in the line:

fdisc = (ftotPurch * .05);

What's the .05 represent?

use const float meaningfull_name = .05 instead.

    float flbGrapes1;
    float flbPears2;
    float flbBellpeppers3;
    float flbNectarines4;
    float flbPotatoes5;
   ...

Why not

    float flbGrapes1,
            flbPears2,
            flbBellpeppers3,
            flbNectarines4,
            ...
            flbPotatoes5;

Or something similar ... alot easier if you want to change to double!


  //initialize variables
    flbGrapes1 = 0.00;
    flbPears2 = 0.00;

Can be done with the declaration - that way [esp. if you set stuff to 0 if you've no other special value in mind] you can easily spot that you've left something either uninitialised, or unused.

  //initialize variables
    float flbGrapes1 = 0.00;
    float flbPears2 = 0.00;

 

by: PaulCaswellPosted on 2008-03-17 at 17:48:24ID: 21147653

Hi jasmith71,

The most important enhancement I would suggest is using arrays. However, if you have not yet covered arrays in your course I would suggest you do not attempt it.

Essentially, you could replace all the variables you have added a number to, such as:

    float ftotLbs1;
    float ftotLbs2;
    float ftotLbs3;
    float ftotLbs4;
    float ftotLbs5;

should become:

    float ftotLbs[5];

obviously you will need to change the code to deal with that change and the others you make but I am sure once you are finished and get it working again you will understand a great deal more about programming in C.

Remember to save a copy of what you have now in case you run out of time.

Paul

Hint:

This may be useful:

char * fruitName[] = {
   "Grapes",
    "Pears",
    "Bellpeppers",
    "Nectarines",
    "Potatoes"
};

but make sure you understand what this means.

Paul

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...