Help with Dev C++
Results 1 to 8 of 8

Thread: Help with Dev C++

  1. #1
    Member
    Join Date
    Jun 2003
    Posts
    42

    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?

  2. #2
    Senior Member
    Join Date
    Jan 2002
    Posts
    1,207
    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

  3. #3
    Senior Member
    Join Date
    May 2002
    Posts
    344
    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

  4. #4
    Senior Member
    Join Date
    Jun 2003
    Posts
    236
    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

  5. #5
    Senior Member br_fusion's Avatar
    Join Date
    Apr 2002
    Posts
    167
    S3cur|ty4ng31 its "iostream.h"

  6. #6
    Senior Member
    Join Date
    Jun 2003
    Posts
    236
    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

  7. #7
    The Iceman Cometh
    Join Date
    Aug 2001
    Posts
    1,209
    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

  8. #8
    Senior Member
    Join Date
    May 2002
    Posts
    344
    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
  •