Home > Net >  How to break when the user input is 1?
How to break when the user input is 1?

Time:09-27

I want to find a way for my code to detect a 1 or 2.

  • If a 2 is pressed, it will continue the game, docking a minute from a counter.
  • If a 1 is detected, it will end the game and give you a score based on how long it took before you entered one.

I just need a way for the code to be able to loop itself when it sees 2, but completely break when it sees 1.

#include <iostream>
#include <sstream>

using namespace std;

int main()
{
    int WorldTime = 80;
    int WorldTimeEND = 80 - WorldTime;
    int Answer;
    cout << "The world will end in " << WorldTime << " Minutes. Will you wait, or press the red button? Type 1 to press it or 2 to wait";
    std::cin >> Answer;

if (WorldTime > 0 && Answer == 1)
{
    cout << "The World was saved in " << WorldTimeEND << " Minutes";
}

if (WorldTime == 80 && WorldTime > 0 && Answer == 2)
{
    WorldTime --; 1;
    cout << "You waited. You have " << WorldTime << " Minutes until the end of the world";
}

if (WorldTime < 35 && WorldTime > 0)
{
    cout << "You feel the earth crumble under you. Hurry up. Press the button... or, do you want everyone to die?( " << WorldTime << " Minutes remain";
}

if (WorldTime = 0 && WorldTime < 0)
{
    cout << "It's over. The world ended.";
}
    return 0;

}

CodePudding user response:

Try this one:

do{
    //your code
}while(Answer!=1);

CodePudding user response:

future answer goes here. give me a minute.

CodePudding user response:

you can try:

#include <iostream>
#include <conio.h>
#include <Windows.h>
using namespace std;

int main() {
    while (1) {
        cout << "loop" << endl;
        if (_kbhit()) {
            char ch = _getch(); 
            if (ch == 49) {
                std::cout << "Press 1" << std::endl;
                system("pause");    // finish game
            }
            else if (ch == 50) {
                std::cout << "Press 2" << std::endl;
                continue; // pause game
            }
        }
    }
    return 0;
}
  • Related