First of all I'd just do std::cout << cNum << std::endl; rather than passing a pointer to it...... but then there's also one or two things that could also do with changing before it works properly. Like sockman mentioned, you shouldn't be returning a pointer to something declared locally in the function. I'd change what you have to something more like this....
Code:#include <cstdio> #include <iostream> const char *ftostr(float dVar, char* buffer) { // char ans[20]; snprintf(buffer, 20, "%f", dVar); return buffer; } int main() { double fNum;// = 3.145; std::cout << "Enter a decimal value: "; std::cin >> fNum; char buffer[20]; //const char *cNum = ftostr(fNum); ftostr(fNum, buffer); std::cout << buffer << std::endl; system("pause"); return 0; }




Reply With Quote