J
Jon Cosby
I'm getting odd output from atol in a for loop. Entering a ten-digit
string I get different results for char and atol(char). If I drop iSum
the output seems to be okay. This happens using the g++ compiler. Using
vc++, I don't get this behavior. Anyone have an explanation for this?
////////////////////////////////////
void test(string sInput)
{
int iSum;
char cCurr;
sum = 0;
for (int i = 0; i < sInput.length(); i++)
{
cCurr = sInput.at(i);
cout << cCurr << " : " << atol(& cCurr) << endl;
iSum += (i + 1) * atol(& cCurr);
}
}
int main()
{
string sInput;
cout << "Enter test string: ";
cin >> sInput;
test(sInput);
return 0;
}
////////////////////////////////////
jon@linux:~/projects/test> ./test
Enter test string: 0201530821
0 : 0
2 : 2
0 : 0
1 : 1
5 : 5
3 : 3
0 : 3 //!!
8 : 8
2 : 2
1 : 1
string I get different results for char and atol(char). If I drop iSum
the output seems to be okay. This happens using the g++ compiler. Using
vc++, I don't get this behavior. Anyone have an explanation for this?
////////////////////////////////////
void test(string sInput)
{
int iSum;
char cCurr;
sum = 0;
for (int i = 0; i < sInput.length(); i++)
{
cCurr = sInput.at(i);
cout << cCurr << " : " << atol(& cCurr) << endl;
iSum += (i + 1) * atol(& cCurr);
}
}
int main()
{
string sInput;
cout << "Enter test string: ";
cin >> sInput;
test(sInput);
return 0;
}
////////////////////////////////////
jon@linux:~/projects/test> ./test
Enter test string: 0201530821
0 : 0
2 : 2
0 : 0
1 : 1
5 : 5
3 : 3
0 : 3 //!!
8 : 8
2 : 2
1 : 1