comparison using c characters

2

Ok, the problem is only in function q31 and q311.

link

I left the complete code in the link above.

so here's this q311 function:

void q311(){
char forma;
int n,jota,b,i,j;
printf("Informe a figura e o tamanho: ");
scanf("%c",&forma);
scanf("%i",&n);
    if (forma=='q')
    {
        for ( i = 0; i < n; ++i)
        {
        printf("*");
            for ( b = 1; b < n; ++b)
            {
                if ((i==jota)|| (i==0))
                {
                    printf("*");
                }
                else if (b==jota)
                {
                    printf("*");
                }
                else
                    printf(" ");
            }
            printf("\n");
            }
        }
    else if (forma=='t')
    {
        for(int i=n-1;i>=0;i--)
        {
            for(int j=0;j<=i;j++)
            {
             if(i==n-1 || j==0 ||i==j)
                 printf("*");
              else
                printf(" ");
        }
          printf("\n");      
         }
    }
    else
        printf("Forma Invalida\n");

}

My program always looks like "Form Invalid" output even if you type "t" or "q" ... I wanted to know why this is happening.

    
asked by anonymous 30.09.2015 / 18:42

1 answer

3

What happens is that stdin is with the character you typed in the main function to choose the q311 function, an "enter". And an enter really is not a "t" or a "q".

The simplest way to fix this is to have scanf ignore "enter" by adding a space before type:

scanf(" %c",&forma);

I recommend two readings:

A question our.

One external material.

    
30.09.2015 / 21:18