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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream.h>
#include <math.h>
#include <conio.h>
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: "<<sum;

getch();
   }


thoughts?
Last edited on
You need to move the statement

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

into your do...while loop.

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <math.h>
#include <conio.h>
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;

   }
   while (x>=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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream.h>
#include <math.h>
#include <conio.h>
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: "<<n;

   }
   while (x>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
1
2
3
4
5
if (x>=y)
        {
            x=x-y; 
            cout <<"\nMoney left owed: "<<x;
        }

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 :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream.h>
#include <math.h>
#include <conio.h>
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: "<<n;
}

   }
   while (x>0);


getch();
 }


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