I'm trying to convert a string to an integer and store it in the same variable multiple time through a loop. For some reason it never works after the first time, it screws the number up.

#include <stdlib.h>
#include <string>
#include <iostream>
#include <cstdio>
#include <sstream>

using namespace std;

int main(int argc, char *arg[]){

string test("329034023");
long testNum = 0;
	for(int i =0; i < 2; i++){
		testNum = 0;
		testNum = atoi(test.c_str());
                cout<<"number "<<test<<" is: "<<testNum<<endl;
return 0;

any suggestions? I also tried using the istringstream but had the same results.

9 Years
Discussion Span
Last Post by siddhant3s

walom, your give the atoi function a overflow parameter. the max integer is 2147483647, which is less than "23423423423", so the result of atoi is incorrect.

I give you two advices:
1. use atol instead of atoi, which turns a string to a long integer
2. refer to the number_limits in STL, you can find the max integer there

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.