# What's wrong with this code...?

Status
Not open for further replies.

#### rmullen3

##### Programmer
This is a chaotic equation program I made, and it works fine- the problem is, there's something wrong with the Do { } while ( ) loop. If you run the program, and say, enter .0000000008 and then 60 (it doesn't matter, the first number has to be between 0 and 1), it'll display the output of the chaotic equation. Now, if you enter 1 to rerun the program, and try it again, it will continuously loop in the wrong area and I'm not sure what's wrong. Keep in mind, I'm a C++ newbie. Okey?

The code:

#include <iostream.h>
#include <stdio.h>
using namespace std; double x = (1/2); int con = 0; int le; char b; int rt = 1;
double equ (double left, double right);

int main()
{
do {
double v1 = 0, v2 = 0;
cout << &quot;\nEnter the starting number (between 0.(0*100)1-0.((9*100)-1)).\n&quot;;
cout << &quot;Then enter the number of iterations wanted.\n&quot;;
cout << &quot;When the iterations are finished, press RETURN once to end.\n&quot;;
cout << &quot;>&quot;; cin >> x; cout << &quot;>&quot;; cin >> le;
if (le < 1) { le=1; };
if (le > 22255) { le=22255; };

do {
++con;
v1 = (2*x); v2 = (1-x);
x = equ(v1, v2);
cout << x << &quot; &quot;;
} while (con != le);

cout << &quot;\n Enter 1 to rerun, 0 to exit\n&quot;;

cin >> rt;
} while (rt != 0);
return(0);

}
double equ(double left, double right)
{
double x = left*right;
return (x);
}

I really haven't looked too carefully but one thing that caught my eye was that at the start of the program you set con = 0 and then your do/while tests (con != le). When you rerun the program con doesn't appear to be reset to 0 so I guess the loop is not behaving as you expect.

Hope this helps,

paulf

Status
Not open for further replies.

Replies
2
Views
135
Replies
2
Views
102
Replies
2
Views
120
Replies
1
Views
45
Replies
0
Views
41