-
No output- std::cout
Hi all, huge noob here.
Ive only ever used one scripting language prior to my jump, and these are my early days of C++, please excuse my ignorance.
I have been reading the brilliant tuts on your site, and decided to register on the forums to try and find out a way to convert a long type var to a string.
After reading the FAQs I found a solution which worked fine on its own, and highly delighted me.
However: This hurled me into another problem which I cannot find the answer to at my present level of experience with C++.
My problem is that when I tried to use the code I found in my own project it failed to output anything at all.
I have stripped it down to its bare bones to demonstrate my problem, and hope someone can help me understand my error(s).
Code:
#include <iostream>
#include <sstream>
#include <string>
#include <Windows.h>
std::string IntToString ( int number )
{
std::ostringstream oss;
// Works just like cout
oss<< number;
// Return the underlying string
return oss.str();
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
std::cout<< "Hi World" << "\n";
system("Pause");
return 0;
}
std::cout<< "Hi World" << "\n"; shows nothing at all.
I would really appreciate any help.
-
WinMain is the entry point for Windows programs, specifically ones that have GUI. If you want a console (where cout would work) you have to create a window. So yeah, you're doing Windows programming when you really want to do a console project.
-
Thank you for the reply.
Unfortunately, for some reason, when I try to use the external dll essential to me in a console project, I get compile time errors, (although it works in debug) so for now, I'd like to stick to whats I have working.
I dont actually have a GUI in my project (and maybe I wont need one, Im not certain yet).
Do you think the actuall IntToString() function will be ok in my project ie just the cout failing?
Or do I need to find another way?
-
I don't know if the cout is failing, but that is less important now, since you have no windows to see anything.
The entry point for a console project is main. If you create a console project, your IDE should provide you with a bare-bones file with main in it. I think you just created the wrong type of project by accident and started using WINAPI. Not the end of the world, but you do want a different project.
The IntToString function does look like it is written properly, though testing is always important.
-
Okay, I'll go off and do some more reading and testing while trying to get my code to compile in a console project.
Thanks once again for you help and time.