Repeat until the correct letter is typed

Asked

Viewed 88 times

-2

Using the function switch,

case 'T' :
case 'T' :

default:

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


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

  • 4

    I don’t understand what you’re up to, can you explain it better?

  • 3

    Well, as for the back and redo part, you can try using a while, do-while, separate the part into a function and use a return or even use a goto. But to know what would be the best case, you have to better explain your problem.

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: As per 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 on C++ Shell.

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

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

  • 1

    @Diogomartins, any letter other than "T" or "F" goes to default. If you want to keep repeating until the correct letter is typed, you can use a while.

  • So what would the code look like using while? And yes I wanted it to be repeated until the correct letter was typed

  • @Diogomartins, I edited the answer

  • When I type the right letter it does not start from Return, but instead closes the window

  • @Diogomartins, when you type the correct letter, in this code it leaves the while, as there is nothing else, the program is terminated. Just put in your code whatever runs after you type the letter T or F.

  • @Diogomartins, I have improved the code for you to understand better.

  • Thanks a lot! Just one more question I’m using the following to generate a Random letter, but the print does not return anything: { int n = Rand() %26; char h = (char)(n + 65); printf(" n %c", h); }

  • 1

    @Diogomartins if you have another question, create one new question. It’s no use (for the community) to change the focus of the question every 5 minutes. Don’t take this the wrong way and welcome, take a [tour] to better understand how Stack Overflow works.

  • @Diogomartins, on the generation of random letters, only with this code is not possible I tell you precisely what is happening.

Show 5 more comments

Browser other questions tagged

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