为什么我的输出与输入不同? (C ++)

时间:2020-06-12 07:29:18

标签: c++ visual-studio visual-c++ input output

输入:7182933164 输出:2147483647 (这不是我知道缺少的所有代码})

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
    string line;
    ifstream file("Numbers.txt");
    int num;
    cout << "Enter credit card number : " << endl;
    cin >> num;
    cout << "enterned : " << num << endl;

2 个答案:

答案 0 :(得分:2)

7182933164如此之大,以至于它可以容纳的整数(即-2147483648 to 2147483647)的值越过。使用long类型修饰符接受此类值。并且,如果只需要正整数,请在unsigned之前添加long

执行以下操作:

...
long num; // dependent upon the computer architecture
...

如果这不起作用,请尝试long long。尽管在OnlineGDB (example)中可以正常工作。

答案 1 :(得分:2)

使用std:: string代表代码编号。

相关问题