C++ Compilers - Very Basic Question

Hi, I'm using VS.NET 2010 C++.

I picked up an old C++ book called "SAMS Teach Yourself C++ in 10 minutes" by Jesse Liberty and I'm trying to learn some simple examples.

The most elementary of examples is the infamous "Hello World" App. However, I'm having a bit of a problem even with that...

My code is shown below. I think the VS.NET 2010 C++ compiler is having an issue with my "cout" statements.

Why...? Am I using the wrong compiler? Is it my namespace...?

I also have XCode, so should I be using that instead of VS.NET2010?  I thought that C++ was C++ anywhere...

Thanks,
Fulano
#include <iostream>

using namespace std;

int main(int argc char* argv[])
{
cout << "Hello World";
return 0;
}

Open in new window

Mr_FulanoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

n2fcCommented:
Try this:

CString s("Hello World");
cout << (LPCTSTR)s << '\n';

0
crystyceCommented:
You should post the error so we know what exactly is wrong.

First of all, if you use visual studio and created a project, you probably need to include the precompiled header.  
#include "stdafx.h"

Open in new window

. And you seem to be missing a comma after int argc
int main(int argc, char* argv[])

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mr_FulanoAuthor Commented:
Hi All, I think I figured it out...ctystyce was on track with the "#include "stdafx.h" suggestion. (BTW, the missing comma was a typo on my part, but was in the actual code, which I retyped into the question.)

So, I opened a new console project in VS2010 C++ and edited out some of the generic template code it had and added my code from the book. The root problem was that I had to include BOTH #include "stdafx.h" AND #include <iostream> for it to run. If you take either of them out, you get an error.

I added my code below in case you want to see it.

Any final thoughts before I close out this question?

Thanks for the help,
Fulano



// CPPConsoleApp.cpp : main project file.

#include "stdafx.h"
#include <iostream>

//using namespace System;   << This was in the generic template code.
using namespace std;

//int main(array<System::String ^> ^args) << This was in the generic template code.
int main(int argc, char* argv[])
{
    //Console::WriteLine(L"Hello World");  << This was in the generic template code.
	cout << "Hello again...";

    return 0;
}

Open in new window

0
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Mr_FulanoAuthor Commented:
Also...one thing I did not mention above.

You also need to include line number 7, which is "using namespace std;" otherwise you get the following error:

It will run with both "using namespace System;" and "using namespace std;", but if you remove "namespace std;" you'll get the error.


Error error C2065: 'cout' : undeclared identifier c:\Users\Me\documents\visual studio 2010\Projects\CPPConsoleApp\CPPConsoleApp\CPPConsoleApp.cpp 13 1 CPPConsoleApp
0
AndyAinscowFreelance programmer / ConsultantCommented:
//int main(array<System::String ^> ^args) << This was in the generic template code.

That looks like C++.net code not C++ code (similar but with big differences), make certain you are not trying to mix two different languages.
0
Mr_FulanoAuthor Commented:
Hi Andy, yes you are correct. I'm using VS2010.NET as a compiler. Is that not advisable? I guess that goes back to my original question...what compiler to use.

When you say "make certain you are not trying to mix two different languages. " what do you mean? Is C++.NET not real C++? What are the differences?

Thanks,
Fulano
0
Mr_FulanoAuthor Commented:
Actually, I'm going to open my question to Andy as a new question and allow for point to be awarded to that question as well.

Please look for my new question titled: The difference between C++ and C++.NET.

Thank you,
Fulano
0
Mr_FulanoAuthor Commented:
Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.