'atoi' : cannot convert parameter 1 from 'char' to 'const char *'

Discussion in 'C++' started by John Smith, Oct 31, 2005.

  1. John Smith

    John Smith Guest

    What's wrong with the use of atoi in the following code? Why do I
    get the error message:

    'atoi' : cannot convert parameter 1 from 'char' to 'const char *'


    char cBuffer[9];
    void PushUnique(int);

    for(y = 0; y < 9; y++)
    {
    isF.getline(cBuffer,9);
    for(x = 0; x < 9; x++)
    bd.Cell[x][y].PushUnique(atoi(cBuffer[x]));
    }
    John Smith, Oct 31, 2005
    #1
    1. Advertising

  2. John Smith

    Chinchilla Guest

    the prototype for atoi is
    int atoi(const char *str);
    meaning it takes a pointer to an entire character string like "12345".

    if you wanna get the value of each digit and pass it to PushUnique just
    subtract 48 from each character. Hackish, but should work.

    bd.Cell[x][y].PushUnique((int)(cBuffer[x] - 48));

    Chinchilla
    Chinchilla, Oct 31, 2005
    #2
    1. Advertising

  3. John Smith wrote:
    > What's wrong with the use of atoi in the following code? Why do I
    > get the error message:
    >
    > 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
    >
    >
    > char cBuffer[9];
    > void PushUnique(int);
    >
    > for(y = 0; y < 9; y++)
    > {
    > isF.getline(cBuffer,9);


    Are you sure the buffer of 9 chars is enough to read a whole line?

    > for(x = 0; x < 9; x++)
    > bd.Cell[x][y].PushUnique(atoi(cBuffer[x]));


    In this statement, the expression inside 'PushUnique's parentheses is

    atoi(cBuffer[x])

    'cBuffer[x]' is a _char_. 'atoi' expects a pointer to char (actually
    a pointer to a first element of an array of char, ending with a null
    character, IOW a C string. What are you trying to accomplish here?
    Depending on your answer, we can suggest a solution or two. The very
    first one that comes to mind is for you to drop the loop here and just
    do

    bd.Cell[x][y].PushUnique(atoi(cBuffer));

    it will compile, but it may not be what you need...

    > }


    V
    Victor Bazarov, Oct 31, 2005
    #3
  4. "John Smith" <> wrote in message
    news:zKd9f.874$...
    : What's wrong with the use of atoi in the following code? Why do I
    : get the error message:
    :
    : 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
    :
    :
    : char cBuffer[9];
    : void PushUnique(int);
    :
    : for(y = 0; y < 9; y++)
    : {
    : isF.getline(cBuffer,9);

    What do you intend cBuffer to contain here?
    I would think it is to contain the ascii representation
    of a single number -- such as: "16384" .
    When that is the case, a single call to atoi will extract
    that number:
    int i = atoi(cBuffer); // atoi takes a string, and
    // returns the value (e.g. i=16384)
    Note that strtoi supports better error reporting,
    and shall usually be preferred to atoi.

    : for(x = 0; x < 9; x++)
    : bd.Cell[x][y].PushUnique(atoi(cBuffer[x]));
    : }

    Are you trying to convert each digit into an integer?
    In this case, PushUnique( cBuffer[x] - '0' ); will do.
    But then you need to do the following:
    - make sure the read line is at least 9 characters
    long, or handle shorter strings appropriately.
    - BTW, if you need 9 characters then cBuffer must
    be at least 10 chars long (null-termination of C strings).
    - error handling: first check that each digit-character
    c is actually a decimal digit: ( (c>='0') && (c<='9') )

    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
    Ivan Vecerina, Oct 31, 2005
    #4
  5. John Smith

    Old Wolf Guest

    Chinchilla wrote:
    > the prototype for atoi is
    > int atoi(const char *str);
    > meaning it takes a pointer to an entire character string like "12345".
    >
    > if you wanna get the value of each digit and pass it to PushUnique just
    > subtract 48 from each character. Hackish, but should work.
    >
    > bd.Cell[x][y].PushUnique((int)(cBuffer[x] - 48));
    >


    Why would you write 48 instead of '0' ?? (Really -- I'm interested,
    it seems to be a fairly common practice). It is less portable
    and less readable and appears to have absolutely nothing in its
    favour, as far as I can see.
    Old Wolf, Oct 31, 2005
    #5
  6. John Smith

    Chinchilla Guest

    force of habit.
    Chinchilla, Oct 31, 2005
    #6
  7. John Smith

    Mike Wahler Guest

    "Chinchilla" <> wrote in message
    news:...

    Chinchilla:

    Please don't omit context (I've restored it below)

    > "Old Wolf" <> wrote in message
    > news:...
    > > Chinchilla wrote:
    > >>
    > >> bd.Cell[x][y].PushUnique((int)(cBuffer[x] - 48));
    > >>

    > >
    > > Why would you write 48 instead of '0' ?? (Really -- I'm interested,
    > > it seems to be a fairly common practice). It is less portable
    > > and less readable and appears to have absolutely nothing in its
    > > favour, as far as I can see.
    > >

    > force of habit.


    All the world is not ASCII.

    -Mike
    Mike Wahler, Oct 31, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alfonso Morra
    Replies:
    3
    Views:
    1,757
    Christopher Benson-Manica
    Aug 12, 2005
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,015
    Ian Collins
    May 9, 2006
  3. kaizen
    Replies:
    3
    Views:
    4,673
    Jim Langston
    Jan 21, 2006
  4. Replies:
    2
    Views:
    811
  5. Replies:
    0
    Views:
    860
Loading...

Share This Page