Hallo, I have been frustarting myself with trying to dispaly anything on my console application using cout but i just can seem to do it.
Pleas help
Printable View
Hallo, I have been frustarting myself with trying to dispaly anything on my console application using cout but i just can seem to do it.
Pleas help
What compiler/IDE do you have? What have you tried? What errors are you getting?
Try this:
Code:#include <iostream>
int main()
{
std::cout << "Hello World" << std::endl;
return 0;
}
Thanks this seems to be the key, i am using the .NET frame work.
I'm really new to C++ but as far as I read and attempted to compile you need the:
So you could use just cout without the std::Code:using namespace std;
Example:Code:#include <iostream>
using namespace std;
int main()
{
cout << "Hello World\n";
return(0);
}
Quote:
Originally Posted by Devil Panther
While that's true, once you start working with multiple namespaces, you'll wish you'd used std::. Using std:: also helps you understand what is in the namespace std and what is not.
I really wouldn't know about multiple namespaces, I just started C++ last week or so... finally moved on from C :D
But can you please explain what do you mean by multiple namespaces, and why is it better to work with std:: ?
I'm fairly new to c++ but many years in programming other languages.
Let's say you write a program with a lot of cool custom functions. You could place all those functions in a namespace. then, you'd reference those functions with that namespace qualifier:
using namespace cool;
...
...
std::string blandtext;
std::string fancytext;
fancytext = cool::fancyit(blandtext);
This doesn't seem like much...now have several custom namespaces for different types of formatting, printing, file stuff, ANYTHING. When you are working through your code with all types of custom functions, it will be easier to recognize your functions from std functions and even which type of custom fuction you are using at that time. Write a large program. Walk away for 6 months. You get an email to add some functionality. Now go in to make the changes. The easier it is to read, the easier to modify the code.
If you really wanted to do that couldn't you just use std:: at the beginning withQuote:
once you start working with multiple namespaces, you'll wish you'd used std::
or is that the same idea as namespace std?Code:using std::cout;
And whoever said death to Visual Basic, what is bad about it because I asked for a Visual Basic .NET book for my birthday (being foolishly new to standalone apps just having come from the world of JavaScript about a month ago).
I see... One last thing, what are namespaces??? Since books and guides use them, but non of them really explain the subject... is it like a class?
p.s: I'm sorry if this is a really stupid question :D
Quote:
Originally Posted by beanroaster
yeah... :D well it's easier than C/C++ and limited as well, I simply don't believe it's a good language to start with (like many people do) nor I belive it's a good lanauge to develope heavy or even mideum size apps. But that's just me :D
http://www.cplusplus.com/doc/tutorial/tut5-2.html
This keeps everything out of the global namespace, in it's own seperate scope in a project. All ANSI C++ header contents are loaded into the std namespace, which is why you see things like std::cout, std::pow, std::printf, std::whatever. The using namespace std; is somewhat of a battle, but I'm sure in very large unwieldy project, multiple coders, you would probably be happy you didn't stick it in a global header, but I'm not all that sure. It sure does make your code look l33t and verbose to reference absolutely everything through the global or std namespaces though.
a namespace isn't a class it is as it says a namespace. It is kind of hard to explain.Quote:
Originally Posted by Devil Panther
This is a generic example but it gets the point across what a namespace does.Code:#include <iostream>
namespace ns
{
void addSomething()
{
std::cout<<5 + 7<<std::endl;
}
}
void addSomething()
{
std::cout<<2 + 4<<std::endl;
}
int main()
{
addSomething();
ns::addSomething();
std::cin.get();
return 0;
}
using std::cout is sort of the same idea as using namespace std;. using namespace std allows all of the objects/functions of that namespace to be used without the std:: in front, which could cause conflicts if any other part of the program had a thing named cout, etc. using std::cout poses the same problem: if theres another thing named cout in the program it could potentially cause a conflict, however since its only using std::cout, cin and the rest of the namespace std wouldnt pose any problem ever, where as with using namespace std its using the entire namespace and potentially could. Then theres std::, and that poses no problems ever unless you happened to name one of your namespaces std ;)Quote:
Originally Posted by beanroaster
Thats what I did the first few days of my C++ programming and all of those colons got really annoying once I started making bigger things.Quote:
Then theres std::, and that poses no problems ever unless you happened to name one of your namespaces std
I find they make my programs look programming-like complex so I like using it for that reason.. sort of like I know what the hell I'm doing. :DQuote:
Originally Posted by beanroaster