rocking.that.eclipse Posted September 1, 2013 Report Share Posted September 1, 2013 Well safe to say I'm not all too good at programming so I ask if someone can stear me in the right direction here. I am writing a code for class to give change for a purchase I have all the change calculating programming done and working great, my problem is that we need to have a way to check to see if inputs are real. (ie can't pay for a 2.00 dollar charge with 1.00 or pay "a" for a 2.00 charge) does anyone know how I could read in my interger inputs and make sure they are actually intergers? Any help in general will do, all i need is a nudge in the right direction Link to comment Share on other sites More sharing options...
MrSkippyJ Posted September 1, 2013 Report Share Posted September 1, 2013 you probably want double or decimal rather than integer if you are dealing with money. Also, won't it error if the input is anything different than the declared variable's data type? i have done very little in C so it may not work that way. F150: Stock 2019 Harley Road Glide: Amp: TM400Xad - 4 channel 400 watt Processor: DSR1 Fairing (Front) 6.5s -MMats PA601cx Lid (Rear) 6x9s - TMS69 Link to comment Share on other sites More sharing options...
rocking.that.eclipse Posted September 1, 2013 Author Report Share Posted September 1, 2013 I declared the change as a decimal. I needed the number of each coin as an interger cause you can't have 2.5 quarters lol I'm really rusty at this, I know i could use it all as a single input as a string and check it for characters there but I don't remember how to do that as of right now Link to comment Share on other sites More sharing options...
MrSkippyJ Posted September 1, 2013 Report Share Posted September 1, 2013 ooh lol, gotcha. like I said I have done very little C, I have really only done some VB.net and lots of SQL. Not sure I can be of much help here. F150: Stock 2019 Harley Road Glide: Amp: TM400Xad - 4 channel 400 watt Processor: DSR1 Fairing (Front) 6.5s -MMats PA601cx Lid (Rear) 6x9s - TMS69 Link to comment Share on other sites More sharing options...
Guest DC Power Kyle Posted September 1, 2013 Report Share Posted September 1, 2013 If the change calculating is done and working great then you shouldn't have an issue with it giving you 2.5 quarters? Is it just giving you coins as change or can it give you bills as well, (1, 5, 10, 20, 50, 100 dollars)? Maybe write out in this thread your input(s) to the program and expected output(s) and you can unit test if you're familiar with that Link to comment Share on other sites More sharing options...
rocking.that.eclipse Posted September 1, 2013 Author Report Share Posted September 1, 2013 #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"); scanf("%lf",&owe); printf("How much money will you be paying?\n"); scanf("%lf",&pay); if (pay < owe || owe < 0) { 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) { quarter++; change=change-0.25; } printf("%i:quarters\n",quarter); while (change > 0.09) { dime++; change=change-0.10; } printf("%i:dimes\n",dime); while (change > 0.04) { nickle++; change=change-0.05; } printf("%i:nickle\n",nickle); printf("%.2f change\n", change); if (change <0.04) { pennie++; change=change - 0.01; } printf("%i:pennie\n",pennie); return 0; } Link to comment Share on other sites More sharing options...
rocking.that.eclipse Posted September 1, 2013 Author Report Share Posted September 1, 2013 Now its always off a pennie... Link to comment Share on other sites More sharing options...
Guest DC Power Kyle Posted September 1, 2013 Report Share Posted September 1, 2013 You should break that into methods to clean up the code and make it less procedural and easier to follow. Link to comment Share on other sites More sharing options...
rocking.that.eclipse Posted September 1, 2013 Author Report Share Posted September 1, 2013 I know. I work on that after I finish the program. and I know I work ass backwards of the easy way Link to comment Share on other sites More sharing options...
Guest DC Power Kyle Posted September 1, 2013 Report Share Posted September 1, 2013 if you're doing this to get it "working" that is so much hassle than it's really worth because you don't have tests to back up your programming that you're refactoring. Either pick one or the other and stick with it or have tests to back it up. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.