basic C++ question!

Custom / CUSTOM BUILD
January 7, 2009 at 21:09:38
Specs: Windows xp, athlon 3g
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;
}


See More: basic C++ question!

Report •


#1
January 7, 2009 at 23:15:16
have you tried the std namespace?

#include <iostream>
using namespace std;

int main(void)
{
cout << "hello world" << endl;

return (EXIT_SUCCESS);
}


Report •

#2
January 8, 2009 at 06:24:15
What error message do you get?

Report •

#3
January 8, 2009 at 09:30:23

/* 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;

}


Report •

Related Solutions

#4
January 8, 2009 at 10:22:38
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?


Report •

#5
January 8, 2009 at 11:35:44
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;


Report •

#6
January 8, 2009 at 13:39:05
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.




Report •

#7
January 8, 2009 at 14:07:13
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;


Report •

#8
January 8, 2009 at 16:20:18
you're right klint. the last line of comand should be std::cout<< std::end;

i didn't double check it!

thank again.


Report •


Ask Question