Infinite loop on switch

Asked

Viewed 522 times

1

The problem is that in all three cases, cout repeats endlessly.

#include<iostream>
using namespace std;
#include<locale.h>

    int main(){
        setlocale(LC_ALL, "Portuguese");
        bool a;
        int consumo;
        cout << "Digite 1 -> até 100kw" << endl;
        cout << "Digite 2 -> de 101kw até 200kw" << endl;
        cout << "Digite 3 -> maior que 200kw" << endl;
        cin >> consumo;
        while(true){
             switch(consumo){
                case 1:
                    cout << "Parabéns, você é econômico." << endl;
                    break;
                case 2:
                    cout << "Cuidado, você está gastando muito." << endl;
                    break;
                case 3:
                    cout << "Que isso vei?" << endl;
                    break;
                default:
                    cout << "Valor inválido, tente novamente." << endl;
                    cout << "Digite 1 -> até 100kw" << endl;
                    cout << "Digite 2 -> de 101kw até 200kw" << endl;
                    cout << "Digite 3 -> maior que 200kw" << endl;
                    cin >> consumo;
                    continue;
            }
        }
    return 0;
    }
  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

2 answers

1

The switch doesn’t work that way I I already answered about the problem. The break does not work as breaking the loop when it is inside the case, cannot use it. More about: Break and Continue on Switch.

You can redo this code by placing a if to find out whether to leave or not or can use a goto, what many shivers, but there’s nothing wrong with it.

#include <iostream>
using namespace std;

int main() {
    while (true) {
        int consumo;
        cout << "Digite 1 -> até 100kw" << endl;
        cout << "Digite 2 -> de 101kw até 200kw" << endl;
        cout << "Digite 3 -> maior que 200kw" << endl;
        cin >> consumo;
        switch (consumo) {
        case 1:
            cout << "Parabéns, você é econômico." << endl;
            goto fim;
        case 2:
            cout << "Cuidado, você está gastando muito." << endl;
            goto fim;
        case 3:
            cout << "Que isso vei?" << endl;
            goto fim;
        }
    }
fim:
    return 0;
}

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

Or you can do:

#include <iostream>
using namespace std;

int main() {
    while (true) {
        int consumo;
        cout << "Digite 1 -> até 100kw" << endl;
        cout << "Digite 2 -> de 101kw até 200kw" << endl;
        cout << "Digite 3 -> maior que 200kw" << endl;
        cin >> consumo;
        switch (consumo) {
        case 1:
            cout << "Parabéns, você é econômico." << endl;
            break;
        case 2:
            cout << "Cuidado, você está gastando muito." << endl;
            break;
        case 3:
            cout << "Que isso vei?" << endl;
            break;
        }
        if (consumo > 0 && consumo < 4) break;
    }
}

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

You can still do it with a variable flag, how it looks like it was going to do, but I think it’s pretty bad to add status to control that.

One last way that is not exactly bad, but probably a little bit of nothing slower, which does not make the slightest difference in case like this, is to change the switch for ifs.

I took the opportunity to eliminate unnecessary duplication of code.

0

Obviously it will be endless loop, off the switch is the while(true) without any kind of break, just take it out that solves the problem.

You can also add the break; after the switch if you want to keep the while(true)

switch(consumo){
                case 1:
                    cout << "Parabéns, você é econômico." << endl;
                    break;
                case 2:
                    cout << "Cuidado, você está gastando muito." << endl;
                    break;
                case 3:
                    cout << "Que isso vei?" << endl;
                    break;
                default:
                    cout << "Valor inválido, tente novamente." << endl;
                    cout << "Digite 1 -> até 100kw" << endl;
                    cout << "Digite 2 -> de 101kw até 200kw" << endl;
                    cout << "Digite 3 -> maior que 200kw" << endl;
                    cin >> consumo;
                    continue;
            }break;

Needs a break to stop the switch and another break to stop the while

  • But it doesn’t do what it wants. In fact the code has errors.

  • Adding the break I think it solves the problem and does what he wants.

  • but if you have a bow that has a break unconditional and not having the loop performs the same thing.

  • 1

    worked out here that way

  • I didn’t realize what I meant, that break serves to complete the process if everything goes well, if you enter another value that is not expected continues in the loop....

Browser other questions tagged

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