"Force" A variable type char to uppercase

1

I would need a function that can convert a "char" variable from lowercase to uppercase after being entered. I know the function toupper () but I understand that it only works with character strings, and I just want to work with a variable.

Ex of what I want to do:

char tipo;
cout << "Tipo de consola (P = Portatil, M = De mesa): ";
cin >> tipo;
if( tipo != 'M' and tipo != 'P')
{
    cout << "Tipo de consola invalido.";
    return false;
}

Just as it works, as long as M or P is entered, but if "m" or "p" is entered the program bounces me. I do not want to add the lowercase letters to the "if" because otherwise it would be untidy.

    
asked by FranciscoFJM 18.06.2017 в 22:30
source

1 answer

3
  

I know the function toupper() but I understand that it only works with character strings.

You're in luck, because you're wrong, nor the C version of toupper or its counterpart toupper of C ++ work with strings of characters if not with single characters. So without fear you can use it:

char tipo;
std::cout << "Tipo de consola (P = Portatil, M = De mesa): ";
std::cin >> tipo;

tipo = std::toupper(tipo);

if( tipo != 'M' and tipo != 'P')
{
    cout << "Tipo de consola invalido.";
    return false;
}
    
answered by 19.06.2017 в 09:03