Do while loop help?
Hello, do my program isn't working properly. It would just print a lot of *.
My program's main goal is to print a pattern.
ex:
Input = 5
*****
****
***
**
*
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
|
#include <stdio.h>
int x, b, c, d;
main()
{
printf("num:");
scanf("%d", &x);
do
{
d=++x;
do
{
b=++x;
printf("*");
}
while (x>b);
printf("\n");
printf("\n");
do
{
c=b++;
printf("*");
}
while (b>c);
}
while (x>c);
getch();
}
|
I'm really confused with how my codes should look like...
Last edited on
You are getting in an infinite loop on lines 11 to 16.
what do you mean by that?
Wouldn't x>b on line 16 break it?
In the process of b=++x there will come the time that b would be greater than x thus breaking the loop??
sorry, I'm still new here..
++x will increase x as well
1 2 3 4 5 6 7 8 9 10 11
|
#include <iostream>
int main()
{
int x=0, b;
for (int i=0;i<5;i++)
{
b=++x;
std::cout<<x<<b<<std::endl;
}
}
|
will produce
As you can see, b is always equal to x
Topic archived. No new replies allowed.