How to convert string to int type?

can someone help me that how to write a program for accept a large number (12 digits) that store in string and the convert it to numbers?

[ques]--is that long long type can store more that 12 digits?

here's the code, and it have
integer constant is too large for "long" type
this error, and i dont know how to fix it... can someone help me...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <conio.h>
#include <sstream>
using namespace std;

int main()
{
  string ic = "";
  int age = 0;
  long long longAge = 0;
  
  cout << "IC: ";
  cin >> ic;
  
  stringstream(ic) >> longAge;
   
  longAge = (112 - longAge/10000000000);  // this line got problem
  
  age = static_cast<int>(longAge);
  
  cout << "\nAge: " << age;
  
  getch();
  return 0;
}

After removing <conio.h> and the getch(), I didn't get any warnings. Odd.

The signed long long type can store 18 digits guaranteed, and possibly a 19th. It is a C++11 feature so you might need to explicitly enable it, though I thought most compilers supported it before the standard came out.

Good luck!

-Albatross
Last edited on
does devC++ supported this features?
If you're using Dev-C++ 4.9.9.2, the bad news is that it's dead.
However, there's an unofficial version of a continued Dev-C++, which you can find below, and comes bundled with a more recent GNU compiler.

http://orwellengine.blogspot.com/

Be sure to set the language standard to C++0x (or C++11).
OMG... i'm using 4.9.9.2 version.... =-=""
is there any variable can stored 12 digits variable in 4.9.9.2 version?
Try this?
1
2
3
#include <cstdint>

int64_t longAge = 0;

limits.h will tell the story of what your implementation is capable of.

In my case on Ubuntu 12.04 64

# if __WORDSIZE == 64
# define LONG_MAX 9223372036854775807L
# else
# define LONG_MAX 2147483647L
# endif
# define LONG_MIN (-LONG_MAX - 1L)

/* Maximum value an `unsigned long int' can hold. (Minimum is 0.) */
# if __WORDSIZE == 64
# define ULONG_MAX 18446744073709551615UL
# else
# define ULONG_MAX 4294967295UL
# endif
catfish: not working.... still cannot stored.
TightCoder: is it just have to include limits.h file with code u write?
i tried but a lot of warning and error coming out....


i think i have to change idea... does anyone know how to get first two character from a string?
example:

my input in string "1234567890"
and what i wanna get from the string is "12"

Last edited on
string newString = oldString.substr(0, 2);
http://www.cplusplus.com/reference/string/string/substr/

Edit: also, post exactly the error you are getting (and the updated source if needed).
Last edited on
it works!!
thanks for all the reply! really appreciated u guys!
Topic archived. No new replies allowed.