CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2009
    Posts
    7

    Could someone please end some slight confusion?

    Hey guys sorry for the another post so fast.

    I have been learning C++ for a few days now and i have come across this:

    #include <iostream>

    int main() {
    std::cout << "Hello, World!" << std::endl;
    return EXIT_SUCCESS;

    }

    but the way i have learned is this:

    #include <iostream>
    using namespace std;

    int main(void)

    {
    cout << "Hello, World!" <<endl <<endl;


    system("PAUSE>NUL");

    return 0;

    }

    can someone please explain, Thanks!

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Could someone please end some slight confusion?

    What do you need explained? Really, they are both valid programs. The first returns immediately after printing "Hello, World!" (which exits the program). The second executes the command "PAUSE" after printing "Hello, World!"

    Viggy

  3. #3
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Could someone please end some slight confusion?

    One pauses execution and lets you see the output, the other exits immediately so you'll see nothing (unless the IDE pauses it for you, which is usually the case)

  4. #4
    Join Date
    Nov 2009
    Posts
    7

    Re: Could someone please end some slight confusion?

    but what about the std:: is that because in the other program i used using namespace std ??

  5. #5
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Could someone please end some slight confusion?

    Quote Originally Posted by JonnySnip3r View Post
    but what about the std:: is that because in the other program i used using namespace std ??
    first form (using declaration) has the control over member introductions,
    where as the latter does not (using directive).
    Code:
    #include <iostream>
    
    int main() {
    std::cout << "Hello, World!" << std::endl;
    return EXIT_SUCCESS;
    
    }
    
    but the way i have learned is this:
    
    #include <iostream>
    using namespace std;
    
    int main(void)
    
    {
    cout << "Hello, World!" <<endl <<endl;
    
    
    system("PAUSE>NUL");
    
    return 0;
    
    }
    Neither of these programs are properly written

  6. #6
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Could someone please end some slight confusion?

    Quote Originally Posted by ninja9578 View Post
    One pauses execution and lets you see the output, the other exits immediately so you'll see nothing (unless the IDE pauses it for you, which is usually the case)
    Not necessarily.
    The output will reamin in the console if the program is run at the command prompt

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Could someone please end some slight confusion?

    Quote Originally Posted by JonnySnip3r View Post
    but what about the std:: is that because in the other program i used using namespace std ??
    Essentially yes. There are some slight differences between the two approaches (you can read about argument-dependent lookup if you want), but for most purposes you can consider explicit namespace specification versus a "using namespace" statement to be equivalent.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured