### do..while cycle problem

Task:Make a program which can show the money left owed to the user while the user inputs the money owed total and the amount he can pay each month.
After each month, show the user how much he is left owed, he CANNOT repay more then the amount neccesary!

Example User owes total of 120\$
1. month – 35\$ (left owed 85\$)
2. month – 50\$ (left owed 35\$)
3. month – 12\$ (left owed 23\$)
4. month – 23\$ (left owed 0\$)

I got this far but have no idea why my program just doesn't calculate the ''left owed'' and doesn't stop when the guy actually pays everything lol

 ``123456789101112131415161718192021`` ``````#include #include #include int main() { clrscr(); int x, y, sum=0; cout<< "Input total amount owed "; cin>> x; do { cout <<"Input amount you can repay each month: "; cin >>y; if (x>y) sum=sum+y; } while (x=sum); cout <<"\nMoney left owed: "<

thoughts?
Last edited on
You need to move the statement

cout<<"\nMoney Left owed: "<<endl;

also this line:
while (x=sum); //while setting x to sum = true
//when making an assignment like this is always returns true

should be

while(x >= sum);

Last edited on
fixed that, where should i put that? like this?

 ``123456789101112131415161718192021`` ``````#include #include int main() { clrscr(); int x, y, sum=0; cout<< "Input total amount owed "; cin>> x; do { cout <<"Input amount you can repay each month: "; cin >>y; if (x>y) sum=x-y; cout <<"\nMoney left owed: "<=sum); getch(); }``````

now when i input a number for example 30 total and 10 in the 1st month it shows money left owed''10''? And program stops...

Ok so now i think i fixed the sum part with

sum=x-y, but however it just shows it correctly the 1st month

like i put in 30 total and 10 first it correctly shows that the user owes 20 more however when i repeat this with another 10 it again shows 20, why

this is hard
Last edited on
your condition must be like this `if(x>=y)`you should put another variable their e.g int variable=0; and do this variable=x-sum; and then
change this `cout<<"\nMoney left owed: "<<variable`
and your while condition can should be this `while(x>0);`
Thanks for response i followed your idea it looks like this now, but still doesn't quite work.
 ``12345678910111213141516171819202122`` ``````#include #include #include int main() { clrscr(); int x, y, sum=0, n=0; cout<< "Input total amount owed "; cin>> x; do { cout <<"Input amount you can repay each month: "; cin >>y; if (x>=y) n=x-sum; cout <<"\nMoney left owed: "<0); getch(); }``````

However at that point the ''sum'' is still 0 which doesn't quite make the program work.

If however i put x-y, it does show the correct result the 1st time
Last edited on
you misunderstand my response, it should be like this
 ``12345`` ``````if (x>=y) { x=x-y; cout <<"\nMoney left owed: "<

this will work for sure...
Last edited on
I should just retire from this, im too stupid, i want to learn this but its so hard..

The program still doesn't work :(

 ``1234567891011121314151617181920212223242526`` ``````#include #include #include int main() { clrscr(); int x, y, sum=0, n=0; cout<< "Input total amount owed "; cin>> x; do { cout <<"Input amount you can repay each month: "; cin >>y; if(x>=y) { sum=x-y; n=x-sum; cout<<"\nMoney left owed: "<0); getch(); }``````

it still doesn't remember the new total amount left owed.
Last edited on
try this im sure this will work...
 ``12345`` ``````if (x>=y) { x=x-y; cout <<"\nMoney left owed: "<
maculhet, it DOES! Thanks alot!
Topic archived. No new replies allowed.