atoi conversion error

Posted on 2004-04-28
Last Modified: 2007-12-19
I am attempting to use an atoi conversion of 1 character of a string. This is stupid but I cannot determine the right combination to get the conversion to work.

here is the code segment:
int var;
      for(int i=0;i<line.length();i++)
              if(isdigit(line[i]))   //determine
                    var=atoi(line[i]);  //error is here
                    test.loadStack(var);  //load stack with integer


Here is the error I am receiving:

c:\2613\p07\p07\p07.cpp(43) : error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

p07.exe - 1 error(s), 0 warning(s)

Question by:Stella Pauley
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 86

Expert Comment

ID: 10942995

var=  (int) (line[i] - 48);  

'atoi()' requires using NULL-terminated strings. If you want to convert a single digit to 'int', it is actually enough to subtract the ASCII code for '0' (48 dec.).

Accepted Solution

mnashadka earned 125 total points
ID: 10942998
It's easier to just subtract '0' from the digit to convert it to an int (if it's just one digit).  For instance:
var = line[i] - '0';

Good luck.
LVL 86

Expert Comment

ID: 10943087
Sorry, but that is what I wrote :-(
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 10943108
Sorry, jkr.  I've been there.

Author Comment

by:Stella Pauley
ID: 10943136
Tell me how to divide points. Or assign more to you jkr.
LVL 86

Expert Comment

ID: 10943141
>> I've been there.

What do you mean? The solutions are equal, the only difference is that I was a tad bit earlier...

Expert Comment

ID: 10943163
Seconds :) I'd recommend against the C-style cast, though.  It's not needed at all here, and it's kind of a bad idea anyway, now that we have safer casts (a static_cast would have been just fine here).

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Would like to move button in a function 3 85
passing data from one form to another form in c++ 27 89
C++ mouse_event mouse look 7 112
One named event, multiple event handlers 2 45
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question