Sand Clock with while

hello my friend
i have a problem with a project that i work in my C++ lab
how to a sand clock by stars with c++ using while loop ?
ex. if the use enter the number 5 it will be like this
*****
***
*
***
*****

i need help before next Thursday any help here ?
Last edited on
How far have you gotten so far?
Post some of your code and give us a more specific problem as it's policy to not do others homework for them on this forum.
here is what i done so far :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
using namespace std;
void main()
{
	int x=0,y,z,f;
	cin >> z;
	f=z;
	while (x<z){
		y=0;
		while (y < (z-f)/2){ cout << " ";y++;}
		while (y<f){ cout << "*";y++;}
		cout << endl;
		if (x<z/2)
			f-=2;
		else 
			f+=2;
		if (z%2==0 && x==(z/2-1)) f+=2;
		x++;
		}
    
}

the shape will be like this
*****
**

**
*****
and i got stuck here and don't know how to fix it !
Last edited on
Took me a while but the problem is that you re-use y without "resetting" it so to speak:
1
2
3
y=0;
while (y < (z-f)/2){ cout << " ";y++;}
while (y<f){ cout << "*";y++;}

first time around it works fine because you don't change y on the first while, second time however y becomes 1 after the first while loop and f is 3 so you loop twice and exit.

just set y to 0 between the while loops and it'll work just fine.
thx my friend i didn't think clearly in it because i have three exam this week
it work perfectly right now

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
using namespace std;
void main()
{
	int x=0,y,z,f;
	cout << "Please Enter a Number that u want :\t";
	cin >> z;
	f=z;
	while (x<z){
		y=0;
		while (y < (z-f)/2){ cout << " ";y++;}
		y=0;
		while (y<f){ cout << "*";y++;}
		cout << endl;
		if (x<z/2)
			f-=2;
		else 
			f+=2;
		if (z%2==0 && x==(z/2-1)) f+=2;
		x++;
		}
    
}
Topic archived. No new replies allowed.