Menu

Unable to get (or see) program output

Mike
2008-01-22
2012-09-26
  • Mike

    Mike - 2008-01-22

    Problem

    I cannot get (or see) the output (‘Hello World 77’) from my first program.

    Analysis

    === Version: 4.9.9.2
    === OS: Windows XP Pro
    === Program:

    include <iostream>

    using namespace std;

    // declare the obligatory main() function
    int main()
    {
    // call the cout function to output a string
    cout << "Hello World 77\n";

    // getchar (); // Try this - no
    // cin.get(); // Try this - no
    system("pause"); // Try this - window only
    // std::cin.get(); // Try this - no
    // std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); // Try this - no

    // return 0 to confirm that the program ran ok
    return 0;
    }

    === Compile Log:
    Compiler: Default compiler
    Building Makefile: "C:\Dev-Cpp\Training\Makefile.win"
    Executing make...
    make.exe -f "C:\Dev-Cpp\Training\Makefile.win" all
    g++.exe -DDEBUG main.o HelloWorld.o -o "Training01.exe" -L"C:/Dev-Cpp/lib" -mwindows -g3
    Execution terminated
    Compilation successful

    I am new to C++ and to Dev-C++ so I know I’m doing something really silly and I know I’m going to look a twit so let’s get it over with.

    I have checked the ‘PLEASE READ BEFORE …’ section and I’ve tried various combinations of commands from various examples without success so here are my questions

    Q1. Why doesn’t ‘getchar ();’ work?
    Q2. Why doesn’t ‘cin.get();’ work?
    Q3. When using ‘system("pause");’ the DOS window does stay there but I cannot see the glorious words ‘Hello World 77’

    Also, sometimes the links from the ‘PLEASE READ BEFORE …’ section look like this:-
    Please come back in the begining of Dec to this page. It is curently not available

    SOLUTION

    I’d be ever so grateful for your comments

    Many thanks

    Mike

     
    • cpns

      cpns - 2008-01-23

      A) The chances are that <cstdio> or <stdio.h> are indirectly included by other headers such as <iostream>. This is by no means guaranteed, and so code that relies on this is non-portable. In C if a prototype is not found, a function returning int with unknowm parameters is assumed (usually with a warning). In C++ it is just illegal.

      B) If you use certain a project templates, they create initial code in main.cpp. You are free to remove this from the project and add your own. Presumably you did remove it from your project other wise you will have had two main() functions.

      C) Anywhere you like in any path that does not contain spaces, and is not in c:\dev-cpp (i.e. NOT C:\Dev-Cpp\Source as you suggested!). I use c:\projects\&lt;projectname>. You just navigate to or create an appropriate folder from within the file select box when you are prompted to save teh project file. Dev-Cpp has a nasty habit of offereing up c:\dev-cpp as default. I think you can fix this by changing the start directory property in the shortcut proerties used for starting teh IDE. Unfortunately you will have to do this for the desktop icon, the quick-start icon and teh start-menu entry.

      Clifford

       
    • Kurgusov

      Kurgusov - 2008-01-22

      Hi Mike,

      thats strange,

      include <iostream>

      int main ()
      {

      std::cout << "hello world 77\n" ;

      system ("pause') ;

      return 0 ;

      }

      will print the text to screen and hold the screen, for me.

      getchar() belongs to the <cstdio> header.
      I guess you'd have to #include <cstdio> to use it.

       
    • Kurgusov

      Kurgusov - 2008-01-22

      thats system( "pause" ) btw.

       
      • cpns

        cpns - 2008-01-22

        ... and it needs #include <stdlib.h> or #include <cstdlib>, but that is not his problem - he supressed the console window by specifying a GUI build.

         
    • cpns

      cpns - 2008-01-22

      All of the approaches you tried should work or at least do something (although you do need to include the appropriate headers for them to compile).

      However there are some issues with your build:

      First (and this is the cause of your problem); you appear to have told the linker to create a GUI application. (the -mwindows option says that). This supresses creation of a console window. The only reason you get a console window when you use system() is because system causes a new cmd.exe process to be spawned. Make sure you create your project initially as the correct type. You can change the type retrospectively in the Project settings dialog.

      Second; there is a bug in Dev-C++ that sometimes causes projects stored in c:\dev-cpp or sub-folders of that to fail to build correctly. It is a bad idea in any case to 'pollute' an applications installation folder with user files.

      Clifford

       
    • Kurgusov

      Kurgusov - 2008-01-22

      Even I knew that !

      Thanks for the head up Clifford.

      peace.

       
    • Kurgusov

      Kurgusov - 2008-01-22

      errm, I mean heads up.

      : )

      Kurgusov, constantly wrong.

       
    • Mike

      Mike - 2008-01-23

      Cliffod,

      Many thanks for your prompt reply, I really appreciate it.

      I went to ‘Project Options’ and changed ‘Type’ from ‘Win32 GUI’ to ‘Win32 Console’ and it works! BRILLIANT! I’m a lot happier now!

      I have a couple of follow up questions

      A). When I include the ‘getchar()’ statement and OMIT the ‘#include <cstdio>’ statement the program still compiles and runs OK.
      Q4. Is this correct or should there be an error/warning during compilation?

      B). Under my project ‘Training01’ there are two ‘.cpp’ files. The First one is ‘HelloWorld.cpp’ which I typed in and the second is ‘main.cpp’.
      Q5. Where did ‘main.cpp’ come from?

      C) Re:- It is a bad idea in any case to 'pollute' an applications installation folder with user files.
      Q6. Where, in your experience, should I put my user files (*.cpp files?) and what parameter should I change (I suppose it should be ‘Environment Options’ then ‘Files & Dir…’ then set ‘User’s Default Directory’ to read ‘C:\Dev-Cpp\Source’)

      Thanks once again

      Mike

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.