PDA

View Full Version : Compiling



Sylvie
11-11-2006, 07:59 AM
Okay, I started C++ and I put in:


#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

o_O
11-11-2006, 08:17 AM
Looks like you missed out "&lt;iostream&gt;". :p
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. :p
Besides, it wouldn't compile if you missed that out.



#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.

Sylvie
11-11-2006, 08:42 AM
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.

o_O
11-11-2006, 09:32 AM
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:



gcc nameOfProgram.C -o nameOfProgram.exe

Sylvie
11-11-2006, 09:33 AM
I'm using Windows.

Endless
11-11-2006, 01:26 PM
It won't compile as program.c with gcc. However, using g++ does compile this, and running does output "this is a test".


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

Sylvie
11-11-2006, 06:20 PM
It won't compile as program.c with gcc. However, using g++ does compile this, and running does output "this is a test".


#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. :(

Sylvie
11-11-2006, 08:45 PM
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>

Shoeberto
11-11-2006, 09:58 PM
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.

Sylvie
11-11-2006, 10:17 PM
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.

o_O
11-11-2006, 10:19 PM
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. :p
I much prefer C to C++.

I know that C compilers are very picky about comments; you have to use block comments:

/* comment */
rather than inline comments:

//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:

//introduces namespace std
and recompiling, and see if it produces output then.

Shoeberto
11-11-2006, 10:22 PM
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

Sylvie
11-11-2006, 10:27 PM
Aha! I found what I needed to do. I put


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

Now the other problem. :p

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.

Shoeberto
11-11-2006, 10:30 PM
Apparantly you've not gotten far in your C++ studies :p

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.

o_O
11-11-2006, 10:30 PM
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. :p
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.

Sylvie
11-11-2006, 10:35 PM
Alright, thanks Hsu. That helped a lot. :D And yeah, I'm not too far into my C++ studies, so I had a hard time understanding some things said here. But I got it working, so yay.