Simple C++ question

Started by
21 comments, last by sneaky_squirrel 15 years, 8 months ago
Quote:Original post by MonkeyIsland
Some compilers also require you to put the full name of the header file. Instead of this: #include <iostream>
you put this: #include <iostream.h>

Do not do this. iostream.h is a C header that your compiler might have (many C++ compilers are also C compilers), not a standard C++ header. The standard C++ header is iostream (no .h!)

int main() is also perfectly legal, and compiles in every compiler I can think of. Neither of these are likely your problem.

@OP: It's really not possible to come to any kind of conclusion here without posting the error information and compiler you're using.
Ra
Advertisement
Wow amazing all the support here, anyways here is the useless error code:

c:\documents and settings\compaq_administrator\my documents\visual studio 2008\projects\my first project\my first project\first file.cpp(3) : warning C4627: '#include <iostream>': skipped when looking for precompiled header use        Add directive to 'stdafx.h' or rebuild precompiled headerc:\documents and settings\compaq_administrator\my documents\visual studio 2008\projects\my first project\my first project\first file.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\Compaq_Administrator\My Documents\Visual Studio 2008\Projects\My First Project\My First Project\Debug\BuildLog.htm"My First Project - 1 error(s), 1 warning(s)========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I have no idea what it means, I tried adding this "#include "stdafx.h" and "stdafx.h", but nothing works, I am sure by reading this (Only thing it is telling me) you will know my situation, oh and I do use the Visual C++ 2008 express edition.
Quote:
I have no idea what it means, I tried adding this "#include "stdafx.h" and "stdafx.h", but nothing works, I am sure by reading this (Only thing it is telling me) you will know my situation, oh and I do use the Visual C++ 2008 express edition.


When you make a new project in Visual Studio make sure you make an EMPTY win32 console application and uncheck the "precompiled header" check box.

File->New Project
Choose win32 console application
Hit Ok
Hit Next

Application Type:
Choose "Console Application"

Additional Options:
Choose "Empty Project"
UnCheck "Precompiled Header"
Didn't work, I did what you told me to and used this:

// my first program in C++

#include <iostream>
using namespace std;

int main ()
{
cout << "Hello World!";
return 0;
}

And he is the error report, this programming really is difficult, tutorials give you instructions that don't work...:

------ Build started: Project: Hi, Configuration: Debug Win32 ------
Compiling...
Hi.cpp
Compiling manifest to resources...
Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0
Copyright (C) Microsoft Corporation. All rights reserved.
Linking...
Embedding manifest...
Project : error PRJ0003 : Error spawning 'cmd.exe'.
Build log was saved at "file://c:\Documents and Settings\Compaq_Administrator\My Documents\Visual Studio 2008\Projects\Hi\Hi\Debug\BuildLog.htm"
Hi - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Quote:Some compilers also require you to put the full name of the header file. Instead of this: #include <iostream>
you put this: #include <iostream.h>
iostream *is* the full file name. (At least in MSVC++)
Quote:
Project : error PRJ0003 : Error spawning 'cmd.exe'.

Perhaps this might help?

According to the link, it may not be finding cmd.exe, which may mean that your MSVC++ directories may be incorrect.
Quote:Original post by Crypter
Quote:Some compilers also require you to put the full name of the header file. Instead of this: #include <iostream>
you put this: #include <iostream.h>
iostream *is* the full file name. (At least in MSVC++)


I believe the non-extension files are standard C++ while its .h counterpart is for standard C.
I had an issue similar to this one the first time I installed and used VS2005, and wrote my "Hello World!"-program for the first time.

No one could figure out the random and faulty(!) errors I got, and in the end I just deleted everything and re-installed. From then on it worked perfectly. We assumed the installation was corrupted somehow, perhaps that has happened to you aswell?


Uninstall and delete everything, then re-download and re-install everything. Try again, and if it produces the same errors, at least you know it's not what happened to me.



Best of luck sorting this out - it's very frustrating to get stuck so early in the learning-process, as what you need now is everything working perfectly so you can focus on understanding the code and get into C++, not fighting random error-messages.
Student at NITH, Norway2nd year of Gameprogramming BachelordegreeC++ enthusiast
All you have to do is put all the #includes you need (#include <iostream>) in stdafx.h, and #include only stdafx.h in <project name>.cpp...
Quote:Original post by _fastcall
Quote:Original post by Crypter
Quote:Some compilers also require you to put the full name of the header file. Instead of this: #include <iostream>
you put this: #include <iostream.h>
iostream *is* the full file name. (At least in MSVC++)


I believe the non-extension files are standard C++ while its .h counterpart is for standard C.


iostream.h (and its like) is a non-standard (IIRC) backward compatibility header, provided to allow old code (C++ was standardized in 1998, but was in common use for some time beforehand) to continue to compile on newer, more standard-compliant, compilers. It's not a C header at all, and consequently you'll get reams of errors if you try to compile it as C.
[TheUnbeliever]
Thats quite simplea actually.

You are probably using visual c++ express


All you need to do is include stdafx.h header file before iostream


Here let me show:

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

int main() { /* Your code here */ }



Make sure stdafx.h comes first, aside from this check if you have this file. If you dont then it will be a problem.

This topic is closed to new replies.

Advertisement