Thread: iostream vs iostream.h

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    12

    iostream vs iostream.h

    Finally got around to learning C++ but thats the warning I got from just putting in a Hello World program with <iostream.h> for a header:

    backward_warning.h:32 #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>.

    So I looked around and found that I should replace it with <iostream>, and thus these errors come up and the program no longer works

    [Warning] In function `int main()':
    4 ` cout' undeclared (first use this function)
    (Each undeclared identifier is reported only once for each function it appears)
    4 ` endl' undeclared (first use this function)
    Code:
    #include <iostream>
    int main()
    {
    cout<<"Hello World"<<endl;
    return 0;
    }
    I also tried it in this program
    Code:
    #include <iostream> 
    int main() 
    { 
    int thisisanumber; 
    cout<<"Please enter a number:";
    cin>>thisisanumber; 
    cout<<"You entered: "<<thisisanumber;
    return 0; 
    }
    it didn't run and had errors really similar to the hello world program except now its saying cout and cin is undeclared, but it works with iostream.h but the warning with the use of iostream.h came up again

    And I have no clue what the errors mean
    Also could someone explain to me what main does? I know that int main is declaring function main to return an integer but...what does that mean/do? I think return 0 just ends the program but is there a way to just leave the screen? I always have to add in an extra cin line so I can see if the last line of code actually worked
    Rawr.

  2. #2
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Hi there,

    You're getting errors because in the new headers (without the .h) all the functions etc. (cout etc.) are in what's called a namespace, they are in a namespace called std. So you need to tell the compiler that it has to look in that namespace to find those functions etc.

    There are a few ways to do this:
    Code:
    using namespace std;
    This brings all members of the std namespace into scope
    Code:
    using std::cout; //etc...
    This brings only that member of the std namespace into scope.
    Code:
    std::cout << "Hello" << std::endl;
    This specifies each individual use of cout, endl etc. is meant to use the version in the std namespace. This is the preferred method. But you have to type std:: before every use of a member.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  3. #3
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    And to answer your second question,

    main() is the function that the operating system calls to begin your program. Every program must have a main() function. It's possible not to return a value from main by declaring it a void function, but that's evil and you shouldn't do it.

    The reason you return from main is to indicate success or failure of your program. 0 means everything went well, and you should return 1 or greater to indicate that your program failed.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090

    Re: iostream vs iostream.h

    Originally posted by LegendsEnd
    Also could someone explain to me what main does? I know that int main is declaring function main to return an integer but...what does that mean/do? I think return 0 just ends the program but is there a way to just leave the screen? I always have to add in an extra cin line so I can see if the last line of code actually worked
    A C++ program is made up of functions. The first function that is called when you start your program is main. That's just the way it is. All functions can return a value to the code that called them if they want. By standard convention, main returns an integer to the code that calls it. You don't see the code that calls it, it is the operating system, but that integer is still returned. Most people return 0 because that generally means success. Technically you can return whatever number you want, but it is generally better to stick to convention unless you have a reason not to. So just always put return 0, or if you want to signal a major error try returning a non-zero number (like 1).

    As far as adding a cin so you can see the output, that is also very common. Check the FAQs on this board for more information and other ways to stop your results from disappearing, and for why it is int main and <iostream> and all that other good stuff.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    12
    thanks for the help regarding the namespace/main/return use, and finally can you/anyone else tell me a way to keep the program up? it closes right after return 0 but I want it to actually stay there instead of flashing for a second

    edit: whoops person above posted faster than me
    Rawr.

  6. #6
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    You can put:
    Code:
    cin.get(); // just before return or
    system("pause"); // windows only
    edit: or you could execute from the command prompt rather than executing the executable on its own.
    Last edited by HybridM; 03-25-2004 at 09:13 PM.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    12
    hmm, about the 3 methods to have namespaces incorporated into the program, you metnioned the preferred method being
    Code:
    std::cout << "hello" << std::endl;
    but why not just use
    Code:
    using namespace std;
    so you wont have to type it again, what does it affect?
    Rawr.

  8. #8
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    As far as I know it's not really any different, but it's considered bad practice to bring all those names in to scope when you're not going to use them.

    And it's also better to use the third method because you immediately know that you're using the standard cout or std::string etc. (you can actually declare your own variables and objects with the same name as standard ones, but it's probably not a good idea).
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >so you wont have to type it again, what does it affect?
    In small programs it doesn't matter, but in large projects that use many different libraries, naming conflicts are much more likely. Like OO, namespaces are a large scale organizational tool.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Originally posted by HybridM
    There are a few ways to do this:
    Code:
    using namespace std;
    This brings all members of the std namespace into scope
    Code:
    using std::cout; //etc...
    This brings only that member of the std namespace into scope.
    It's worth noting that at local scope the latter adds a name to the scope, the former doesn't.

    Originally posted by Prelude
    In small programs it doesn't matter, but in large projects that use many different libraries, naming conflicts are much more likely.
    It's surprising how often name clashes can occur even in small programs, where a well chosen identifier happens to match a lesser known entity from namespace std.
    Gambling. The new yoga, with cash prizes!

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    I like namespace alot because you have consistent naming. For example, I have a bunch of action classes such as GoRight, GoLeft but these have similar behavior for each game object so I give each game object a different name space.

  12. #12
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    I don't mean to intrude on your thread here....

    But, I have 2 questions.

    When returning a value to the operating system through main(), how would one deal with the argument?

    And, with namespaces. When your coding with:
    Code:
    using namespace foo;
    Since that brings the entire namespace in scope, would the filesize of the binary be larger, then if I were to go:
    Code:
    using foo::bar;
    ?
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  13. #13
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    When returning a value to the operating system through main(), how would one deal with the argument?
    No effect whatsoever. Main is function that is an entry point from C library code. What this means is that the compiler just as to generate assembly code with the main function, link the c-library in, and it automatically gets called.

    Since that brings the entire namespace in scope, would the filesize of the binary be larger, then if I were to go:
    No, namespaces are more of a naming scheme that do not really effect the actual code generation. #includes, on the other hand, just do a simple text substitution of the header file and so they can increase the size of the program.

  14. #14
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Originally posted by okinrus
    No effect whatsoever. Main is function that is an entry point from C library code. What this means is that the compiler just as to generate assembly code with the main function, link the c-library in, and it automatically gets called.


    No, namespaces are more of a naming scheme that do not really effect the actual code generation. #includes, on the other hand, just do a simple text substitution of the header file and so they can increase the size of the program.
    Ahh, thank you Very much appreciated, I was speculating this for a while and couldn't come up with a logical explanation..
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  15. #15
    Registered User
    Join Date
    Mar 2004
    Posts
    15
    <iostream.h> works for me as a header - is it different in windows is that why you use namespace ? - Our computer lab I use at school is a Unix lab

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. iostream.h (vs) iostream
    By Brain Cell in forum C++ Programming
    Replies: 5
    Last Post: 11-05-2004, 08:20 AM
  2. iostream or iostream.h?
    By serruya in forum C++ Programming
    Replies: 15
    Last Post: 05-05-2003, 07:41 AM
  3. Problem during compile (iostream.h error)
    By JoJo in forum C Programming
    Replies: 4
    Last Post: 04-29-2003, 06:58 PM
  4. Is there a user input command without using iostream.h?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 11-04-2001, 02:15 AM
  5. << in iostream
    By badman in forum C++ Programming
    Replies: 8
    Last Post: 10-18-2001, 10:19 PM