Detect hitting Enter Key in C++

若如初见. 提交于 2019-12-23 04:14:14

问题


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;
}

回答1:


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)




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/26325932/detect-hitting-enter-key-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!