troubleshooting Question

Returning String from a function - Please Help

Avatar of dreamvb
dreamvb asked on
C++
5 Comments1 Solution478 ViewsLast Modified:
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:

&#9830;   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.
ASKER CERTIFIED SOLUTION
nonubik

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros