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?
 
crystyceConnect With a Mentor Commented:
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
 
n2fcCommented:
Try this:

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

0
 
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.