atoi and itoa

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Myrna
    New Member
    • May 2012
    • 1

    atoi and itoa

    I wrote itoa function.
    Code:
    #include <iostream>
    #include <string>>
    char ch
    char name
    int main()
    {
    char buf (10);
    int somenum = 20
    itoa (somenum, buf, 10
    string astring = buf;
    }
    Error message on int main. In advance, thank you.


    atoi
    Code:
    #include <iostream>
    #include <string>
    
    string digits = "153"
    int num;
    
    //Enter your number
    
    int atoi (char *s);
    cout << num << end1;
    Last edited by Banfa; May 2 '12, 09:12 AM.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You will need to #include <locale> for the complier to accept atoi and itoa.

    You are also missing some semi-colons.

    Your atoi code is very bad.
    Your variable is named digits but you never use it.
    You have an int num which you display witout ever putting a value in the variable.
    You show a function prototype for atoi but never call the function.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      atoi is declared in stdlib.h (or cstdlib). itoa is not declared anywhere it is not a part of the C standard library but rather an extension in some, but not all, compilers.

      In general terms if you are using C++ then you should be using a stringstream not atoi, itoa or any other of the C functions to do with converting numbers to strings and strings to numbers.

      Comment

      Working...