to_string() only working for small numbers

trying to input a credit card then convert it to a string so I can work with each individual number, now when i do a number like 323213 it works, but if I do a number with 16 digits it gives me back "-858993460" which is junk

1
2
3
4
5
6
7
8
9
10
11
12
  // Variables for Member function use.
	long creditcard;
	cout << "This machine accepts credit card only." << endl;
	cout << "Available items: " << endl;
	//display items from text file
	cout << "Enter your credit card number: ";
	cin >> creditcard;


	string CreditString = to_string(creditcard);
	cout << "Credit card as string " << CreditString << endl;
Depending on the implementation long can have a range of [–2,147,483,648, 2,147,483,647] OR [–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807].
https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models
I don't get it, why don't you just change
long creditcard
to
std::string creditcard
liuyang wrote:
I don't get it, why don't you just change
long creditcard
to
std::string creditcard

The OP could possibly be testing out the std::string::to_string() function.
Last edited on
You could use long long.

 
long long creditcard;
Last edited on
This blog tests 16 (yes, SIXTEEN) different ways to convert int to string, most with external libraries though. However what's interesting is that conversion via stringstream was faster than std::to_string though not as fast as sprintf + std::string, at least for int's:

http://zverovich.net/2013/09/07/integer-to-string-conversion-in-cplusplus.html
Topic archived. No new replies allowed.