#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    14
    Rep Power
    0

    Beginner problems with Bloodshed Dev-C++ - please help!


    I've just installed Dev-C++ and can successfully compile Hello World. But when I try to run it, the IDE minimises briefly, the console window pops up for a fraction of second then the IDE comes back.

    What I found in the FAQ doesn't really seem to apply:

    1. When I compile my dos program and execute it, Dev-C++ minimizes and then restore in a second but nothing appears ?

    When creating a console application, be sure to uncheck “Do not create a console” in Project Options (when working with source files only uncheck “Create for win32” in Compiler Options).


    2. When executing my dos program, it closes automatically. How I can change this ?

    You can use an input function at the end of you source. The following code is an example for asking the user to press Enter to continue:

    #include <stdlib.h>

    int main()
    {
    system(“PAUSE”);
    return 0;
    }
    1. It's only one source file, so the project option doesn't apply.
    There doesn't seem to be a “Create for win32” in Compiler Options to uncheck...

    2. I get a compile error if I add system("PAUSE"); anywhere. (Whatever that means...)


    I'm following "C++ in 24 Hours" which suggests the following program text:
    (I don't understand it yet.)


    #include <iostream>

    int main()
    {
    std::cout << "Hello World!\n";
    return 0;
    }


    Can anyone help, please?

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Location
    NC, USA
    Posts
    364
    Rep Power
    12
    Open up a Command Prompt, navigate to the directory, and run the file from the command line.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    36
    Rep Power
    13
    Well, it is doing that because thats what you told it to do.

    #include <iostream> // include Input output header file

    int main() //beginning of program
    {
    std::cout << "Hello World!\n"; //Print Hello World to screen
    return 0; //close
    }
    To answer one of your questions, you are probabbly confused because there is std::cout instead of just cout. That is because cout is in the standard library. If you add the line "using namespace std;" under #include <iostream> you could just have cout.

    EX.
    Code:
    #include <iostream> // include Input output header file
    using namespace std;
    
    int main() //beginning of program
    {
    cout << "Hello World!\n"; //Print Hello World to screen
    return 0; //close
    }
    ==========================================
    If you want it to pause, there is many ways to do so. You could include conio.h to your program and use getch(); before return 0 this would make the program stop untill a key is pressed. Or you could use cin.ignore(); which will not continue untill enter is pressed. Now with your prob with system pause..

    1. You need to create a project even though it is just one source file. (it is still a project)

    2. Forget what that FAQ said create a Console Application.

    3. Make sure you have included: #include <stdlib.h>

    Now you should be able to compile with no problems. I believe Dev-C++ actually starts out with the code:

    #include <stdlib.h>

    int main()
    {
    system(“PAUSE”);
    return 0;
    }


    Hope this helps. Feel free to ask if you have any probs.

    0pt!x-
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    14
    Rep Power
    0
    Thanks!
    I think I'm starting to get the hang of it... yesterday was my very first day with C++!

    I think the main problem was that I was trying to do:

    system(“PAUSE”);

    without

    #include <stdlib.h>.


    But I'm a bit annoyed that my book didn't give a working version for Dev-C++, which it recommends.

    Anyway, now for chapter 2!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    36
    Rep Power
    13
    Yea, that must be it because if you dont have the include file, then C++ doesnt know what the function is.

    0pt!x-
    Last edited by Optix; November 2nd, 2002 at 09:06 PM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    system("PAUSE"); will work, but I wouldn't recommend it. There are better options.

    The best option is to execute it from a command prompt rather than the Run button of your compiler. This is true of all console applications. You may also find an option somewhere to keep consoles open after execution has finished. Visual C++ has this option somewhere, and I suspect C++ Builder does as well. Don't know about the package you're using.

    The second best option is to ask for input in your program. A call to system(char*) isn't actually a request for input. It's a request to run an outside program, one that happens to block on input. To actually ask for input, you would be better served to add the following code:

    Code:
    string blankline;
    cout << "Press enter to continue." << endl;
    getline(cin,blankline);
    That will cause the program to halt until you hit enter. If it doesn't, duplicate the last line (some runtime libraries, such as Microsoft's, have a tendency to cache a newline from prior input).

    The getline function reads a single line, no matter what it contains, from the provided data stream and stores it in the string.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    1
    Rep Power
    0
    Hi. I got the same book and I am enthusiastic about programming.
    Last year I learnt TrueBasic but now Im learning C++.
    I need help with getting DOS to pause. None of the things above work.
    If someone could show me an example of a program or tell me how to configure DOS I would be very apreacitive.
    Thanks.
  14. #8
  15. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    First, what compiler are you using and second what OS (I assume windows, but ask to make sure) and third what exactly are you trying?

    For VC++ this functions correctly:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(void) {
    
    	cout << "Hello world!\n";
    
    	cout << "Press enter to continue.\n";
    	getchar();
    
    	return (0);
    }
  16. #9
  17. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    Using a project in Dev-C++ is still a good idea even if you are using a single sourcefile. This is because the project templates set up the appropriate compiler/linker options to cirrectly build the typr of app you select. Also, the options are contained with the project. If you do not use a project and switch the global options for say a Windows GUI app, then reload the console app, the incorrect options will be applied. Using a project prevents this, because it has separate project options.

    And finally the console app project template has a sample main() that works!

    Yo can get all the Dev-C++ specific help you need at http://sourceforge.net/forum/forum.php?forum_id=48211.

    Clifford

IMN logo majestic logo threadwatch logo seochat tools logo