如何解决在C ++中输入int时如何不接受浮点数
我目前正在学校里尝试编写一个程序,该程序包含用于掷骰子的随机数生成器。我已经成功编写了代码,但是我正在尝试对其进行改进,以便用户可以选择骰子的大小和用户尝试投掷的数字。
我已经编写了执行此操作的代码,并且还添加了代码,如果值错误(例如,不是所提供的骰子大小之一或试图将数字掷出骰子范围之外),则重复输入请求的代码输入类型(即var而不是int)被输入。但是,如果输入了浮点数,并且浮点数左侧的数字在正确的值范围内,那么它将使用该数字。
例如:
int size = 0;
cout << "Please choose the size of the dice to roll (6,12 or 20): ";
cin >> size;
while (size != 6 && size != 12 && size != 20 || !cin)
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Invalid number entered. Choose the size of the dice to roll (6,12 or 20): ";
cin >> size;
}
如果输入的任何字母或数字不是6、12或20,但如果输入20.125(或任何6.- 12.-或20.-的浮点数),这将正确地要求重复输入。 )输入,它将取数字并继续进行下一个代码位。如果我输入一个有效数字后接字母(例如6f),也会发生这种情况。
我尝试将while循环的条件修改为:
while (size != 6 && size != 12 && size != 20 || !(cin >> size))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),12 or 20): ";
cin >> size;
}
这可以解决问题,并且如果我输入12.5或20R等,它会要求我再输入一次,但是当我输入正确的输入(6、12或20)时,它将带我进入调试器中的新行无需移至下一行代码。如果我随后再次输入正确的输入,它将在读取时将我带到下一行代码。
我不太理解为什么一个错误发生时我为什么要99%地工作,而另一个错误修复了该错误却又给了我另一个错误。
在此先感谢您的任何建议指导!
解决方法
您可以读取一个浮点数,将其分配给一个整数变量,然后将两者进行比较。遵循以下原则:
int integerSize;
float floatSize;
cin >> floatSize;
integerSize = floatSize; //Your program will perform a cast from float to integer,it will essentially truncate whatever appears after the floating point.
if (integerSize != floatSize) //Your program will now perform a cast from integer to float in order to compare the two,but if floatSize was not a whole number,this comparison should return false.
{
//code to ask for another value
}
话虽这么说,浮点数(以及双打和长双打)确实会出现舍入错误,所以正如另一个用户所建议的那样,最安全的选择是自己读取字符串并对其进行解析,或者使用诸如{{1 }}(仅用于整数)和std::stoi()
(用于浮点数)。
cin >> some_int_variable
解释输入的方式是逐个字符的,直到不再有意义为止为止。例如,当遇到.
或f
时,cin
就完成了读取该int的操作。
如果您想要其他行为,则必须自己实施。具体来说,您如何停止处理一个输入,而开始处理下一个输入?
cin >> some_int_variable
不再有效时将停止,cin >> some_std_string_variable
遇到空格字符(包括新行)时将停止。你的问题呢?您想如何将一个输入与另一个分开?
如果空格是一种明智的方法,则可以这样做:
std::string word;
std::cin >> word;
int value;
bool error = false;
try {
size_t pos;
value = std::stoi(word,&pos);
// Was the full string used to build the int?
if(pos != word.size()) error = true;
} catch(std::logic_error&) {
error = true;
}
if(error) { /* something went wrong */ }
,
template<typename T>
T typed_read_line(const std::string& prompt)
{
T result{};
std::string l;
std::cout << prompt << ":";
while (std::getline(std::cin,l)) {
std::istringstream line{l};
line >> result >> std::ws;
if (line.eof()) break;
//line contains more then expected
std::cout << "Again: " << prompt << ":";
}
return result;
}
或带有extra validation或v3的版本。
,非常感谢所有为此提供帮助的人。我最终要做的是:
string diceSizeString;
int diceSize;
cout << "Choose the size of the dice to roll (6,12 or 20): ";
cin >> diceSizeString;
while (diceSizeString != "6" && diceSizeString != "12" && diceSizeString != "20")
{
cout << "Invalid number entered. Choose the size of the dice to roll (6,12 or 20): ";
cin >> diceSizeString;
}
diceSize = stoi(diceSizeString);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。