Repeat until the correct letter is entered [closed]

-2

Using the switch function,

case 'T' :
case 'T' :

default:

How do I do for example the two cases represent a letter each, but if the user chooses a different one then it follows by default that will redo the same question, (choose a letter between T e F ).

What I wanted was for example: Choose a letter between P or B , if the user chooses the Letter C the system says that the letter is incorrect and asks the same question again. p>     

asked by anonymous 05.01.2017 / 18:20

1 answer

2

This is the basic syntax of switch :

switch (letra)
{
    case 'T': 
        // Código;
        break;
    case 'F': 
        // Código;
        break;
    default: 
        // Código;
        break;
}

EDIT: According to your comment:

#include <iostream>
#include <string>
using namespace std;

int main()
{

    char letra;
    do
    {
       cout << "Digite a letra correspondente: \n";
       cin >> letra;
    }
    while((letra !='T')&&(letra !='F')&&(letra !='t')&&(letra !='f'));

    switch (letra)
    {
        case 'T': 
        case 't': 
            cout << "Digitou a letra T.\n";
            break;
        case 'F':
        case 'f': 
            cout << "Digitou a letra F.\n";
            break;
        default: 
            // Código;
            break;
    }

    return 0;    
}

See working in the C ++ Shell .

This code will repeat until the letters T or t or F or f are typed. This condition is done in this line: while((letra !='T')&&(letra !='F')&&(letra !='t')&&(letra !='f'));

    
05.01.2017 / 18:28