I am trying to write a function which converts a number to a string. But when I return from the function, the reviving string - according to debug mode - still is empty, so the program outputs nothing.

What am I doing wrong?

#include <iostream>
#include <string>
using namespace std;

string convertNumberToString(int num);
 
int main()
{
	string num=convertNumberToString(7);

	cout<<num<<endl;

	fflush(stdin);
	cin.get();
}

string convertNumberToString(int num)
{
	string scoreString;

	itoa(num,const_cast<char *> (scoreString.c_str()),10);

	return scoreString;

}

Recommended Answers

All 2 Replies

Don't use itoa, but rather use std::stringstream

This should help: Code Snippet
Also, the function itoa() is not defined in the ANSI-C++ standard, so it may not be supported on all platforms.

Hope this helped

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.