Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Compiling

  1. #1
    Banned Sylvie's Avatar
    Join Date
    Aug 2004
    Location
    Hell
    Posts
    4,136
    Blog Entries
    4

    Default Compiling

    Okay, I started C++ and I put in:

    Code:
    #include <iostream>
    using namespace std;    //introduces namespace std
    int main( void )
    {
              cout << "this is a test" ;
              return 0;
    }
    I compiled it using Dev C++, and then I clicked Run. Nothing happens.

    Does anyone know what could be wrong?

    The &lt;iostream&gt; was there, I just added the correct HTML entities to make it visible.
    - Sam

  2. #2
    i n v i s i b l e Tech Admin o_O's Avatar
    Join Date
    Jun 2001
    Location
    New Zealand
    Posts
    2,957
    Blog Entries
    1

    FFXIV Character

    Humphrey Squibbles (Sargatanas)

    Default

    Looks like you missed out "&lt;iostream&gt;".
    The forums have interpreted it as an HTML tag though, so you'll want to substitute "&lt;" with "&amp;lt;" and "&gt;" with "&amp;gt;" when posting code.
    Besides, it wouldn't compile if you missed that out.

    Code:
    #include &lt;iostream&gt;
    using namespace std;    //introduces namespace std
    int main( void )
    {
              cout << "this is a test" ;
              return 0;
    }
    Anyway, what operating system are you using?
    If you're using Windows, do you find that a DOS window pops up for a split second and then disappears?

    If so, then open up a command prompt (Start > run > cmd.exe) and type "yourProgramName.exe" and see if that works.

    Otherwise if you're using a Unix system, you might want to check that the compiler is creating and linking the output files in your pwd.
    Last edited by o_O; 11-11-2006 at 10:57 PM.

  3. #3
    Banned Sylvie's Avatar
    Join Date
    Aug 2004
    Location
    Hell
    Posts
    4,136
    Blog Entries
    4

    Default

    Okay, the command prompt thing didn't work. And it doesn't pop up for a split second either, its just that nothing happens at all.

  4. #4
    i n v i s i b l e Tech Admin o_O's Avatar
    Join Date
    Jun 2001
    Location
    New Zealand
    Posts
    2,957
    Blog Entries
    1

    FFXIV Character

    Humphrey Squibbles (Sargatanas)

    Default

    What operating system are you using? I recall you installed Ubuntu a while ago.
    It would make sense to use that because Linux comes with a C compiler.

    Also, are the output files being created in the present working directory?

    Try compiling from the command line:

    Code:
    gcc nameOfProgram.C -o nameOfProgram.exe

  5. #5
    Banned Sylvie's Avatar
    Join Date
    Aug 2004
    Location
    Hell
    Posts
    4,136
    Blog Entries
    4

    Default

    I'm using Windows.

  6. #6
    Prinny God Recognized Member Endless's Avatar
    Join Date
    Aug 2000
    Location
    Prinny Moon
    Posts
    2,641
    Contributions
    • Former Cid's Knight

    Default

    It won't compile as program.c with gcc. However, using g++ does compile this, and running does output "this is a test".

    Code:
    #include &lt;iostream>
    using namespace std;    //introduces namespace std
    int main( )
    {
              cout << "this is a test" ;
              return 0;
    }

    And then there is Death

  7. #7
    Banned Sylvie's Avatar
    Join Date
    Aug 2004
    Location
    Hell
    Posts
    4,136
    Blog Entries
    4

    Default

    Quote Originally Posted by Endless View Post
    It won't compile as program.c with gcc. However, using g++ does compile this, and running does output "this is a test".

    Code:
    #include <iostream>
    using namespace std;    //introduces namespace std
    int main( )
    {
              cout << "this is a test" ;
              return 0;
    }
    Sorry, but what does that mean? I'm just starting out, and the book I'm using told me to enter that in and compile it. The book said I'm not supposed to have any programming experience (which I don't, besides HTML). But anyway, I'm still confused.

  8. #8
    Banned Sylvie's Avatar
    Join Date
    Aug 2004
    Location
    Hell
    Posts
    4,136
    Blog Entries
    4

    Default

    Also, when I go to the command prompt and try to open the file manually, it seems to work somehow, but its like this:

    C:\Documents and Settings\Genji\My Documents> main.exe
    *presses enter*
    C:\Documents and Settings\Genji\My Documents>

  9. #9
    Got obliterated Recognized Member Shoeberto's Avatar
    Join Date
    Jun 2000
    Location
    THE OC BABY
    Posts
    12,018
    Blog Entries
    1
    Contributions
    • Former Cid's Knight

    Default

    Different compilers get picky about small coding descrepancies. Try doing little things like changing int main ( void ) to int main(void), or even int main(). I always use int main(), myself, and in my experience with DevC++ it compiles fine.


  10. #10
    Banned Sylvie's Avatar
    Join Date
    Aug 2004
    Location
    Hell
    Posts
    4,136
    Blog Entries
    4

    Default

    Still isn't working. :\

    I tried your suggestions Hsu, and they didn't work. This is starting to piss me off, because I really want to learn C++ but this stupid problem is getting in my way.

  11. #11
    i n v i s i b l e Tech Admin o_O's Avatar
    Join Date
    Jun 2001
    Location
    New Zealand
    Posts
    2,957
    Blog Entries
    1

    FFXIV Character

    Humphrey Squibbles (Sargatanas)

    Default

    Quote Originally Posted by Endless View Post
    It won't compile as program.c with gcc. However, using g++ does compile this, and running does output "this is a test".
    That's what I meant.
    I much prefer C to C++.

    I know that C compilers are very picky about comments; you have to use block comments:
    Code:
    /* comment */
    rather than inline comments:
    Code:
    //comment
    C++ supports inline comments, but the compiler could be getting fussy about it, since it technically MingW is a port of gcc. Try removing:
    Code:
        //introduces namespace std
    and recompiling, and see if it produces output then.

  12. #12
    Got obliterated Recognized Member Shoeberto's Avatar
    Join Date
    Jun 2000
    Location
    THE OC BABY
    Posts
    12,018
    Blog Entries
    1
    Contributions
    • Former Cid's Knight

    Default

    It compiles fine for me with DevC++, so I'm not sure.

    Maybe try reinstalling, and make sure you're getting the DevC++ with MiniGW/GCC package from this page:
    http://www.bloodshed.net/dev/devcpp.html


  13. #13
    Banned Sylvie's Avatar
    Join Date
    Aug 2004
    Location
    Hell
    Posts
    4,136
    Blog Entries
    4

    Default

    Aha! I found what I needed to do. I put

    Code:
    system("PAUSE");
    at the end of a line and that worked.

    Now the other problem.

    It says "this is a testPress any key to continue"

    I want it to be... more organized. Like say:

    this is a test

    Press any key to continue.

  14. #14
    Got obliterated Recognized Member Shoeberto's Avatar
    Join Date
    Jun 2000
    Location
    THE OC BABY
    Posts
    12,018
    Blog Entries
    1
    Contributions
    • Former Cid's Knight

    Default

    Apparantly you've not gotten far in your C++ studies

    Your choices are either to:
    1) add on to your cout statement to say cout << "this is a test << endl;
    The endl is endline, which starts a new line.
    2) add on to your "this is a test" to say "this is a test\n"
    The \n is an escape character (you'll learn about those) that makes a new line. Both do the same and work in different situations, it's really all personal preference.


  15. #15
    i n v i s i b l e Tech Admin o_O's Avatar
    Join Date
    Jun 2001
    Location
    New Zealand
    Posts
    2,957
    Blog Entries
    1

    FFXIV Character

    Humphrey Squibbles (Sargatanas)

    Default

    Also, you should check the compiler options. What is the actual command that DevC++ is using to compile the code?

    If it has any of the flags -c -E or -S, it won't output anything executable.

    And one more thing; I don't know if the file extension matters in Windows, but make sure your source code has a valid C++ extension (.C, .cc, .c++, .cxx, etc.).

    EDIT: Ok, too late.
    I didn't realise you need to null-terminate your strings manually in C++.
    The reason you need it there is because a string is represented in C++ by some characters followed by a newline (\n). The \n is to tell the compiler that it has reached the end of the string, and should stop trying to process the data in memory as a string. When you don't have it, the "string" will extend far past the end of the string into a block of memory that contains other data.
    Last edited by o_O; 11-11-2006 at 10:53 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •