【发布时间】:2014-04-21 23:19:21
【问题描述】:
我目前正在使用我在另一个 StackOverflow 帖子中找到的函数(我找不到它),我以前使用过,名为“GetInt”。我的问题是,如果用户输入诸如“2 2 2”之类的内容,它会将其放入我接下来的两个 Cin 中。我试过 getLine,但它需要一个字符串,我正在寻找一个 int 值。我将如何构造检查以清理大于 2 的整数值并向2 2 2 答案抛出错误。
#include <iostream>
#include <string>
#include <sstream>
#include "Board.cpp"
#include "Player.cpp"
using namespace std;
int getInt()
{
int x = 0;
while (!( cin >> x))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Please input a proper 'whole' number: " << endl;
}
return (x);
}
和我的电话
do
{
//do
//{
cout << "How many players are there? \n";
numberOfPlayers = getInt();
//} while (isdigit(numberOfPlayers) == false);
} while (numberOfPlayers < 2);
编辑:
我选择了贾斯汀的答案,因为它最接近我的原始代码,并且在没有进行重大更改的情况下解决了问题。
【问题讨论】: