fault in do while

Code does not what i wish, who can help me.

The code below asks you first for an amount you want to deposite (=beginbedrag).
Then an interest rate (=rentepercentage).
I wish that the program gives you the new amount (bedrag) from each year, untill it's two times the same as the beginning amount:

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

//vb_4.15_6
#include <iostream>
using namespace std;
int main ()
{
	int CNT = 0;
	double rentepercentage, tegoed, beginbedrag, bedrag;
	
	tegoed = 0;
	bedrag = 0;
	
	cout << "welk bedrag wil u storten?" << endl;
	cin >> beginbedrag;
	cin.get();
	
	bedrag = bedrag + beginbedrag;
	cout <<"bedrag is: " << bedrag << endl;
	
	cout << "wat is uw rentepercentage in procenten?" << endl;
	cin >> rentepercentage;
	cin.get();
	
	tegoed = tegoed + rentepercentage / 100 + 1;
	cout << "tegoed is: "  << tegoed << endl;

	
	do
	{
		
		CNT = CNT + 1;
		bedrag = bedrag * tegoed;
		
		cout << "uw tegoed na" << CNT << "jaar is" << bedrag << endl;
		
	}
	
	while (bedrag == 2 * beginbedrag);
	{
		cout << "einde"<< endl;
		return 0;
	}
}



Last edited on
I wish that the program gives you the new amount (bedrag) from each year, untill it's the same as the beginning amount:


If you increase bedrag every year, when will it ever be the same as it was at the start?
Line 33: Why is this a while loop? You're using the assignment operator (=), not the equality operator (==). You never change bedrag within this loop. so you're going to have an infinite loop.

Line 33: The ; at the end of this line terminates the loop. Therefore this loop does nothing.


thank you! First faults finded.
but now its still not works.
I wish that the program gives you the new amount (bedrag) from each year, untill it's two times the same as the beginning amount:
1
2
cout << "welk bedrag wil u storten?" << endl;
cin >> beginbedrag;

You ask for ‘bedrag’, but you store it into a variable called ‘beginbedrag’. I think this is misleading you.
Later you make ‘bedrag’ equal to ‘beginbedrag’:
1
2
3
bedrag = 0;
. . .
bedrag = bedrag + beginbedrag;

But I think it’s not what you want.
These kind of errors are due to a bad design strategy: you declare you variables at the beginning of the function, far from where you use them. That’s not a problem for the compiler (it’s a program), but it’s terribly bad for humans: I’d avoid it.

An other problem is that you’re comparing two floating point numbers: that’s another thing which usually causes a lot of troubles. A simple method to compare two floating point number is to determine if their difference is so little that we can ignore it. So, assuming they’re both positive:
1
2
double a, b;
if(a-b <0 std::numeric_limits<double>::epsilon()) // let's say they are equal 

std::numeric_limits<double>::epsilon() is defined in <limits> and returns a pretty little value, but you can use any number you think suits your needs (0.00001, for example).
If one of the two numbers can be negative, you’d better ask for the absolute value of the difference (have a look at fabs() in <math>).

I’ve modified your code just to make it more legible. Let me know if it can be of any help:
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
27
28
29
30
31
32
33
34
35
36
37
38
39
// The code below asks you first for an amount you want to deposite (=beginbedrag).
// Then an interest rate (=rentepercentage).
// I wish that the program gives you the new amount (bedrag) from each year, 
// untill it's two times the same as the beginning amount:
//vb_4.15_6
#include <iostream>

int main ()
{
    std::cout << "welk bedrag wil u storten? ";
    double beginbedrag = 0;
    std::cin >> beginbedrag;
    std::cin.ignore();

    double bedrag = 0;
    bedrag += beginbedrag;  // in this case, "double bedrag = beginbedrag;"
                            // would do the trick.
    std::cout <<"bedrag is: " << bedrag << '\n';
    
    std::cout << "wat is uw rentepercentage in procenten? ";
    double rentepercentage = 0;;
    std::cin >> rentepercentage;
    std::cin.ignore();

    // Again: double tegoed = rentepercentage / 100 + 1;
    double tegoed = 0;
    tegoed += rentepercentage / 100 + 1;
    std::cout << "tegoed is: "  << tegoed << '\n';

    int CNT = 0;
    do {
        CNT++;
        bedrag *= tegoed;
        std::cout << "uw tegoed na " << CNT << " jaar is " << bedrag << '\n';
    } while (bedrag == 2 * beginbedrag);

    std::cout << "einde.\n";
    return 0;
}

Topic archived. No new replies allowed.