Solved

atoi conversion error

Posted on 2004-04-28
7
549 Views
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;
while(ifile>>line)
  {
        
      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)



0
Comment
Question by:Stella Pauley
  • 3
  • 3
7 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Use

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.).
0
 
LVL 8

Accepted Solution

by:
mnashadka earned 125 total points
Comment Utility
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.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Sorry, but that is what I wrote :-(
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 8

Expert Comment

by:mnashadka
Comment Utility
Sorry, jkr.  I've been there.
0
 
LVL 1

Author Comment

by:Stella Pauley
Comment Utility
Tell me how to divide points. Or assign more to you jkr.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>> I've been there.

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

Expert Comment

by:mnashadka
Comment Utility
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).
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now