Accept uppercase and lowercase character

3

I'm making a rough draft just to train if and else that will be a subject in my next college class. I want to get there and at least already have a base of what the teacher will teach.

My question is as follows.

When I ask the user to type s (Yes) or n (No) to show the table works, however if the user types the S in uppercase the IDE executes the block of else .

How do I get the user to only type uppercase in printf ?

Follow the code below.

#include <stdio.h>
#include <stdlib.h>

main(){

  int num;
  char sn;
  printf("DESEJA ABRIR A TABUADA? S/N: ");
  scanf("%c", &sn);

  if(sn=='s') {
        printf("\nDIGITE O NUMERO DA TABUADA QUE DESEJA: ");
        scanf("%d", &num);
        printf("\n%d x 1 = %d\n", num , num*1);
        printf("%d x 2 = %d\n", num , num*2);
        printf("%d x 3 = %d\n", num , num*3);
        printf("%d x 4 = %d\n", num , num*4);
        printf("%d x 5 = %d\n", num , num*5);
        printf("%d x 6 = %d\n", num , num*6);
        printf("%d x 7 = %d\n", num , num*7);
        printf("%d x 8 = %d\n", num , num*8);
        printf("%d x 9 = %d\n", num , num*9);
        printf("%d x 10 = %d\n\n", num , num*10); 
  }

  else
       printf("\nOK, PROGRAMA FINALIZADO.\n\n");

  system("pause");

}
    
asked by anonymous 02.09.2018 / 19:43

1 answer

7
  • You can use the toupper function and convert sn to uppercase, then put something like:

    if(sn=='S')

Thus, if the user types s will convert to S , typing S retains%. That is, this way you can either type in uppercase or lowercase.

  • Another way would be to put the operator || (OU)

    if(sn=='S' || sn=='s')// sn igual a S OU sn igual a s

P.S. The user will not type in printf , scanf is that which reads what the user writes.

    
02.09.2018 / 19:53