Computing.Net > Forums > Programming > basic C++ question!

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

basic C++ question!

Reply to Message Icon

Name: donever
Date: January 7, 2009 at 21:09:38 Pacific
OS: Windows xp
CPU/Ram: athlon 3g
Product: Custom / CUSTOM BUILD
Subcategory: C/C++
Comment:

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;
}



Sponsored Link
Ads by Google

Response Number 1
Name: shutat
Date: January 7, 2009 at 23:15:16 Pacific
Reply:

have you tried the std namespace?

#include <iostream>
using namespace std;

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

return (EXIT_SUCCESS);
}


0

Response Number 2
Name: klint
Date: January 8, 2009 at 06:24:15 Pacific
Reply:

What error message do you get?


0

Response Number 3
Name: donever
Date: January 8, 2009 at 09:30:23 Pacific
Reply:


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

}


0

Response Number 4
Name: klint
Date: January 8, 2009 at 10:22:38 Pacific
Reply:

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?


0

Response Number 5
Name: shutat
Date: January 8, 2009 at 11:35:44 Pacific
Reply:

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;


0

Related Posts

See More



Response Number 6
Name: donever
Date: January 8, 2009 at 13:39:05 Pacific
Reply:

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.




0

Response Number 7
Name: klint
Date: January 8, 2009 at 14:07:13 Pacific
Reply:

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;


0

Response Number 8
Name: donever
Date: January 8, 2009 at 16:20:18 Pacific
Reply:

you're right klint. the last line of comand should be std::cout<< std::end;

i didn't double check it!

thank again.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Batch Script Reformat EDI... how can i get just the fi...



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: basic C++ question!

basic c++ questions cocnerning win3 www.computing.net/answers/programming/basic-c-questions-cocnerning-win3/14967.html

Classes : Basic - C Language www.computing.net/answers/programming/classes-basic-c-language/11058.html

Basic C++ question www.computing.net/answers/programming/basic-c-question/11522.html