Dipsy
Dipsy

Reputation: 33

Detect hitting Enter Key in C++

How can I get my code to detect me hitting the enter key? I tried using cin.get() without any success. Also when the enter key is pressed, I'd like to change a boolean x from true to false.

Why doesn't this work?

if (cin.get() == '\n'){
x = false;
}

I'd like to end my loop (and thus and the program) when the enter key is pressed (see code below)

All code (simple rock, paper, scissors game):

#include <iostream>
#include <string>
#include <cstdlib> //random
#include <time.h> //pc time

using namespace std;

int main()
{

    string rpsYou;
    string rpsCom;
    string winner;
    bool status = true;

while (status){
    cout << "Welcome to Rock, Scissors, Paper!\nYou'll have to compete against the computer."
            " Please enter 'Rock', 'Paper' or 'Scissors' here: ";
    cin >> rpsYou;

    //Random number
    srand (time(NULL));
int randomNum = rand() % 4; //  -> (rand()%(max-min))+min;

//Computers guess
if (randomNum ==1){
    rpsCom = "Rock";
}
else if (randomNum ==2){
    rpsCom = "Paper";
}
else {
    rpsCom = "Scissors";
}

//First letter to capital
rpsYou[0] = toupper(rpsYou[0]);

if (rpsYou == "Rock" || rpsYou == "Paper" || rpsYou == "Scissors"){

    cout << "You: " << rpsYou << "\nComputer: " << rpsCom << "\n";

}
else {
    cout << "ERROR: Please enter 'Rock', 'Paper' or 'Scissors'.";
}


if ( (rpsYou == "Rock" && rpsCom == "Rock") ||
     (rpsYou == "Paper" && rpsCom == "Paper") ||
     (rpsYou == "Scissors" && rpsCom == "Scissors") ){

    cout << "Tie :|";

}
else if( (rpsYou =="Rock" && rpsCom =="Scissors") ||
         (rpsYou =="Paper" && rpsCom =="Rock") ||
         (rpsYou =="Scissors" && rpsCom =="Paper")){
    cout << "Congratulations! You won! :)";
}

else{
    cout << "Oh no! You lost! :(";
}

}

    return 0;
}

Upvotes: 1

Views: 20126

Answers (3)

Edwin
Edwin

Reputation: 825

You could do:

cout << "Hit enter to stop: ";
getline(cin, rpsYou);
if (input == "") {
    status=false;
}

This is assuming there's nothing in the user input, (i.e: the user just simply presses enter)

Upvotes: 2

hyde
hyde

Reputation: 62906

Sounds like you are thinking of getting key presses "in real time", like might be useful in a game for example. But cin does not work like that. There is no way to "detect when user presses enter" in standard C++! So you can not end the program when user presses enter. What you can do is end the program when user enters empty line, or when user enters for example "quit" (or whatever, up to you), but every user input must end in them pressing enter.

Reading from cin is just like reading from a text file, except this text file gets a new line every time user presses enter. So closest thing to detecting user pressing enter is using std::getline:

std::string line
std::getline(std::cin, line);

This will get all characters from stdin until a newline (or until end of file), which usually means user pressed enter, when this is used in a console application. Note that the actual end-of-line will not be stored in the string, so if user just pressed enter without typing anything else, line will be empty string.


Looking at question after edit, you could replace cin >> rpsYou; with getline(cin, rpsYou);. You might then also want to add trimming the string you read, in case user for example typed extra spaces.

Upvotes: 0

devil0150
devil0150

Reputation: 1469

You can't detect what key was pressed in standard C++. It's platform dependent. Here is a similar question that might help you.

Upvotes: 0

Related Questions