Identify accented letters and print a warning

Asked

Viewed 73 times

3

I need to make a program that accepts only letters without accent, and for that I’m trying to do something that identifies a letter with accent and then print a warning about the error, but when I type some letter with accent it simply ends the program.

int main()
{
    setlocale(LC_ALL, "ptb");

    string palavra;
    string comAcento = "ÄÅÁÂÀÃäáâàãÉÊËÈéêëèÍÎÏÌíîïìÖÓÔÒÕöóôòõÜÚÛüúûù";
    bool palavra_valida;
    palavra_valida = true;

    do
    {
        cout << "Informe uma palavra: ";
        cin >> palavra;

        for (int a = 0; a < palavra.size(); a++)
        {
            for (int b = 0; b < comAcento.size(); b++)
            {
                if (palavra[a] == comAcento[b])
                {
                cout << "Não digite letras com acento";
                palavra_valida = false;
                }
            }
        }

    } while (palavra_valida == !true);
    return 0;
}
  • 1

    It is not easier to verify that each character is among the characters represented in the ASCII table?

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

1 answer

1

I don’t want to move too much because it might be exercise that you ask to do this way.

To make it so direct on main() becomes very complicated, would have to do if with break in each level, plus use flag who is uglier than goto.

To not complicate too much it would be interesting to use a goto, which is not usually recommended, even the performance would be much higher because you don’t have to check other characters when you already know the word is invalid. It would be something like that:

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

int main() {
    setlocale(LC_ALL, "ptb");
inicio:
    string palavra;
    cout << "Informe uma palavra: ";
    cin >> palavra;
    for (int a = 0; a < palavra.size(); a++) {
        string comAcento = "ÄÅÁÂÀÃäáâàãÉÊËÈéêëèÍÎÏÌíîïìÖÓÔÒÕöóôòõÜÚÛüúûù";
        for (int b = 0; b < comAcento.size(); b++) {
            if (palavra[a] == comAcento[b]) {
                cout << "Não digite letras com acento";
                goto inicio;
            }
        }
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

If you still don’t want to use goto see how it looks. Some people prefer it, although it is more confusing to follow. I have never met a programmer where I was able to evaluate the good quality of what produces that prefers it, but I have found some experienced people who like it like this:

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

int main() {
    setlocale(LC_ALL, "ptb");
    bool palavra_valida;
    do {
        palavra_valida = true;
        string palavra;
        cout << "Informe uma palavra: ";
        cin >> palavra;
        for (int a = 0; a < palavra.size(); a++) {
            string comAcento = "ÄÅÁÂÀÃäáâàãÉÊËÈéêëèÍÎÏÌíîïìÖÓÔÒÕöóôòõÜÚÛüúûù";
            for (int b = 0; b < comAcento.size(); b++) {
                if (palavra[a] == comAcento[b]) {
                    cout << "Não digite letras com acento";
                    palavra_valida = false;
                    break;
                }
            }
            if (!palavra_valida) break;
        }
    } while (!palavra_valida);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

But if you use a function (which is the largest goto of all, but it is a way that organizes better) becomes much more structured and readable:

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

bool temAcento(string palavra) {
    for (int a = 0; a < palavra.size(); a++) {
        string comAcento = "ÄÅÁÂÀÃäáâàãÉÊËÈéêëèÍÎÏÌíîïìÖÓÔÒÕöóôòõÜÚÛüúûù";
        for (int b = 0; b < comAcento.size(); b++) {
            if (palavra[a] == comAcento[b]) {
                cout << "Não digite letras com acento";
                return true;
            }
        }
    }
    return false;
}

int main() {
    setlocale(LC_ALL, "ptb");
    while (true) {
        string palavra;
        cout << "Informe uma palavra: ";
        cin >> palavra;
        if (!temAcento(palavra)) break;
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

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