No output- std::cout

This is a discussion on No output- std::cout within the C++ Programming forums, part of the General Programming Boards category; Hi all, huge noob here. Ive only ever used one scripting language prior to my jump, and these are my ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    3

    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.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,529
    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.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    3
    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?

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,529
    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.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    3
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 02:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 02:29 PM
  3. Replies: 4
    Last Post: 11-30-2005, 03:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 04:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21