|
-
September 19th, 2003, 02:05 PM
#1
Member
Help with Dev C++
I've recently started a computer science course at school and downloaded Dev C++ so i could do some compiling at home. I tried to make a basic program that made an output statement and it claimed it was invalid.... Here's an example of what it looked like:
# include<iostream>
int main()
{
cout<<"hi \n";
return 0;
}
The part in read was identified to not be recognized by the compiler, does anyone know why this is, or what I can do to make "cout<<" regcognized?
-
September 19th, 2003, 02:08 PM
#2
cout is in the namespace std. Your program is in the default (unnamed) namespace.
Therefore it isn't going to work unless you either replace cout with std::cout
or put
Code:
using namespace std;
before main
Slarty
-
September 19th, 2003, 02:59 PM
#3
slarty said it but there are a couple different versions of doing it. You are missing the using namespace std; directive, and therefore, when the debugger hits that line of code, it doesnt reconize cout <<. Anyways there are three different ways to fix your problem:
First way, use a using directive (just like what saltry said), this is considered bad programming practice, but i dont care right now:
Code:
#include <iostream>
using namespace std;
int main (void)
{
cout << "hi \n";
}
You could also use a using directive, there are two different forms of this bellow ill show you both:
Code:
#include <iostream>
using std::cout;
int main (void)
{
cout << "hi \n";
}
or
Code:
#include <iostream>
int main (void)
{
std::cout << "Hi \n";
}
also i wanted to point out a few warnning that you might have recieved when you tried to compile:
You int main() function doesnt return anything, change it by adding return 0; at the end of the function or instead of typing int main(), just type void main();
also your output was cout << "hi \n";, a better way to code this is to write it like this:
Code:
#include <iostream>
using std::cout;
using std::endl;
int main(void)
{
cout << "Hi" << endl;
return 0;
}
that is the perfect version of the code you typed above....endl; is like the newline escape character (\n)
EDIT: Oops, didnt see that you had your main function return something...my bad, well i am too lazy to change my code examples, by the way, example number 2 is the best programming style
Support your right to arm bears.

^^This was the first video game which i played on an old win3.1 box
-
September 19th, 2003, 05:17 PM
#4
Originally posted here by White_Eskimo
First way, use a using directive (just like what saltry said), this is considered bad programming practice, but i dont care right now:
Why is this considered bad programming practice? The final .exe is no different. I always figured it was just a matter of programmer style.
That which does not kill me makes me stronger -- Friedrich Nietzche
-
September 19th, 2003, 05:39 PM
#5
S3cur|ty4ng31 its "iostream.h"
-
September 19th, 2003, 05:48 PM
#6
Originally posted here by br_fusion
S3cur|ty4ng31 its "iostream.h"
I asked about what was wrong with has namespace directives????
And it is not always iostream.h for the record. Many systems now drop the .h on a lot of the system libraries. It really depends on the system and compiler.
That which does not kill me makes me stronger -- Friedrich Nietzche
-
September 19th, 2003, 10:38 PM
#7
I asked about what was wrong with has namespace directives????
And it is not always iostream.h for the record. Many systems now drop the .h on a lot of the system libraries. It really depends on the system and compiler.
Actually, on *every* compiler I've ever used (including MSVC, Dev C++, gcc, xlC, Metroworks, etc., iostream.h works. They didn't change the system libraries, they simply changed the way that the compiler interprets the #include statement. As for dropping the .h, most compilers will only allow this if you use the using namespace directive. Most of what I've read lately about ANSI C++ standards is that the new "standard" is to use the using directives and drop the .h.
By the way, adding the using namespace may not even work for your system. Many compilers (I don't have Dev C++ installed at the moment so I can't test it), won't allow a space between the # and include (just fyi).
AJ
-
September 20th, 2003, 03:00 AM
#8
Why is this considered bad programming practice? The final .exe is no different. I always figured it was just a matter of programmer style.
Yeah you are right my bad, i meant programming style...using a using directive includes more stuff than just specifing a using declaration (if you know what i mean kind of, instead of using everything in namespace you choose what you use). Not a big difference, just style, but it may speed up compile time by a tiny fraction of a milisecond
Support your right to arm bears.

^^This was the first video game which i played on an old win3.1 box
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|