1

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;
}
halfer
  • 18,701
  • 13
  • 79
  • 158
Dipsy
  • 33
  • 1
  • 1
  • 8

3 Answers3

2

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)

Edwin
  • 825
  • 1
  • 6
  • 15
  • 1
    you can replace `cin >> rpsYou;` with `getline(cin, rpsYou);` in your `while loop` and add the `if (input == "") {status=false;}` after you receive user's input. (i.e: add an `else if` statement above this line: `else {cout << "ERROR: Please enter 'Rock', 'Paper' or 'Scissors'.";}`) – Edwin Oct 12 '14 at 14:45
0

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.

Community
  • 1
  • 1
hyde
  • 50,653
  • 19
  • 110
  • 158
0

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

Community
  • 1
  • 1
devil0150
  • 1,017
  • 3
  • 12
  • 24