Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with atoi() 1

Status
Not open for further replies.

mrhegemon

Programmer
Jul 3, 2003
20
US
I am having a problem with atoi() in this function. It works correctly in Windows, putting a shiftAmount that corresponds to the two digits, but fails under Linux/UNIX where, for example, a 2 and a 3 get made into 20 + 32 = 52 instead of 20 + 3 = 23. What's wrong? Is it because I declared my variables sequentially and byte order is reversed in memory so that passing by reference to atoi(), the second call picks up both digits in reverse order? How do I fix this?

Code:
void convertTwoDigits(char firstDigit, char secondDigit, int& shiftAmount, bool& errorFlag)
{
   if(isdigit(firstDigit))
   {
      shiftAmount = atoi(&firstDigit);
      cout << shiftAmount << "\n";
      if(isdigit(secondDigit))
      {
         shiftAmount *= 10;
         shiftAmount += atoi(&secondDigit);
      }
   }
   else
   {
      displayErrorMessage(1);
      errorFlag = true;
   }

   if(shiftAmount > 26)
   {
      displayErrorMessage(2);
      errorFlag = true;
   }

}
 
The fact that it works under windows is just dumb luck. atoi() expects a null-terminated string. You are passing it a pointer to a character, so this only works if the next character in memory is either a string terminator ('\0') or non-numeric: i.e. atoi("3t") returns the value 3, it stops when it encounters the 't'.

Instead of using atoi(), simply do the following:

Code:
void convertTwoDigits(char firstDigit, char secondDigit, int& shiftAmount, bool& errorFlag)
{
   if(isdigit(firstDigit))
   {
      shiftAmount = (int) firstDigit - (int) '0';
      cout << shiftAmount << "\n";
      if(isdigit(secondDigit))
      {
         shiftAmount *= 10;
         shiftAmount += (int) secondDigit - (int) '0';
      }
   }
   else
   {
      displayErrorMessage(1);
      errorFlag = true;
   }

   if(shiftAmount > 26)
   {
      displayErrorMessage(2);
      errorFlag = true;
   }

}

Hope this helps.
 
Thanks itsgsd.

I can't believe I didn't think of that.

BTW, It isn't necessary to cast the chars as ints, in fact I was taught casting basic datatypes is bad unless unavoidable, C/C++ handles chars as ints when doing arithmetic.
 
> BTW, It isn't necessary to cast the chars as ints

You are correct, it's simply a matter of preference.

> in fact I was taught casting basic datatypes is bad unless unavoidable

Casting is only a bad idea if you don't understand what you are doing beacause it may result in compiler errors/warnings being suppressed that may clue you in to potential problems in your code.

Good luck.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top