<iostream.h> or <iostream>?

This is a discussion on <iostream.h> or <iostream>? within the C++ Programming forums, part of the General Programming Boards category; Here is a simple code: Code: // abc.cpp # include<iostream.h> void main() { int a=1,b=2,c; c=a+b; cout<<"c="<<c<<endl; } in VC++6, ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    30

    <iostream.h> or <iostream>?

    Here is a simple code:

    Code:
            // abc.cpp 
             # include<iostream.h>
               void main()
              {
                int a=1,b=2,c;
                c=a+b;
                cout<<"c="<<c<<endl;
              }
    in VC++6, it passed compling and linking all right. If use <iostream> to replace <iostream.h>, the vc6 prompts:

    error C2065: 'cout' : undeclared identifier
    error C2065: 'endl' : undeclared identifier

    BUT in VC7, use <iostream.h>, it says:

    fatal error cannot open include file 'iostream.h': no such file or directory.

    If changed to <iostream>, it says:

    Error c2065: 'cout': undeclared identifier.
    Error c2065: 'endl': undeclared identifier.

    What is the reason? How deal with?

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    the problem is namespacing

    The simplest solution is to add "using namespace std" after "#include <iostream>"

    Another bad thing is your use of void main, main should have type int

    EDIT
    Here is you code, nicely formatted and working
    Code:
    // abc.cpp 
    # include<iostream>
    using namespace std;
    int main()
    {
         int a=1,b=2,c;
         c=a+b;
         cout<<"c="<<c<<endl;
    }

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    <iostream> (as well as all the conventional C++ libraries) is defined under the std namespace. Either use the appropriate using namespace line at the top of your program:

    Code:
    #include <iostream>
    
    using namespace std;
    or preceed all functions and identifiers from those libraries with its namespace.

    Code:
    std::cout << std::endl;
    Last edited by SlyMaelstrom; 01-28-2006 at 02:59 PM.
    Sent from my iPad®

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    It's apparent that you are a beginner, so for now don't worry about what everything means, and just use this template for all your programs:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
     
         //your code here
    
         return 0;
    
    }
    Depending on what compiler you are using, you may need to add:
    Code:
    cin.get();
    before the line:
    Code:
    return 0;
    to keep the console window open so that you can see the output from your program.
    Last edited by 7stud; 01-28-2006 at 06:51 PM.

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Or use iostream.h to know whats going on.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    » Or use iostream.h to know whats going on.
    What do you mean by that?
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Just
    Code:
    #include<iostream.h>
    And no need to specify a namespace. If it compiles then it's OK.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #8
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    No it's not.
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  9. #9
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Don't post like that if so, i will answer: IT IS!
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  10. #10
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    And I will answer: <iostream.h> is a non-standard header in C++, so don't use it. Period. And if it compiles okay, throw out your compiler and get a new one.
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I don't know a single modern C++ compiler that won't compile <iostream.h>.

    They'll all give a warning though. It's a bad habit to use antiquated headers, as they might lose their functionality in the future. This is why you use <iostream>.
    Sent from my iPad®

  12. #12
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Hmm, you're right. Well then, if it doesn't give a warning...
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  13. #13
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    >>And if it compiles okay, throw out your compiler and get a new one.
    Then we should throw out the microsoft!!!
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,628
    Then we should throw out the microsoft!!!
    No, you should throw out Microsoft Visual C++ 6. If you insist on using a Microsoft compiler, the one from Visual C++ 2005 Express should be fine, especially since the package is available as a free download till November this year, or something like that, and you can continue using it for free even after that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    The cost of the IDE is not a problem for me (look where am i). But I don't have any problem with my compiler, I like it! I think it is not a problem for a compiler to compile a code, the problem is when it doesn't.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. <iostream> <stdio.h>
    By hero_bash in forum C++ Programming
    Replies: 13
    Last Post: 06-24-2006, 04:56 PM
  2. <iostream.h> or <iostream>?
    By {MaX} in forum C++ Programming
    Replies: 18
    Last Post: 06-05-2006, 12:52 AM
  3. <iostream.h> or <iostream> ??
    By Lazy Student in forum C++ Programming
    Replies: 8
    Last Post: 11-18-2002, 07:58 PM
  4. <iostream> over <iostream.h>
    By GreenCherry in forum C++ Programming
    Replies: 10
    Last Post: 10-16-2002, 11:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21