Hi can someone please tell me what I may have done wrong with the code below:
Anyway what I want to code to do is allow the user to pass a number convert it to a string, return the string and display it on the screen. I left the code I made below:
#include <iostream>
using namespace std;
char *IntToStr(int n_num){
char s_buff[25];
itoa(n_num,s_buff,10);
return s_buff;
}
int main(){
int a_numer;
char *a_str_num; // used to hold the converted number
a_numer = 512;// number to convert
a_str_num = IntToStr(512); // do the convert
cout << a_str_num; // return the result but ant working why.
return 0;
}
You can give the function a number and it does return. but does not return a vaild number it just returns s single char as shown below:
♦ Press any key to continue
anyway ideas thanks. I not been doing C++ for long so do expect this to be a falt on my part
but I still like to giet if fixed.
anyway thanks.
You need to either pass in a string buffer to the conversion function (which is how itoa works anyway) or return a type like an stl string.
e.g.
#include <string>
using namespace std;
string IntToString(int n)
{
char s_buff[25];
itoa(n_num,s_buff,10);
return string(s_buff);
}
int main(){
int a_number;
char *a_str_num; // used to hold the converted number
a_number = 512;// number to convert
a_str_num = IntToStr(a_number ).c_str(); // do the convert
cout << a_str_num; // return the result but ant working why.
return 0;
}