itoa function problem
Asked Answered
L

6

15

I'm working on Eclipse inside Ubuntu environment on my C++ project.

I use the itoa function (which works perfectly on Visual Studio) and the compiler complains that itoa is undeclared.

I included <stdio.h>, <stdlib.h>, <iostream> which doesn't help.

Lowminded answered 26/9, 2010 at 20:11 Comment(2)
please post your code, it's difficult to help without itBlaeberry
@CharlesB: this problem is very easy to diagnose without code. There's no need to post the code here.Adapt
V
14

www.cplusplus.com says:

This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers.

Therefore, I'd strongly suggest that you don't use it. However, you can achieve this quite straightforwardly using stringstream as follows:

stringstream ss;
ss << myInt;
string myString = ss.str();
Veinstone answered 26/9, 2010 at 20:36 Comment(3)
thanks, i tried it in visual studio and it does not recognize it, do i have to include somthing ?Lowminded
Error 4 error C2079: 'streamstringKey' uses undefined class 'std::basic_stringstream<_Elem,_Traits,_Alloc>' h:\workspace\hw5\hw5\vehicle.cpp 151Lowminded
Error 8 error C2228: left of '.str' must have class/struct/union h:\workspace\hw5\hw5\vehicle.cpp 154 Error 6 error C2297: '<<' : illegal, right operand has type 'char const ' h:\workspace\hw5\hw5\vehicle.cpp 153 const string Vehicle::GetKey() const{ stringstream streamstringKey; streamstringKey << GetTypeNum(); streamstringKey << m_licenseId; //m_licenseId is char string stringKey = streamstringKey.str(); return streamstringKey; }Lowminded
N
8

itoa() is not part of any standard so you shouldn't use it. There's better ways, i.e...

C:

int main() {
    char n_str[10];
    int n = 25;

    sprintf(n_str, "%d", n);

    return 0;
}

C++:

using namespace std;
int main() {
    ostringstream n_str;
    int n = 25;

    n_str << n;

    return 0;
}
Nickelson answered 26/9, 2010 at 20:17 Comment(2)
actually i need to append an int to a string.Lowminded
so, can i do it not with ostringstream, because my compiler does not recognize it.Lowminded
T
5

Boost way:

string str = boost::lexical_cast<string>(n);

Thracophrygian answered 26/9, 2010 at 21:0 Comment(0)
G
2

itoa depends on compiler, so better use the following methods :-

method 1 :If you are using c++11, just go for std::to_string. It will do the trick.

method 2 :sprintf works for both c & c++. ex- ex - to_string

#include <bits/stdc++.h>
using namespace std;
int main ()
{
  int i;
  char buffer [100];
  printf ("Enter a number: ");
  scanf ("%d",&i);

  string str = to_string(i);
  strcpy(buffer, str.c_str());

  cout << buffer << endl;
  return 0;
}

Note - compile using -std=c++0x.

C++ sprintf:

int main ()
{
int i;
  char buffer [100];
  printf ("Enter a number: ");
  scanf ("%d",&i);
  sprintf(buffer, "%d", i);
  return 0;
}`
Gelid answered 6/9, 2015 at 5:28 Comment(0)
A
1

you may use sprintf

char temp[5];
temp[0]="h"
temp[1]="e"
temp[2]="l"
temp[3]="l"
temp[5]='\0'
sprintf(temp+4,%d",9)
cout<<temp;

output would be :hell9

Affirmation answered 18/7, 2017 at 6:14 Comment(0)
S
0

Did you include stdlib.h? (Or rather, since you're using C++, cstdlib)

Suave answered 26/9, 2010 at 20:14 Comment(1)
That won't necessarily help, since itoa is non-standard.Ducan

© 2022 - 2024 — McMap. All rights reserved.