I'm trying to change a char to an int using the stdlib.h atoi function
It works perfectly on windows using VC++ however on Ubuntu
Linux using g++ the value of t is off and from is correct.

char from = '0', to = '0';
cout<< "From: ";
cin >> from;
cout<<"To: ";
cin>> to;

//from within range 0-7 to in range 0-6
if( ((int)from >= 48 && (int)from <= 55) &&
		((int)from >= 48 && (int)to <= 54) )
    int f = atoi(&from);
    int t = atoi(&to);

For example when i input
from = 2 which is 50
to = 6 which is 54


This appears to be adding from and to together 50 + 54
and the using atoi on that then uses atoi on to on its own and
adds them together

50 + 54 = 104
104 -48 = 56 + 6 = 62

On a few different tests it seems to follow this pattern
My question is why is this happening using g++ and not VC++

8 Years
Discussion Span
Last Post by Salem

> int f = atoi(&from);
Simply creating values of the right type isn't enough. You need to know what's going to happen next.

atoi() expects an ARRAY of characters ending in a \0 character.
This, you do not have. You have a single char and undefined values following it (so yes, you get garbage).

> It works perfectly on windows using VC++
It works just the same.
The only difference is, you got lucky with the junk being \0.

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.