C++ 我们是否可以限制用户在C++;?

C++ 我们是否可以限制用户在C++;?,c++,C++,问题是当用户输入=99999999时,控制台返回所有值。 我想通过使用简单的else if语句来限制此输入如果有人试图输入a,b=99999999的值,则用户必须获得我在代码中定义的警告。 我可以用double而不是int来控制它,但这不是一个解决方案 #include <iostream> using namespace std; main() { int a, b, c, d; cout << "Enter Value for A: "; ci

问题是当用户输入=99999999时,控制台返回所有值。 我想通过使用简单的else if语句来限制此输入如果有人试图输入a,b=99999999的值,则用户必须获得我在代码中定义的警告。 我可以用double而不是int来控制它,但这不是一个解决方案

#include <iostream>
using namespace std;
main()
{
    int a, b, c, d;
    cout << "Enter Value for A: ";
    cin >> a;

    cout << "Enter Value for B: ";
    cin >> b;

    if (a > b)
        {
        cout << "A is Greater than B " << endl; //This will be printed only if the statement is True
        }
    else if ( a < b )
        {
        cout << "A is Less than B " << endl; //This will be printed if the statement is False
        }
        else if ( a, b > 999999999 )
        {
        cout << " The Value is filtered by autobot !not allowed " << endl; //This will be printed if the statement is going against the rules
        }
     else
        {
        cout << "Values are not given or Unknown " << endl; //This will be printed if the both statements are unknown
        }

    cout << "Enter Value for C: ";
    cin >> c;

    cout << "Enter Value for D: ";
    cin >> d;

    if (c < d)
    {
        cout << c << " < " << d << endl; //This will be printed if the statement is True
        }
        else if ( c > d )
        {
        cout << "C is Greater than D " << endl; //This will be printed if the statement is False
        }
        else
        {
        cout << c << " unknown " << d << endl; //This will be printed if the statement is Unknown
        }
}
#包括
使用名称空间std;
main()
{
INTA、b、c、d;
cout>a;
cout>b;
如果(a>b)
{

coutint的最大值为2147483647,小于9999999999。如果要输入9999999999,必须使用其他数据类型

编辑: 我想我必须更深入地解释为什么必须使用不同的数据类型

C++与Java不同。在Java中,如果您尝试将9999999999输入一个int,它将抛出一个异常,表示该数字太大,程序将退出

但是,在C++中,当CIN的数量太大时,CIN将进入失败模式。在失败模式下,CIN将取最接近的值域值,并且默默地忽略任何进一步的CIN,并将其作为初始值。大多数时间初始值为0;但是,根据堆空间,变量可以被分配为垃圾VA。因此,a的值为2147484647,b保留为其初始化值0或随机值。 因此,您的第一个if语句的计算结果将为true,并且可能是“A大于B”

简而言之,int的最大值是2147483647,因此如果要输入9999999999,则必须使用不同的数据类型

if (std::min(a, b) > 999999999)
是一个修复,虽然<代码>(a> 999999999)<代码>将在您的情况下工作,因为您已经安排了<代码>如果块。请注意检查<代码> 999999999 < /C> >在<代码> INT/COD>范围内。C++标准不需要它。表达式< /P>
if (a, b > 999999999)
相当于

if (b > 999999999)

在正式情况下,
a
被评估,但结果被丢弃。

您可以检查输入值,以及它是否超过另一个输入的预定义限制请求

int limit = 99999;
int a, b;
cout << "Input a : ";
cin >> a;
while(a>limit){
    cout << "Input exceeds limit reinput a : ";
    cin >> a;
}
cout << "Input b : ";
cin >> b;
while(b>limit){
    cout << "Input exceeds limit reinput b : ";
    cin >> b;
}
int limit=99999;
INTA,b;
cout>a;
while(a>限制){
cout>a;
}
cout>b;
while(b>限制){
cout>b;
}

这里有一个确保有效输入的示例函数。我让它也接受一个最小值和最大值,但您不一定需要它们,因为99999999将超出
int
的范围,这将导致
cin
失败。不过它将等待有效输入

int getNum( int min, int max ) {
    int val;
    while ( !( cin >> val ) || val < min || val > max ) {
        if ( !cin ) {
            cin.clear();
            cin.ignore( 1000, '\n' );
        }
        cout << "You entered an invalid number\n";
    }
    return val;
}

int main() {
    int a = getNum( 0, 12321 );
}
intgetnum(intmin,intmax){
int-val;
而(!(cin>>val)| valmax){
如果(!cin){
cin.clear();
cin.忽略(1000,“\n”);
}

如果(a,b>9999999)不能满足您的要求,则不能执行
if(a,b>9999999)
操作。这与
if(b>9999999)相同
@Swift FridayPie我必须在两行代码中声明它们的一部分?这回答了你的问题吗?这回答了你的问题吗?@ChrisMM不完全是因为我想用int,而不是float或double来解决它。不,那是错误的。如果
int
有那个范围(可能有)然后,9999999999的类型将比
int
更宽(a
long
或可能是a
long
),和
b
隐式转换为该类型。您在Java中的分析是正确的,这就是该语言不会走这么远的原因之一!@Bathsheba,int确实有这个范围。如果您实际运行此代码并输入9999999999,则a将获得int max的值,而b将自动获得垃圾值,这就是a的原因返回Bathsheba的代码。@“代码< >代码>:ST::CIN < /CUT>在超出代码< INT/INTCOR> >范围内时,读取该值将失败。”Bathsheba代码甚至不会到达第二个if语句,因为A已经大于B,输入9999999999。C++中的代码> int /代码>有时会让我恼火。所有的标准都表示它的大小。is
int
至少为16位且不大于
long
循环中的条件是backwards我已经提到我想用else if语句来实现它。@Саа,不是在你的初始问题中。不过,答案是否定的。@Сааа,问题是
999999999999
对于
int来说太大了
(在我能想到的任何实现上)。这会导致
cin
失败。如果您想限制为
9999999
,那么您可以使用
If
else
。我建议的解决方案的优点是,当用户为整数输入类似“adas”的内容时,它也可以处理,这显然是无效的。