Loops

Hi, im new to C++. I wrote this code to count from 1 to 9999 using loops, problem is that it only counts upto 8999. Any help would be greatly appreciated


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
46
47
48
49
50
51
52
53
#include <iostream>
#include <windows.h>
#include "conio.h"

using namespace std;



int main()

{
	int i1=0;
	int i2=0;
	int i3=0;
	int i4=0;
	do
	{
		while (i1<9)
		{
		cout << i4 << i3 << i2 << i1 <<  endl;
		//Sleep(1000);
		i1++;
		}
		while (i1==9 && i2<9)
		{
		cout << i4 << i3 << i2 << i1 <<  endl;
		i1=0;
		//Sleep(1000);
		i2++;
		}
		
		while (i1==9 && i2==9 && i3<9)
		{
		cout << i4 << i3 << i2 << i1 <<  endl;
		i1=0;
		i2=0;
		//Sleep(1000);
		i3++;
		}
		while (i1==9 && i2==9 && i3==9 && i4<9)
		{
		cout << i4 << i3 << i2 << i1 <<  endl;
		i1=0;
		i2=0;
		i3=0;
		//Sleep(1000);
		i4++;
		}
	}
	while(i4<9);
	cout << "Press any key to continue " << endl;
	_getch();
}
Last edited on
Why not use a single variable to store this in? To solve your problem with this current code, change rule 18 to: while (i1<=9)
It's much easier to do it with a single variable, though.
i agree with kyon

if i understand the problem i think the loop should go like this:

1
2
3
4
5
int i = 1;
while(i<=9999){
cout << i << endl;
i++;
}


i think this should already count from 1- 9999
Managed to fix it

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
46
47
48
49
50
51
52
53
#include <iostream>
#include <windows.h>
#include "conio.h"

using namespace std;

int main()
{
	int i1=0;
	int i2=0;
	int i3=0;
	int i4=0;
	int i5=0;
	
	do
	{
		while (i1<=8)
		{
			cout << i4 << i3 << i2 << i1 << endl;
			i1 = i1 + 1;
			//Sleep(100);
		}
		while (i1==9 && i2<=8)
		{
			cout << i4 << i3 << i2 << i1 << endl;
			i2 = i2 + 1;
			i1=0;
			//Sleep(100);
		}
		while (i1==9 && i2==9 && i3<=8)
		{
			cout << i4 << i3 << i2 << i1 << endl;
			i1=0;
			i2=0;
			i3 = i3 + 1;
			//Sleep(100);
		}
		while (i1==9 && i2==9 && i3==9 && i4<=9)
		{
			cout << i4 << i3 << i2 << i1 << endl;
			i1=0;
			i2=0;
			i3=0;
			i4 = i4 + 1;
			//Sleep(100);
		}
		i5 = i5 + 1;
	}
	while(i4<=9);
	cout << endl << "Total number of times the main loop was executed: " << i5 << endl << endl;
	cout << "Press any key to continue ";
	_getch();
}


The reason never used a single variable is because im learning how things in C++ programming work & using the loop suggested by markXD07 would have been too easy. I wanted to write something a little more complex - spice things up a bit. I have learnt more from this code i wrote than i would have learnt if i did it the easy way : )

Thanks for the responses though.

Last edited on
Topic archived. No new replies allowed.