This code cant keep cmd open.

This is a discussion on This code cant keep cmd open. within the C++ Programming forums, part of the General Programming Boards category; I'm reading Accelerated C++ and on their first example they have the code: Code: // a small C++ program #include ...

  1. #1
    Programmer
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    33

    This code cant keep cmd open.

    I'm reading Accelerated C++ and on their first example they have the code:

    Code:
    // a small C++ program
    #include <iostream>
    
    int main()
    {
        std::cout << "Hello, world!" << std::endl;
        return 0;
    }
    When it starts the cmd closes and won't display Hello, World!

    However from another book I was taught to do it like this:

    Code:
    // a small C++ program
    #include <iostream>
    
    useing namespace std; // (I use this)
    
    int main()
    {
        cout << "Hello, world!\n";  // Instead of std::cout << just cout and I use \n for next line.
        return 0;
    }
    This code works fine, why is std::cout taught then?
    (Expert Visual Basic Programmer)
    (Newbie C/C++ Programmer)

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    std::cout allows you to call cout without switching namespaces. You can do it whichever way you want. This shouldn't have any affect on weather or not the command window closes.

  3. #3
    Programmer
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    33
    So your saying instead of using "using namespace std;" (Might be useing don't no) you can just use std::cout instead?

    EDIT: Ok I got it to work, I added this line before return 0;

    Code:
    std::cin.get();
    So it works now!
    Last edited by dimirpaw; 11-27-2005 at 05:31 PM.
    (Expert Visual Basic Programmer)
    (Newbie C/C++ Programmer)

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Namespaces are there to avoid naming clashes. A lot of the time, it won't be a problem to put "using namespace std;" but if you use another library, you might have two classes/functions/variables/etc with the same name. For example:
    Code:
    #include <iostream>
    int cin;
    
    using namespace std;
    
    int main()
    {
      cin>>cin;
    }
    This will obviously generate an ambiguity error. It's easy to resolve, though:
    Code:
    #include <iostream>
    int cin;
    
    using namespace std;
    
    int main()
    {
      std::cin>>::cin;
    }
    Edit: Board's running really slow...Anyways, I was trying to say that a good reason to avoid "using" statements is for code like this:
    Code:
    #include <iostream>
    
    
    using namespace std;
    
    int main()
    {
      int cin;
      cin>>cin;
    }
    Doesn't generate any compile errors but could easily lead to some problems
    Last edited by JaWiB; 11-27-2005 at 06:29 PM.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Programmer
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    33
    Thanks! I learned even in Visual Basic that you should use diffrent names for strings. On one of my books it has a big list of names to avoid using aswell! I must say learning C++ has been a lot of fun with the book I'm reading! Even though I'm an expert in VB, when I learn C++ I most likely will never use VB again.
    (Expert Visual Basic Programmer)
    (Newbie C/C++ Programmer)

  6. #6
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    I'm in the middle of Accelerated C++, and the authors, I believe, are just trying to have you learn and appreciate the concept of namespaces and the using statements over time. You start out with
    Code:
    std::cout
    which specifies the cout within the std namespace for just this one use, then later you'll see instead
    Code:
    using std::cout;
    near the beginning of your code, which lets the compiler know that you want to use the cout from the std namespace by default. (You can specify a different cout with something like other_namespace::cout within the program if you need to.) Eventually, I imagine, we'll get on to
    Code:
    using namespace std;
    which says that you'll be exclusively using names from the std namespace, except where you opt out as above.
    There is a difference between tedious and difficult.

  7. #7
    Programmer
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    33
    Alright thanks! I'm only on Chapter 2, about strings. Which I knew before but going through it all anyways.
    (Expert Visual Basic Programmer)
    (Newbie C/C++ Programmer)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help porting code lseek(), open() and read()
    By mohankarthik in forum C Programming
    Replies: 8
    Last Post: 09-12-2008, 08:15 AM
  2. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 04:39 PM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Problems with open and save boxes
    By pinkcheese in forum Windows Programming
    Replies: 3
    Last Post: 05-21-2002, 06:03 PM

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