gpt4 book ai didi

C++ : integer constant is too large for its type

转载 作者:行者123 更新时间:2023-11-30 01:56:30 38 4
gpt4 key购买 nike

我需要暴力破解一年来进行练习。编译器不断抛出此错误:

bruteforceJS12.cpp:8:28: warning: integer constant is too large for its type [enabled by default]

我的代码是:

#include <iostream>

using namespace std;

int main(){

unsigned long long year(0);
unsigned long long result(318338237039211050000);
unsigned long long pass(1337);

while (pass != result)
{
for (unsigned long long i = 1; i<= year; i++)
{

pass += year * i * year;

}

cout << "pass not cracked with year = " << year << endl;
++year;

}

cout << "pass cracked with year = " << year << endl;
}

请注意,我已经尝试使用 unsigned long long result(318338237039211050000ULL);

我使用的是 gcc 4.8.1 版

编辑:

这是使用 InfInt 库的更正版本 http://code.google.com/p/infint/

#include <iostream>
#include "InfInt.h"

using namespace std;

int main(){

InfInt year = "113";
InfInt result = "318338237039211050000";
InfInt pass= "1337";

while (pass != result)
{
for (InfInt i = 1; i<= year; i++)
{

pass += year * i * year;

}

cout << "year = " << year << " pass = " << pass << endl;
++year;

}

cout << "pass cracked with year = " << year << endl;
}

最佳答案

找出系统数字限制的快速方法是使用 std::numeric_limits .运行以下代码时系统上的输出:

#include <iostream>
#include <limits>

int main()
{
std::cout << "ull\t"
<< std::numeric_limits<unsigned long long>::lowest() << '\t'
<< std::numeric_limits<unsigned long long>::max() << std::endl ;
}

是:

ull 0   18446744073709551615

我们可以看到最大值肯定小于你的字面值:

 18446744073709551615
318338237039211050000

所以你的整数字面值对于 unsigned long long 类型来说太大了。

关于C++ : integer constant is too large for its type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19715782/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com