Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
bare with me ask i'm new to programming. ok so i'm tying to display the hello world programming example and have a few question the i can't seem to digest.
from this below code it will display the hello, world. Is there anyway of writing this code. keeping everything as is i try changing the cout command like this:
std::cout<<"hello, world!";
std::endl;
this below will display the hello world. replacing the 2 command above to below command will have an error message. can somebody help please!#include <iostream>
int main()
{
std::cout<<"hello, world!" <<std::endl;
}

have you tried the std namespace?
#include <iostream>
using namespace std;int main(void)
{
cout << "hello world" << endl;return (EXIT_SUCCESS);
}

/* yes I have use the namespace. I'm trying to figure out
another way of writing this without using the namespace.
below is the code writen with namespace */
// This program will print the "hello world"#include <iostream>
using namespace std;int main()
{
cout << "hello, world!";
cout <<endl;
}/* this program will print the "hello, world!" too but without
using the namespace and the code are: */// this want will not work! The error message generate are
// statement cannot resolve address of overloaded function
#include <iostream>int main()
{
std::cout<<"hello, world!";
std::endl;
}// below is the same as above wihtout using namespace but work
#include <iostream>
int main()
{
std::cout<<"hello, world!" <<std::endl;
}

Ok, let me give you a clue, and you'll then spot the error yourself.
You've already successfully tried this:
using namespace std;
...followed by:
cout << "hello, world!";
cout <<endl;The alternative way is to qualify each occurrence of a name with its namespace:
std::cout << "hello,world!"; instead of
cout << "hello,world!";
So what's the next statement?

What compiler are you using? I *think* newer compilers require the using namespace std or will choke.
I don't have access to a compiler right now, but how about
using std::cout;
using std::endl;cout << "hello world!" << endl;
perhaps
std::cout << "hello world!\n";
or maybe
std::cout << "hello world!" << endl;

thank for the help shutat and klint! if using namespace, the cout would look like this:
cout << "hello, world!"; << endl; OR
cout << "hello, world!";
cout << endl;Without using the namespace, the cout would look like this:
std::cout << "hello, wolrd"; <<std::endl; OR
std::cout << "hello, world";
std::cout << endl;you are right shutah. by declaring std::cout
and std::endl i got it running.
thank for everything.

shutat, newer compilers most certainly do NOT require "using" anything!
The using keyword is only for convenience. All compilers allow you to specify the namespace explicitly, without using using.
donever, you NEARLY got it right. It should be:
std::cout << std::endl;

you're right klint. the last line of comand should be std::cout<< std::end;
i didn't double check it!
thank again.

![]() |
Batch Script Reformat EDI...
|
how can i get just the fi...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |