?
Solved

how to use itoa() function

Posted on 2006-05-30
3
Medium Priority
?
7,764 Views
Last Modified: 2011-08-18
Hi Experts,

I was using
#include <iostream.h>
#include <stdlib.h>

void main()
{

char *charValue = "123";
int value;
value = atoi(charValue);
cout << value << endl;
value = 256;

char buff[5];
itoa( value, buff, 10 );         <<<<-----

cout << buff << endl;

}

I am getting the error "itoa" unknown identifier.  But, I included the correct header file, I guess..  So, what is wrong.... I am running it in Linux. using g++....
0
Comment
Question by:thanesh
  • 2
3 Comments
 
LVL 4

Expert Comment

by:e_tadeu
ID: 16791883
itoa is not ANSI-C
Use sscanf (in C) or an istringstream (in C++) instead
0
 
LVL 4

Assisted Solution

by:e_tadeu
e_tadeu earned 400 total points
ID: 16791902
Sorry, use sprintf or ostringstream instead!

#include <sstream>

void main() {
  std::ostringstream oss;
  int value = 256;
  oss << value;

  cout << oss.str() << endl;
}
0
 
LVL 86

Accepted Solution

by:
jkr earned 600 total points
ID: 16791903
There is no 'itoa()' on Linux, '_itoa()' is a convenience function for MS compilers. Just use

#include <iostream> // NO .h!
#include <stdlib.h>
#include <stdio.h>

using namespace std;

void main()
{

char *charValue = "123";
int value;
value = atoi(charValue);
cout << value << endl;
value = 256;

char buff[5];
sprintf( buff,"%d", value);  
cout << buff << endl;

}
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

809 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