Czech Bass
Czech Bass

Reputation: 3

Why is my output different from input? (C++)

Input : 7182933164 Output : 2147483647 (it isnt all of the code i know there are missing } )

#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;

Upvotes: 0

Views: 190

Answers (2)

Rohan Bari
Rohan Bari

Reputation: 7736

The value 7182933164 is such a huge number that it crosses the value of the integer it could holds (i.e. -2147483648 to 2147483647). Use long type modifier to accept such values. And if there's only positive integer required, added unsigned before long.

Do something like:

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

If that doesn't works, try long long. Although it's working fine in OnlineGDB (example).

Upvotes: 2

Zukaru
Zukaru

Reputation: 331

Use std:: string to represent code numbers instead.

Upvotes: 2

Related Questions