Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

visual studio 2012 c++ hello world - iostream not working

I have an issue with Visual Studio's 2012. I am also using "Sams Teach Yourself C++ in One Hour a day, 7th edition".

After using google to find the "best" compilers for C++, Visual Studios seemed to be the tool of choice.

So I downloaded and installed Visual Studios 2012. The very first lesson in the book is (and tells me to run it as a console app by going to File > New > Project >Visual C++ > Win32 > Console Application )

 #include <iostream>

int main()
{
 std::cout << “Hello World!” << std::endl;
 return 0;
}

which doesnt work, at all. it outputs an error message similiar to the following:

1>c:\users\nik\documents\visual studio 2012\projects\consoleapplication4\consoleapplication4\consoleapplication4.cpp(8): error C2065: '“Hello' : undeclared identifier 1> Generating Code... ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped =========="

(there is more lines similiar to the first, but its rather long)

However, after googling and watching a video on youtube the following code works (using File > New > Project >Visual C++ > General > Empty Project )

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

using namespace std;

int main() {
cout << "Hello Nik" << endl;
_getch();

return 0;
}

Does Visual Studio's 2012 have a C++ compiler? or does it just have a visual c++ compiler (if thats even the issue, only reason I think it could be is I can see templates for Visual C++ but none for c++ by itself...) or do I need to download Visual Studio Express to download native c++ ??

Any help would be greatly appreciated as I am feeling some-what out of my depth here...

Thanks.

like image 786
Nik Avatar asked Sep 27 '12 05:09

Nik


People also ask

How do I run a code in Visual Studio C++?

Build and run your code in Visual Studio To build your project, choose Build Solution from the Build menu. The Output window shows the results of the build process. To run the code, on the menu bar, choose Debug, Start without debugging. A console window opens and then runs your app.

Can I use C++ in Visual Studio?

You can use Visual Studio to create Standard C++ programs. By following the steps in this walkthrough, you can create a project, add a new file to the project, modify the file to add C++ code, and then compile and run the program by using Visual Studio.


4 Answers

Besides aphostrophes you may need to disable precompiler headers in project properties.

They are turned on by default in VS2012. If you are not familiar with precompiled headers turn them off.

  1. Right click on project (not solution)
  2. Click properties.
  3. Expand "Configuration properties"
  4. Expand "C/C++"
  5. Choose "Precompiled headers"
  6. Set "Precompiled header" to "Not Using Precompiled Headers"

More information about precompiled headers and stdafx.h file at Wikipedia

like image 155
Kamil Avatar answered Oct 03 '22 09:10

Kamil


The apostrophes you used are wrong:

“Hello World!” 

should be

"Hello World!"

Notice even how SO recognizes the difference. You should at least type the code you see in the book instead of copying and pasting it. ;-)

like image 26
Alex R. Avatar answered Oct 03 '22 09:10

Alex R.


The Win32 console application is actually quite different from the empty project. Win32 utilize a message (input) queue which you poll in a loop and your program respectively utilizes the Win32 API and performs certain operations.

The empty project is a bit less dependent on Win32 or anything that Windows provides in terms of API unless you make it dependent on it. This would be the simples hello world app in you empty project:

#include <iostream>

using namespace std;

int main() 
{
    cout << "Hello World" << endl;

    return 0;
}
like image 29
Konstantin Dinev Avatar answered Oct 02 '22 09:10

Konstantin Dinev


Just try this::

"Hello World!" instead of “Hello World!”.

like image 30
Abhineet Avatar answered Oct 03 '22 09:10

Abhineet