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
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.
#include <iostream>
#include <windows.h>
#include "conio.h"
usingnamespace 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 : )