C++ 无法获取整数的适当输入验证 双倍年龄=0; 内部管理; 年龄; //年龄输入验证。防止输入十进制数,如5.6、7.8、90.9等 while(cin.fail()| |((int)age!=(double)age)){ 年龄; 如果(cin.fail()| |(int)年龄!=双倍(年龄)){ cin.clear(); 字符串不完整; cin>>非国际性; } 年龄; } //将双类型age变量中的数据分配给int类型empAge变量 年龄=经验;
我需要做什么? 要求用户只输入整数。 如果不是整数,则给出错误“该选项无效,请重试”,并要求用户再次输入 附件中还有一张图片,显示了我的代码在使用输入时出现的错误 我需要做什么?要求用户只输入整数。如果 非整数给定错误”不是有效选项,请重试 “再次”,并要求用户再次输入 我建议你小心点,因为你到底在问什么还不清楚 我已经为您准备好了这个小片段,以防您会问这样的问题:如何在验证用户输入仅为整数值时使用C++ 无法获取整数的适当输入验证 双倍年龄=0; 内部管理; 年龄; //年龄输入验证。防止输入十进制数,如5.6、7.8、90.9等 while(cin.fail()| |((int)age!=(double)age)){ 年龄; 如果(cin.fail()| |(int)年龄!=双倍(年龄)){ cin.clear(); 字符串不完整; cin>>非国际性; } 年龄; } //将双类型age变量中的数据分配给int类型empAge变量 年龄=经验;,c++,C++,我需要做什么? 要求用户只输入整数。 如果不是整数,则给出错误“该选项无效,请重试”,并要求用户再次输入 附件中还有一张图片,显示了我的代码在使用输入时出现的错误 我需要做什么?要求用户只输入整数。如果 非整数给定错误”不是有效选项,请重试 “再次”,并要求用户再次输入 我建议你小心点,因为你到底在问什么还不清楚 我已经为您准备好了这个小片段,以防您会问这样的问题:如何在验证用户输入仅为整数值时使用stdin读取用户输入 #包括 #包括 #包括 bool是自然的(const std::stri
stdin
读取用户输入
#包括
#包括
#包括
bool是自然的(const std::string s)
{
返回s.find_first_not_of(“0123456789”)==std::string::npos;
}
int main()
{
std::字符串输入;
而(!(std::cin>>输入)| |!是自然的(输入))
{
std::cout如果您想在输入中只读整数,这是一种可能的解决方案:
#include <iostream>
#include <string>
#include <limits>
bool is_natural(const std::string s)
{
return s.find_first_not_of( "0123456789" ) == std::string::npos;
}
int main()
{
std::string input;
while (!(std::cin >> input) || !is_natural(input))
{
std::cout << "That is not a valid option, please try again.\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
int age = std::stoi(input);
std::cout << "Age (natural integer value) entered: " << age << "\n";
}
旧方法:
int num = stoi( str )
使用名称空间std;
//方法检查字符串是否仅包含数字
bool是数字(const std::string和str)
{
返回std::all_of(str.begin(),str.end(),::isdigit);
}
int main()
{
智力年龄;
布尔状态=假;
字符串myString;
而(!状态){
cout>myString;
状态=is_数字(myString);
如果(!状态)
您可能想调用cin.ignore(std::numeric\u limits::max(),'\n');
而不是string not\u int
等,就在cin.clear()
之后。附件中还显示了我的代码在使用输入时出现的错误不要发布文本图像。将粘贴文本复制到问题中,作为文本。我不明白问题是什么。请澄清你的问题。问题是什么?阅读代码中的注释,我认为你应该做一些类似于int-tmp\u-age;cin>>tmp\u-age;..//check输入失败…double-age=tmp\u-age;
,即“我会做最后一个赋值(或注释)以使其有意义。@KorelK对这种歧义表示歉意。基本上,我需要用户输入一个整数值,如果它不是整数值,则给出错误。继续重复,直到用户输入整数值。只是尝试删除错误输入,如字母表、十进制数字。
#include <iostream>
#include <string>
using namespace std;
int main() {
int age;
unsigned long status;
string myString;
do {
cout << "Please enter your age in integers: ";
getline(cin, myString);
/*
* Return the position of the first character that does not match.
* Else return "string::npos"
*/
status = myString.find_first_not_of("0123456789");
if (status != string::npos)
cout << "That is not a valid option, please try again." << endl;
} while (status != string::npos);
//Convert string to int.
age = stoi(myString);
cout << "Age as integer: " << age << endl;
}
int num = stoi( str )
int num = atoi( str.c_str() )
using namespace std;
// The method check if the string only contains digits
bool is_digits(const std::string &str)
{
return std::all_of(str.begin(), str.end(), ::isdigit);
}
int main()
{
int age;
bool status = false;
string myString;
while (!status) {
cout << "Please enter your age in integers: ";
cin >> myString;
status = is_digits(myString);
if (!status)
cout << "That is not a valid option, please try again." << endl;
}
//Convert string to int.
age = stoi(myString);
cout << "Age as integer: " << age << endl;
return 0;
}