• Advertisement
Sign in to follow this  

Simple C++ question

This topic is 3457 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What do I have to do to find errors? It says It couldn't compile a program because it had an error, but it wouldn;t indicate which line carried the error, and I find no go to line option to find it, I see nothing wrong with the program. I might as well copy paste it, it is the simplest program ever:
#include <iostream>
using namespace std;

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

Share this post


Link to post
Share on other sites
Advertisement
What are you using to compile that? The error probably specifying something else and not the program.

Share this post


Link to post
Share on other sites
Quote:
Original post by sneaky_squirrel
... it had an error, but it wouldn;t indicate which line carried the error, and I find no go to line option to find it


Sounds like a linker problem. The most common mistake would be to create a win32 project, instead of a console program (was my first mistake in C++ [smile]). Go to File->New->Project..., then locate "Win32 console application", fill in your project name and etc, click finish, then click "empty project" under "application settings" in the next dialog.

For further explanation, a win32 project looks for a WinMain entry, instead of a main.

Share this post


Link to post
Share on other sites
It compiles for me, but you generally don't put a space between a function name and the left parenthesis. And if you are using Microsoft Visual Studio, did you create an empty project?

Share this post


Link to post
Share on other sites
Quote:
Original post by sneaky_squirrel
What do I have to do to find errors?

Posting the error here might be a good start

Share this post


Link to post
Share on other sites
Some compilers require that you add parameters to the main function so replace this line

int main()

with

int main (int argc, char *argv[])

Share this post


Link to post
Share on other sites
Quote:
Original post by Googol PL3X
Some compilers require that you add parameters to the main function so replace this line

int main()

with

int main (int argc, char *argv[])


Which ones? Every compiler I've ever used seems to turn off parameter checking for main(). IIRC, the standard is quite lax about what is required here.

Quote:
Original post by Googol PL3X
but it wouldn;t indicate which line carried the error


If that is the case, _fastcall has the correct diagnosis. I'd agree that the most likely cause was creating a Win32 rather than console project.

Listen to Spoonbender. Post the error. It'll be solved in seconds.

Share this post


Link to post
Share on other sites
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>

I recommend trying yo compile this way and then if it does not work try ading the args to the main function. Also if your IDE requires you to have any aditional code or format you should use it.
You can look for a simple helloworld in internet that has proven to work in your compiler via entering "Hello World in <your IDE name>"

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 header
c:\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.

Share this post


Link to post
Share on other sites
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"

Share this post


Link to post
Share on other sites
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 ==========

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ra
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!)


For the record iostream.h is a C++ header. It is the version of the iostream library that was written before standardization and is simply distributed for legacy support.

Share this post


Link to post
Share on other sites
Quote:
Original post by CodeLuggage
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.


Yep you know exactly how I feel, so I really should uninstall and reinstall?

I guess I should seeing as there is no solution left.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement