C++用户输入浮点值而不是整数
C++ user enters floating point instead of integer
我试图让程序只接受x作为整数,然后要求另一个整数y。然而,当我在x中输入浮点时,它会取输入的小数部分,并使其成为y值。我不确定我在这里犯了什么错误。
#include <iostream>
#include <string>
#include <limits>
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);
}
int toobig()
{
cout << "Your number is too large, please enter something smaller: " << endl;
int x = getInt();
return (x);
}
int toosmall()
{
cout << "your number is negative, please enter a positive number: " << endl;
int x = getInt();
return (x);
}
int main()
{
cout << "your number please:-" << endl;
int x = getInt();
if (x>100000)
{
toobig();
}
else if (x<0)
{
toosmall();
}
int y = 0;
cout << "enter y " << endl;
cin >> y;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
system("PAUSE");
return 0;
}
大多数到int
的转换在发现不能成为int
的一部分时就会停止,只有少数转换函数会告诉您它们是否在解析整个字符串之前停止。
让我们用其中一个,好吗?
int getInt()
{
for ( ; ; ) // loop until user provides something we can use.
// This is dangerous. You probably want to give up after a while.
{
std::string input; // read in as string
if (std::cin >> input)
{
char * endp; // will be updated with pointer to where conversion stopped
errno = 0;
// convert string to int
long rval = std::strtol (input.c_str(), &endp, 10);
if (*endp == ' ') // check whole string was read
{
if (errno != ERANGE) // check converted number did not overflow long
{
if (rval >= std::numeric_limits<int>::min() &&
rval <= std::numeric_limits<int>::max())
// check converted number did not overflow int
// you could replace this min and max with your own passed-in
// min and max values if you want
{
return rval; // return the known-to-be-good int
}
}
}
}
else
{ // note: usually when cin fails to read a string, it's over.
// This is actually a good time to throw an exception because this
// just shouldn't happen.
std::cin.clear(); // but for now we'll just clear the error and
// probably enter an infinite loop of failure
}
// failed for any reason. Blow off all user input and re-prompt
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
std::cout << "Please input a proper 'whole' number: " << std::endl;
}
return 0; // to satisfy compiler because a non-void function must always return.
// Never reached because of infinite for loop.
}
由于C++上的每个控制台输入都被视为字符串,因此在您的getInt()方法下,我将执行以下操作:
int GetInt(istream &stream)
{
char obtainChar; //read a character from input
int x;
stream >> x;
while(stream.fail() || (stream.peek() != 'r' && stream.peek() != 'n'))
{
stream.clear(); //clear the fail state of stream
obtainChar = stream.get(); //read a character from input
while(obtainChar != 'n' && obtainChar != EOF) //while gotten char is not a return key or EOF
obtainChar = stream.get(); //read a character from input iterate up to 'n' or EOF
//displays an error message if there was a bad input (e.g. decimal value)
cerr << endl << "Please input a proper 'whole' number: " << endl;
cout << endl << "Please re-enter x: "; //re-prompt to re-enter a value
x = GetInt(stream); //Try again by calling the function again (recursion)
}
return x; //will return after the user enters ONLY if an integer was inputted
}
第一个while基本上是说,如果流(控制台输入)失败,或者下一个流char(.peek())不是a或a,则清除流并获取第一个字符。
如果该字符不是文件结尾(EOF),则获取下一个字符,依此类推。
如果出现问题,则向用户显示错误消息,并重新提示用户x的值。
然后调用相同的函数重新测试输入(递归),如果一切正常,则返回x的值。
现在可以调用此函数来评估Y.的值
注:istream是iostream库的一部分,基本上是cin
注意:调用函数如下:
int x;
cout << "your number please:-" << endl;
x = GetInt(cin);
相关文章:
- 如何只允许用户输入正整数
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- 显示基于用户输入的整数的字符
- 正在验证c++中用户的整数输入
- 验证用户输入和整数是否C++
- 如果用户输入在整数变量中浮动怎么办?
- 尝试将文本文件中的整数替换为用户输入的整数 [C++]
- 编写一个程序,提示用户输入一个整数,然后输出数字的单个数字和数字的总和
- 只要用户输入,如何从用户那里获取整数?
- C++ - 重复 std::getline() 作为用户整数输入?
- 如何让这个C++代码从用户那里读取五个整数而不是一个整数?
- 在C 中遇到了如何从用户输入直到输入的整数输入
- 计算用户使用循环输入的整数数量
- 将用户输入验证为整数:用户必须两次输入输入
- (显示金字塔)编写一个程序,该程序提示用户输入1到15的整数并显示金字塔
- 我可以使用什么方法或方式将用户输入的字符串转换为 C++ 中的整数
- 告诉用户只允许整数
- 为什么用户定义的字符串文本和整数文本具有不同的行为?
- C++用户定义的不使用数组的序列整数
- C++不包括非整数用户输入