Asad Asad - 4 months ago 16
C Question

The program is asking again and again of input of width ?What to do ? New in C language

The program is asking again and again of input of width ?What to do ? New in C language. Continuously asking for ("Enter the Width ") when i enters it it kept asking again and again.




Code Listing




#include<cs50.h>
#include <stdio.h>
#include <stdbool.h>

int main (void)
{
int height = 10;
int width = 10;
int width_Asterix = 2;

printf("Enter the Height:");
height = GetInt();

for (int i = 0; i<height; i++)
{
printf("Enter the width: ");
width = GetInt();

for (int j = width ;j>0; j--)
{
printf(" ");
}

width--;
for (int k = 0; k<width_Asterix; k++)
{
printf("*");
}

width_Asterix +=2 ;
printf("\n");
}
return 0;
}

Answer

I think that you should put this:

printf("Enter the width: ");
width = GetInt();

Outside of the for loop, between

height = GetInt();

and

for (int i = 0; i<height; i++)

In that way it is only asked once (outside the loop).