C++ “如何限制字符输入”;cin";只得到一个字符串

C++ “如何限制字符输入”;cin";只得到一个字符串,c++,string,input,cin,C++,String,Input,Cin,我写这段代码是为了训练,我遇到了一个问题,如果我的用户在名字后面加上空格或其他东西,程序会把我的流程搞乱。所以,如果你尝试一下这个小程序,当它询问名字时,写上像“罗伯特·瑞德”这样的字会更容易。当你在空格后加上其他东西时,问题就出现了,如果你只输入“Robert”,一切都会好起来 代码如下: // Description: This is a simple replica of the Japanese game Rock, Paper and // Scissors. // Author:

我写这段代码是为了训练,我遇到了一个问题,如果我的用户在名字后面加上空格或其他东西,程序会把我的流程搞乱。所以,如果你尝试一下这个小程序,当它询问名字时,写上像“罗伯特·瑞德”这样的字会更容易。当你在空格后加上其他东西时,问题就出现了,如果你只输入“Robert”,一切都会好起来

代码如下:

// Description:  This is a simple replica of the Japanese game Rock, Paper and
// Scissors.
// Author: Ernesto Campese
// Last Update: 11/04/2018
// Version: 0.0.1

#include "std_lib_facilities.h"

int main() {

    string username = "";
    char userinput;
    int rounds = 0;
    int wins = 0;
    int draws = 0;
    int loses = 0;
    int user_secret = 0;
    vector<string> options = {"Paper", "Scissors", "Rock"};

    cout << "Enter your name: ";
    cin >> username;
    cout << "Welcome " << username << ", this is the game of Rock, Paper and Scissors.\n";
    cout << username << " how many rounds you want to do? ";
    cin >> rounds;
    if (rounds <= 0) {
      cout << "You need to play at least one round!\n";
      rounds++;
    }
    cout << "The game is based on " << rounds << " rounds, you versus the CPU.\n";
    cout << "Are you ready? (y/n): ";
    cin >> userinput;

    if (userinput != 'y') {
      cout << "\nThank you.\nProgram Terminated by " << username;
      return 0;
    }

    for(int i = 1; i <= rounds; i++) {
      // Title of the rounds
            if (i == 1) {
                cout << "\nLet's start the first round!\n";
            } else {
                cout << "Round n. " << i << " begins!\n";
            }

            // USER makes a move
            cout << "Which is your move? (r,p,s):  ";
            cin >> userinput;
            cout << '\n' << username << " says... ";
            switch (userinput) {
            case 'r':
                cout << "Rock\n";
                user_secret = 2;
                break;
            case 'p':
                cout << "Paper\n";
                user_secret = 0;
                break;
            case 's':
                cout << "Scissors\n";
                user_secret = 1;
                break;
            default:
                cout << "something weird...\n";
                break;
            }

            // CPU makes a move
            int cpu_secret = rand() % 3;
            cout << "CPU says... " << options[cpu_secret] << "!\n";

            // The program calculates the result.
            if (user_secret == cpu_secret) {
          draws++;
                cout << username << " and the CPU draws!\n\n";
        } else if (user_secret == 0 && cpu_secret == 2) {
            wins++;
                cout << username << " wins!\n\n";
        } else if (user_secret == 1 && cpu_secret == 0) {
            wins++;
                cout << username << " wins!\n\n";
        } else if (user_secret == 2 && cpu_secret == 1) {
            wins++;
                cout << username << " wins!\n\n";
        } else {
          loses++;
                cout << username << " lose!\n\n";
        }
    }

        cout << "\n\nBattle End!\n";
        if (wins > loses) {
            cout << username << " won the battle!\n";
        } else if (loses > wins) {
            cout << username << " lost the battle!\n";
        } else {
            cout << username << " draws the battle!\n";
        }
        cout << "Thank you " << username << "!\n";

}
//描述:这是一款简单的日本游戏《石头、纸和石头》的复制品
//剪刀。
//作者:埃内斯托·坎佩斯
//最后更新:11/04/2018
//版本:0.0.1
#包括“std_lib_facilities.h”
int main(){
字符串username=“”;
字符用户输入;
整数=0;
int=0;
int=0;
int=0;
int user_secret=0;
向量选项={“纸”、“剪刀”、“石头”};
cout>用户名;

cout
操作符>
在发现空白字符时停止读取输入

使用
std::getline()
读取带有空格的用户输入

使用代码的示例:

cout << "Enter your name: ";
getline(cin, username);

cout如果希望用户能够键入一个包含空格的名称,请使用而不是
operator>

getline(cin, username);
否则,如果希望用户仅输入一个单词作为名称,并且希望忽略用户可能输入的任何其他内容,请使用:


哇,谢谢你,实际上我的目标是第二个!现在我开始学习这个函数“cin.ignore…”
#include <limits>
...

cin >> username;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
#include <sstream>
...

string line;
getline(cin, line);
istringstream(line) >> username;