Why does this code work?

Posts 15 of 5 · Page 1 of 1
Why does this code work?
So i'm learning c++ by reading a book, and i started messing around with the code. This is how it usually looks like:
{
using namespace std; //for cout
std::cout << "How are you?"; //writes to display
std::cout << std::endl; //goes to next line
std::cout << "I'm fine atleast"; //writes to display
cin.get(); //makes the program stay on the screen until enter is pressed
return 0; //terminate main
}

Now that seems perfectly fine which it is, now why does this work?
{
using namespace std; //for cout
std::cout << "How are you?"; //writes to display
std::cout << std::endl; //goes to next line
std:cout << "I'm fine atleast"; //writes to display
cin.get(); //makes the program stay on the screen until enter is pressed
return 0; //terminate main
}
I removed one of the : from the std:cout << "I'm fine atleast";
I tried to remove it from any of the other std::cout but then the build got errors, someone please explain
I'd really appreciate if you put the code in [CODE] so it's more easy to read it.

Anyway, to the code...

First of all, if you have typed
Code:
using namespace std;
There's no need for you to type:
Code:
std::cout << "How are you?";
This is enough:
Code:
cout << "How are you?";
I wrote a much more cleaner version of the code here:

Code:
#include <iostream> // Importing the library for this to work

using namespace std; // The standard namespace instead of using std:: infront of cout. Dont use both.

int main() // Always have a main function
{

	cout << "How are you?" << endl;
	cout << "I'm fine atleast";
	cin.get();
	return 0;

}
Just a bit curious, What book are you reading?

Oops, Almost forgot to ask.. What kind of compiler are you using?
Quote Originally Posted by ERGaergaergaergaergearg View Post
I'd really appreciate if you put the code in [CODE] so it's more easy to read it.

Anyway, to the code...

First of all, if you have typed
Code:
using namespace std;
There's no need for you to type:
Code:
std::cout << "How are you?";
This is enough:
Code:
cout << "How are you?";
I wrote a much more cleaner version of the code here:

Code:
#include <iostream> // Importing the library for this to work

using namespace std; // The standard namespace instead of using std:: infront of cout. Dont use both.

int main() // Always have a main function
{

	cout << "How are you?" << endl;
	cout << "I'm fine atleast";
	cin.get();
	return 0;

}
Just a bit curious, What book are you reading?

Oops, Almost forgot to ask.. What kind of compiler are you using?
I actually know that i dont have to write it like that i just tested it for the case of testing, you know, exploring makes you better at stuff.
I'm reading C++ Primal Plus 6th edition.
I'm using Microsoft Visual C++ 2010 Express

Also, if interested, here's the actual code i wrote:
Code:
#include <iostream> //includes input/output, must use for cout and cin functions
int main() //Main is a MUST unless it's a DLL
{
	using namespace std; //for cout
	cout << "How are you?"; //writes to display
	cout << std::endl; //goes to next line
	cout << "I'm fine atleast"; //writes to display
	cin.get(); //makes the program stay on the screen until enter is pressed
	return 0; //terminate main
}
The "why doesn't this work" is because of this line:
std:cout << "I'm fine atleast"; //writes to display

You are missing a : in the std:: namespace prefix.
Quote Originally Posted by atom0s View Post
The "why doesn't this work" is because of this line:
std:cout << "I'm fine atleast"; //writes to display

You are missing a : in the std:: namespace prefix.
it works, thats why im wondering lol

edit: Nvm, found out why. /thread
Posts 15 of 5 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?