Loop program to start from top again
Aug 3, 2012 at 7:56pm UTC
I looked around but Im having trouble trying to make my program run again, it draws a diamond, I want it to run over and over again asking the user to put in another size for the diamond. I added a line of "while ( b > 0)" but this just makes it run forever.
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 54 55 56 57 58 59 60 61 62 63 64
#include <stdio.h>
#include <stdlib.h>
int main()
{
int s;
int a;
int b;
printf("enter size of diamond\n" );
scanf("%d" ,&b);
if (b == '0' )
{
exit(0);
}
while ( b > 0)
for (s=1; s<=b; s++)
{
for (a=1; a<=b-s; a++)
{
printf(" " );
}
for (a=1; a<=2*s-1; a++)
{
printf("*" );
}
printf("\n" );
}
for (s=1; s<b; s++)
{
for (a=1; a<=s; a++)
{
printf(" " );
}
for (a=2*b-2*s-1; a>=1; a--)
{
printf("*" );
}
printf("\n" );
return (0);
}
anyhelp would be appreciated...
Aug 3, 2012 at 8:34pm UTC
ok i also made exactly the same program some while ago. here is my code (it keeps asking for a new number) and maybe you could improve your code with this.
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
#include <iostream>
using namespace std;
int main ()
{
int n, i, k, j; char space=0x20;
while (n>0) {
cin>>n; cout<<endl;
for (i=1;i<=n;i++)
{
for (k=n-i;k>=0;k--)
{
cout << space;
}
for (j=1;j<=2*i-1;j++)
{
cout<<"*" ;
}
cout<<endl;
}
for (i=n-1;i>=1;i--)
{
for (k=0;k<(n-i)+1;k++)
{
cout << space;
}
for (j=2*i-1;j>=1;j--)
{
cout<<"*" ;
}
cout<<endl;
}
}
system("pause" );
return 0;
}
Last edited on Aug 3, 2012 at 8:41pm UTC
Aug 3, 2012 at 8:37pm UTC
I think you have missing curly braces there, ma21212. Right after the mentioned while statement.
Aug 3, 2012 at 8:43pm UTC
you also miss } at line 57
Topic archived. No new replies allowed.