Solved

atoi conversion error

Posted on 2004-04-28
7
555 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

816 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

8 Experts available now in Live!

Get 1:1 Help Now