I am trying to run a program with countdown , but in the counter it is not decreasing

Asked

Viewed 290 times

0

#include "stdafx.h"
#include <windows.h>
#include <string>
#include <iostream>
using namespace std;


int main()
{

    int counter, number_1, number_2;
    string resposta, resposta_2, responder, responder_2, resp = "lol";
    bool verdadeiro = true;


    while (verdadeiro) {

        cout << "A) Executar um pequeno quiz\n" << "B) Sair do programa\n";
        cin >> resposta;


        if (resposta == "A") {
            resposta_2 = "true";
            counter = 10;

        }


        else if (resposta == "B") {
            verdadeiro = false;
        }


        while (resposta != "A" && resposta != "B") {
            cout << "digite novamente apenas A ou B\n";
            cin >> resposta;

            if (resposta == "A") {
                resposta_2 = "true";
                counter = 10;

            }

            else if (resposta == "B") {
                verdadeiro = false;
            }
        }

        while (resposta_2 == "true") {

            while (counter > 0) {

                counter--; // Faz a contagem regressiva(porem nao esta funcionando)
                Sleep(1000);
                cout << counter;

                if (counter == 0) { // E volta para o menu inicial
                    resposta_2 = "false";
                }

                cout << "Em que ano teve inicio a 2 guera mundial?\n";
                cin >> responder;



                if (responder == "1939") {
                    cout << "Muito bem acertou , o quiz acabou deseja sair do programa ou voltar par o menu?\n";

                }

                else {
                    cout << "Errou acabou o jogo\n";
                    counter = 0;
                    resposta_2 = "Nao";
                }

                cout << "Digite Sim para voltar ao menu principal ou Nao para sair\n";
                cin >> responder_2;
                while (responder_2 != "Sim" && responder_2 != "Nao") {
                    cout << "Digite apenas Sim ou Nao\n";
                    cin >> responder_2;
                    if (responder_2 == "Sim") {
                        counter = 0;
                        resposta_2 = "false";
                    }
                    if (responder_2 == "Nao") {
                        counter = 0;
                        resposta_2 = "false";
                        verdadeiro = false;
                    }
                }

            }
        }
    }

    return 0;
}
  • Try to encapsulate logic in a for: for (i=counter, i>0, i--)

  • I tried but I didn’t give.

  • 1

    Thomson, does the down count indicate 10 attempts to hit the question? If yes why you set to 0 (zero) the counter after a wrong answer. This way you break the logic you created right above and don’t repeat 10 times the code. In fact the loop only runs once because hitting or missing the answer you finish the game.

1 answer

-2

When you type yes or nothing happens because you have not put a condition to lock the loop (counter > 0). And when you type something other than "yes"/"nay" enters the loop

while (responder_2 != "Sim" && responder_2 != "Nao")

Use the logical operator OR (||) to lock the loop, I suggest using a break;

Browser other questions tagged

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