do..while cycle problem

Oct 7, 2012 at 2:55pm
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 Oct 7, 2012 at 2:56pm
Oct 7, 2012 at 3:14pm
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 Oct 7, 2012 at 3:31pm
Oct 7, 2012 at 3:26pm
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 Oct 7, 2012 at 3:41pm
Oct 7, 2012 at 3:43pm
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);
Oct 7, 2012 at 3:52pm
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 Oct 7, 2012 at 4:00pm
Oct 7, 2012 at 4:06pm
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 Oct 7, 2012 at 4:15pm
Oct 7, 2012 at 4:12pm
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 Oct 7, 2012 at 4:16pm
Oct 7, 2012 at 4:16pm
try this im sure this will work...
1
2
3
4
5
if (x>=y)
        {
            x=x-y; 
            cout <<"\nMoney left owed: "<<x;
        }
Oct 7, 2012 at 4:22pm
maculhet, it DOES! Thanks alot!
Topic archived. No new replies allowed.