如何将字符输入限制为仅获取一个字符串"cin"

How to limit character input "cin" to get just one string

本文关键字:字符串 cin 一个 获取 字符输入      更新时间:2023-10-16

我正在编写此代码进行训练,我遇到了一个问题,如果我的用户写他的名字后跟一个空格和其他东西,程序会搞砸我的流程。因此,如果您尝试使用小程序,当它询问名称时,请像"Robert Red"一样输入,这会更容易。当您在空格后放置其他内容时,就会出现问题,如果您只输入"罗伯特",一切都会很好。

这是代码:

// 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 << "Rockn";
                user_secret = 2;
                break;
            case 'p':
                cout << "Papern";
                user_secret = 0;
                break;
            case 's':
                cout << "Scissorsn";
                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!nn";
        } else if (user_secret == 0 && cpu_secret == 2) {
            wins++;
                cout << username << " wins!nn";
        } else if (user_secret == 1 && cpu_secret == 0) {
            wins++;
                cout << username << " wins!nn";
        } else if (user_secret == 2 && cpu_secret == 1) {
            wins++;
                cout << username << " wins!nn";
        } else {
          loses++;
                cout << username << " lose!nn";
        }
    }
        cout << "nnBattle 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";
}

你可以在这里试试:试试我谢谢!

operator>>在找到空格字符时停止读取输入。

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

使用代码的示例:

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

如果您希望用户能够键入包含空格的名称,请使用 std::getline() 而不是 operator>>

getline(cin, username);

否则,如果希望用户只输入 1 个单词作为名称,并且希望忽略用户可能输入的任何其他内容,请使用 std::cin.ignore()

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

或者,您可以使用 std::getline() 读取一行,然后将 std::istringstreamoperator>> 一起使用以提取该行的第一个单词:

#include <sstream>
...
string line;
getline(cin, line);
istringstream(line) >> username;