Solved

atoi conversion error

Posted on 2004-04-28
7
553 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
ID: 10942995
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
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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 10943087
Sorry, but that is what I wrote :-(
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Expert Comment

by:mnashadka
ID: 10943108
Sorry, jkr.  I've been there.
0
 
LVL 1

Author Comment

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

Expert Comment

by:jkr
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...
0
 
LVL 8

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

867 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

20 Experts available now in Live!

Get 1:1 Help Now