Accept uppercase and lowercase character

Asked

Viewed 743 times

3

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

My question is this:.

When I ask the user to type s (Yes) or n (No) to show the tab works, but if the user type the S uppercase letter IDE executes block else.

How do I make the user only type uppercase letters on 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");

}

1 answer

7


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

    if(sn=='S')

Thus, if the user writes s will convert to S, write S maintains. That is, this way you can either write in uppercase or lowercase.


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

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


P.S. User will not type in printf, the scanf is reading what the user writes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.