output is not right

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 2
Reputation: jks1234 is an unknown quantity at this point 
Solved Threads: 0
jks1234 jks1234 is offline Offline
Newbie Poster

output is not right

 
0
  #1
Jun 21st, 2008
Its an exact change program but the output is not right. I don't know whats wrong in it. Can somebody please help me with program.

#include <iostream>
using namespace std;

int main ()

{

int twenties, tens, fives, singles, quarters, dimes, nickles, pennies;
double purchase, pay, rempay=0;
cout<< "please enter the purchase amount ";
cin>> purchase;
cout<< " please enter the payment amount ";
cin>> pay;
rempay = pay - purchase;
twenties = int(rempay/20);
rempay= rempay - 20;
tens= int(rempay/10);
rempay = rempay - 10;
fives= int(rempay/5);
rempay= rempay - 5;
singles = int(rempay / 1);
rempay = rempay - 1;

quarters = int(rempay / .25);

rempay = rempay - .25;
dimes = int(rempay / .10);
rempay = rempay - .10;
nickles = int(rempay / .05);
rempay= rempay - .05;
pennies= int(rempay / .01);



cout<< "twenties - " << twenties << '\n';
cout << "tens - " << tens << '\n';
cout <<" fives - " << fives << '\n';
cout << "singles - " << singles << '\n';


cout << "Quarters - " << quarters << '\n';
cout << "Dimes - " << dimes << '\n';
cout << "Nickels - " << nickles << '\n';
cout << "Pennies - " << pennies << '\n';





return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: output is not right

 
0
  #2
Jun 22nd, 2008
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main ()
  5.  
  6. {
  7.  
  8. int twenties, tens, fives, singles, quarters, dimes, nickles, pennies;
  9. double purchase, pay, rempay=0;
  10. cout<< "please enter the purchase amount ";
  11. cin>> purchase;
  12. cout<< " please enter the payment amount ";
  13. cin>> pay;
  14. rempay = pay - purchase;
  15. twenties = int(rempay/20);
  16. rempay= rempay - 20;
  17. tens= int(rempay/10);
  18. rempay = rempay - 10;
  19. fives= int(rempay/5);
  20. rempay= rempay - 5;
  21. singles = int(rempay / 1);
  22. rempay = rempay - 1;
  23.  
  24. quarters = int(rempay / .25);
  25.  
  26. rempay = rempay - .25;
  27. dimes = int(rempay / .10);
  28. rempay = rempay - .10;
  29. nickles = int(rempay / .05);
  30. rempay= rempay - .05;
  31. pennies= int(rempay / .01);
  32.  
  33.  
  34.  
  35. cout<< "twenties - " << twenties << '\n';
  36. cout << "tens - " << tens << '\n';
  37. cout <<" fives - " << fives << '\n';
  38. cout << "singles - " << singles << '\n';
  39.  
  40.  
  41. cout << "Quarters - " << quarters << '\n';
  42. cout << "Dimes - " << dimes << '\n';
  43. cout << "Nickels - " << nickles << '\n';
  44. cout << "Pennies - " << pennies << '\n';
  45.  
  46.  
  47.  
  48. cin.ignore();
  49. cin.get();
  50. return 0;
  51. }

Added cin.ignore() and cin.get() just before main finishes execution.

Even so, this is just a temporary problem. You can't rely on this if the user give improper input.

I'll pm you with what I have. Really sorry, thought you couldn't get an output going with the way the code was--

Note: You may want to consider using the modulus operator to carry the remainder of the amount one set of payment couldn't fully pay off naturally...

For example...

Purchase is 300, Pay is 410

110%20 will be 5 remainder 10.

Doing int division is only half it, you'll have to calculate the remainder to be divided by the next form of payment and mod that result as well.
Last edited by Alex Edwards; Jun 22nd, 2008 at 12:34 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 6
Reputation: entei is an unknown quantity at this point 
Solved Threads: 1
entei entei is offline Offline
Newbie Poster

Re: output is not right

 
0
  #3
Aug 31st, 2008
you should consider using a modulus operator for several reasons.

In you code, when you divide by rempay, unless the result is a value greater than 1, the value you get for rempay will not give you the desired output. You have no conditions in your code to check whether the value of rempay before deducting the appropriate amount from it. to illustrate my point, consider

purchase = 27
pay = 30

when you divide by 20 and takeaway 20, rempay = 3,
then you devide by ten, tens is 0.3, and then your code deducts 10 from rempay without any set conditions, so rempay now becomes -7, and then folowing through the code , your output will give you the wrong values.

i suggest you use an if statement to check conditions whereby before you deduct the amounts, it checks to see how much, if any, you should deduct.

eg: from my ilustration
  1. rempay= rempay - 20;
  2. if(tens > 1)
  3. rempay -= 10;
  4. fives= int(rempay/5);

otherwise you should consider using the modulus function
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC