rocking.that.eclipse Posted September 1, 2013 Author Report Share Posted September 1, 2013 Kk I'll clean it up for you. Give me 5 min and fixed the pennies issue Link to comment Share on other sites More sharing options...
Guest DC Power Kyle Posted September 1, 2013 Report Share Posted September 1, 2013 Here is a quick cleanup, IMO you should use greater than or equal to or less than or equal to https://gist.github.com/kylecannon/4e0d8092c8bdd207d7b6 Here is the diff: https://gist.github.com/kylecannon/4e0d8092c8bdd207d7b6/revisions Link to comment Share on other sites More sharing options...
Guest DC Power Kyle Posted September 1, 2013 Report Share Posted September 1, 2013 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 More sharing options...
rocking.that.eclipse Posted September 1, 2013 Author Report Share Posted September 1, 2013 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; } Link to comment Share on other sites More sharing options...
Guest DC Power Kyle Posted September 1, 2013 Report Share Posted September 1, 2013 That's not using methods, still procedural. Methods are functions but look at my code i posted above. Link to comment Share on other sites More sharing options...
rocking.that.eclipse Posted September 1, 2013 Author Report Share Posted September 1, 2013 KK I'm looking at the code and I see subtle difference. What exactly am I doing incorrectly? I'm just not seeing the difference. Sorry I'm trying to go ME and they make me practice EE and CPE to go ME Link to comment Share on other sites More sharing options...
Guest DC Power Kyle Posted September 1, 2013 Report Share Posted September 1, 2013 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 More sharing options...
RyuunosukePanda Posted September 1, 2013 Report Share Posted September 1, 2013 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.