I just started C++, and I really like it.
Now I want to try out some code, but Microsoft Visual C++ 2005 keeps giving errors, while I exactly copy the code of a C++ book I own.
This are the steps I take:
Open Visual 2005> file > new > project > win32 > Win32 Console Application > Fill in name and click O.K. > then change the code there is by this:
When I run that (well, compile and stuff) I get an error:
1 2 3 4 5 6
Compiling...
HELLO.cpp.cpp
c:\documents and settings\gebruiker\mijn documenten\visual studio 2005\projects\hello.cpp\hello.cpp\hello.cpp.cpp(11) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
Build log was saved at "file://c:\Documents and Settings\Gebruiker\Mijn documenten\Visual Studio 2005\Projects\HELLO.cpp\HELLO.cpp\Debug\BuildLog.htm"
HELLO.cpp - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Also, when i add #include "stdafx.h" to my code it gives an error to:
1 2 3 4 5 6
Compiling...
HELLO.cpp.cpp
c:\documents and settings\gebruiker\mijn documenten\visual studio 2005\projects\hello.cpp\hello.cpp\hello.cpp.cpp(4) : fatal error C1083: Cannot open include file: 'iostream.h': No such file or directory
Build log was saved at "file://c:\Documents and Settings\Gebruiker\Mijn documenten\Visual Studio 2005\Projects\HELLO.cpp\HELLO.cpp\Debug\BuildLog.htm"
HELLO.cpp - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I wanna know if it is just me and my book, Visual C++ 2005, and what is the solution then?
Well, I'm using Visual C++ 2008 Express edition (free on Microsoft's website if you're interested) and it doesn't recognized #include <iostream.h>, but it does recognize #include <iostream>. (Notice the lack of the .h) Dev-C++ is this way too. It says the <iostream.h> is an antiquated header. I think the book you're using might be a little out of date. Try this:
1 2 3 4 5 6 7 8 9
#include <iostream>
usingnamespace std; //Tells the compiler that your using the standard namespace
int main()
{
cout<<"Hello World!\n";
system ("pause"); // pauses the screen until you press a key
return 0;
}
cout is contained within the standard namespace, and you need to tell the compiler that you're using it. As an alternative to writing 'using namespace std;' at the top of your program you can write std::cout every time you want to use the cout function, but that gets rather tedious. The system ("pause") will hold the result on the screen until you press a key. Otherwise your program will execute and leave the screen to fast for you to see the ouput.
Well, it does work when I do it like this:
Thanks mate!
1 2 3 4 5 6 7 8 9 10
#include "stdafx.h"
#include <iostream>
usingnamespace std; //Tells the compiler that your using the standard namespace
int main()
{
cout<<"Hello World!\n";
system ("pause"); // pauses the screen until you press a key
return 0;
}