| | |
output is not right
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2008
Posts: 2
Reputation:
Solved Threads: 0
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;
}
#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;
}
c++ Syntax (Toggle Plain Text)
#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'; cin.ignore(); cin.get(); return 0; }
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.
•
•
Join Date: Aug 2008
Posts: 6
Reputation:
Solved Threads: 1
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
otherwise you should consider using the modulus function
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
c++ Syntax (Toggle Plain Text)
rempay= rempay - 20; if(tens > 1) rempay -= 10; fives= int(rempay/5);
otherwise you should consider using the modulus function
![]() |
Similar Threads
- Capturing Console Output (C)
- Why am I getting this output (C++)
- Reverse Output (Stack) (C++)
- Application Output Help (Java)
Other Threads in the C++ Forum
- Previous Thread: c++ multidimensional optimization library?
- Next Thread: c++ help function call
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





