It does.
It just doesn't do it
when you expect.
Use the debugger; put a breakpoint on the line
for(j=1;j<=c;j++)
And run your code.
Enter your integer and press ENTER and it will hit the breakpoint.
c
will be zero,
a
will be an empty string - because the
scanf
call will end the integer when it meets the ENTER, but will leave it on the input buffer. So when you try to read the string, there is a empty line "sitting there" waiting for you, and it returns that.
Try this:
int main()
{
int c,cas,i,j,sum=0;
char a[100];
scanf("%d\n", &cas);
for(i=1;i<=cas;i++)
And it should do what you expected.
Can I suggest that the
if
code would be a lot more readable is you used a
switch
statement instead of
if
?