Jump to content

Is anyone here any good at C programming


Recommended Posts

Guest DC Power Kyle

I am being a lot more "difficult" on you than a teacher would be at the level you're at but I wish I knew what I told you when I first started out. Refactoring is so much safer when you have unit tests because if you clean it up and miss something now you have a bug you didn't have before.

Link to comment
Share on other sites

Thanks for the help. I am really rusty at this and prolly don't know the correct way to lay everything out.

is this better?

#include <stdio.h>
int main()
{
double owe
,pay
,change
,negative;
int quarter = 0
,dime = 0
,nickle = 0
,pennie = 0;
printf("How much did the product you want cost?\n"); /* This sets the cost of the product being purchased*/
scanf("%lf",&owe);
printf("How much money will you be paying?\n"); /* This sets the what the customer will be paying*/
scanf("%lf",&pay);
if (pay < owe || owe < 0) /*this checks to see if the person has enough money to pay the bill*/
{
printf("I'm sorry you don't have enough money to pay for this item\n");
negative = -1*(pay-owe);
printf("You owe %.2f more\n",negative);
printf("Goodbye\n");
return 0;
}
printf("If this is true you will be paying %.2f for a product costing %.2f\n", pay, owe);
change=pay-owe;
printf("You will recieve %.2f in change\n",change);
while (change > 0.24) /*this checks to see if there can be any quarters made for change*/
{
quarter++;
change=change-0.25;
}
printf("%i:quarters\n",quarter);
while (change > 0.09) /*this checks to see if there can be any dimes made for change*/
{
dime++;
change=change-0.10;
}
printf("%i:dimes\n",dime);
while (change > 0.04) /*this checks to see if there can be any nickles made for change*/
{
nickle++;
change=change-0.05;
}
printf("%i:nickle\n",nickle);
while (change >= 0 ) /*this checks to see if there can be any pennies made for change*/
{
pennie++;
change=change - 0.01;
}
printf("%i:pennie\n",pennie);
return 0;
}
1349713160_rte.png
Link to comment
Share on other sites

Guest DC Power Kyle

Look at the first file https://gist.github.com/kylecannon/4e0d8092c8bdd207d7b6/revisions

The green lines is what i typed and the red is what i removed from your code. Just trying to be consistent is all. So basically you were doing

while (change > 0.09)

{

dime++;

change=change-0.10;

}

when it should be:

while (change >= 0.1)

{

dime++;

change=change-0.1;

}

I look at it as "While I can make a dime or more, make a dime"

while you programmed "while i have greater than .09 then make a dime"

try to think of your code as talking. Simplicity makes code really readable and when things are going south, you can read it back to yourself in english and know exactly what is going on.

Worth the money: http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882

Link to comment
Share on other sites

A simple way could be:

while (scanf("%d",&num) !=1) /*while not input looking for*/
while( (getchar() !='\n') ); /*eat garbage in stdin that scanf has*/
puts("puts please enter an integer");/*prompt for proper input*/
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Who's Online   1 Member, 0 Anonymous, 1451 Guests (See full list)

×
×
  • Create New...