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

Posted

技术标签:

【中文标题】如何限制字符输入“cin”只得到一个字符串【英文标题】:How to limit character input "cin" to get just one string 【发布时间】:2018-04-14 00:23:45 【问题描述】:

我正在编写此代码用于培训,但我遇到了一个问题,如果我的用户写他的名字后跟一个空格和其他东西,程序会打乱我的流程。因此,如果您尝试这个小程序并且当它询问名称时,请输入“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 << "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";


你可以在这里试试:Try me 谢谢!

【问题讨论】:

【参考方案1】:

operator&gt;&gt; 在找到空白字符时停止读取输入。

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

使用您的代码的示例:

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

【讨论】:

【参考方案2】:

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

getline(cin, username);

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

#include <limits>
...

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

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

#include <sstream>
...

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

【讨论】:

哇,谢谢正在工作,实际上我的目标是第二个!现在我要研究这个函数“cin.ignore...”

以上是关于如何限制字符输入“cin”只得到一个字符串的主要内容,如果未能解决你的问题,请参考以下文章

如何限制用户仅在 C++ 中输入单个字符

Objective-C如何限制文本字段在2次输入后只允许特定字符

VB文本框怎样限制只能输入数字值

C++ cout cin 字符串操作

c++中cin/cout如何输入输出字符串数组?

shell脚本中如何限制输入的字符串中不能是某个特定的字符