Solved

atoi conversion error

Posted on 2004-04-28
7
565 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
[X]
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
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
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!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

696 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