First off I want to say that I started to learn C++ about a year ago but stopped because of school. I decided to learn it again and I noticed that my code is not working. I am directly copying it from a book from "Learn C++ in 21 days, fifth edition, burrowed it) Anyway it goes like this:
#include <iostream>
int main()
{
std::cout << "Hello World!\n";
return 0;
}
*I know that I can put the using namespace std at the start.
So my question is, why is it not working? I am pretty sure that I am not doing anything different from about a year ago, so is it Visual Studio 2010 that is the problem? Was there some new change that I am unaware of? Thank you for any help.
What exactly does "not working" mean?? Does it not compile, or does it not display the text?
What is probably happening is that it appears, shows "Hello World!" and then because there are no further instructions, it returns 0 thus ending the application and closing the window.
You need to make it stay open. The easiest but not the most convenient way is to get user input before exiting.
It does not compile, here is the error message at the bottom:
1>------ Build started: Project: ase, Configuration: Debug Win32 ------
1> ase.cpp
1>c:\users\sebastian\documents\visual studio 2010\projects\ase\ase\ase.cpp(1): warning C4627: '#include <iostream>': skipped when looking for precompiled header use
1> Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\users\sebastian\documents\visual studio 2010\projects\ase\ase\ase.cpp(9): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source?
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
When I add #include <StdAfx.h>:
#include <iostream>
#include <StdAfx.h>
int main()
{
std::cout << "Hello World!\n";
return 0;
}
, this is the new error I get:
1>------ Build started: Project: ase, Configuration: Debug Win32 ------
1> ase.cpp
1>c:\users\sebastian\documents\visual studio 2010\projects\ase\ase\ase.cpp(1): warning C4627: '#include <iostream>': skipped when looking for precompiled header use
1> Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\users\sebastian\documents\visual studio 2010\projects\ase\ase\ase.cpp(9): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source?
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I tried that but it still failed, here is the message I got:
1>------ Build started: Project: HelloWorld, Configuration: Debug Win32 ------
1>LINK : error LNK2001: unresolved external symbol _mainCRTStartup
1>c:\users\sebastian\documents\visual studio 2010\Projects\HelloWorld\Debug\HelloWorld.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
The alternative is to switch off precompiled headers for your project. The project was created incorrectly. To do so:
1. select project propery pages.
2. navigate to Configuration Properties->C/C++->Precompiled Headers
Change Create/Use Precompiled Header setting to: Not Using Precompiled Headers.
Remember to do this for Debug and Release.
Remove that StdAfx nonsense from your project and remove #include "StdAfx.h" from your code.