So what am I doing wrong?

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

Recommended Posts

So, I'm a newbie and I picked myself up a book on C++, and downloaded Dev-C++. I'm currently working on the "Hello World" basic program in the first chapter of the book. The source code is: #include <iostream.h> int main() { cout << "Hello World!\n"; return 0; } I can't seem to get it to run without errors. Am I compiling wrong, or do I just suck at life? Any help will be appreciated, thanks.

what errors?

Share on other sites
What errors are you getting?

theTroll

Share on other sites
Quote:
 Original post by ItsNotATumaaaSo, I'm a newbie and I picked myself up a book on C++, and downloaded Dev-C++. I'm currently working on the "Hello World" basic program in the first chapter of the book. The source code is:#include int main(){ cout << "Hello World!\n"; return 0;}I can't seem to get it to run without errors. Am I compiling wrong, or do I just suck at life? Any help will be appreciated, thanks.

Umm... May I ask which book? I don't want to sound harsh but any book that teaches the first program to you wrong should be thrown out. Literally.

I suggest you try Beginning C++ Game Programming.

Now, to correct it:
#include <iostream.h> // No '.h', any good book should teach that the [current]// standard library does not have an extension on the files.int main() { // Ok.    std::cout << "Hello!\n";    // Cout resides in the std namespace.  You'll learn that later on, but remember this:    // either have 'std::' in front of everything from the std, or on the top of the    // file put "using namespace std;'.    return 0; // Ok.} // Ok.

Share on other sites
#include <iostream>using namespace std;int main(void){ cout << "Hello World" << endl; return 0;}

edit: Damn too slow

Share on other sites
#ifndef _BACKWARD_BACKWARD_WARNING_H
#define _BACKWARD_BACKWARD_WARNING_H 1

#ifdef __DEPRECATED
#warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
#endif

#endif

Share on other sites
Unfortunately it appears your book is obsolete and will therefore be more of a hinderance than a help. I strongly suggest you throw it out and get a new one. Pre-standard C++ (pre 1998) usually had a header named <iostream.h>. This was eliminated when C++ was standardised and replaced with the header <iostream> which is similar to the old <iostream.h> but (among other changes) uses the std namespace. Your corrected example in standard C++ would therefore be:
#include <iostream>int main(){	std::cout << "Hello World!\n";	return 0; // this line is not actually necessary	// the main function (and only the main function) implicitly	// returns zero if no return statement is included.}

Don't be fooled into thinking that all the changes made in the standardisation of C++ and beyond are as trivial. An old, obsolete book will teach you bad practices that will be hard to unlearn.

Σnigma

Share on other sites
It's called Sams Teach Yourself C++ in 21 days.

Note: I just looked at the publishing and it's 1999. Damn, I thought it was brand new.

Share on other sites
Thanks for your help, I should have checked the date it was published before I bought.

So, I wrote the updated code and I received no errors, and saved the file to .cpp. Now how do I link and run it?

(Sorry, I'm a big newbie.)

Share on other sites
What are you using as ani IDE/Compiler?

Dev C++

Share on other sites
Quote:
 Original post by ItsNotATumaaaThanks for your help, I should have checked the date it was published before I bought.So, I wrote the updated code and I received no errors, and saved the file to .cpp. Now how do I link and run it?(Sorry, I'm a big newbie.)

After you save your project (cpp file), you go to Execute->Compile. After you compile it, go to Execute->Run. The Comand prompt should pop up.

NOTE: Your command promt window might pop up and go away really quickly, so ill show you a simple solution.

int main(){  std::cout << "HELLO WORLD!"; /* the following code, make's it so the user decides when to end the program.Type it in with    out this following line, and see how the program runs, then type this    folowing code in, and see how this one works. */   system("PAUSE"); //This line waits till the user wants to end the program  return 0;}

Hope that Helps. :)

[Edited by - NUCLEAR RABBIT on April 30, 2006 10:35:36 AM]

Share on other sites
Before you throw the book away, I should probably chime in and say that yes, you're going to run into this kind of problems throughout, but it might not be such a big deal. First, you can usually google for examples on how it should have been written, and second, the only really important change (from a beginner point of view, anyway) is the namespace std part, and that the headers no longer use .h

Everything else should be pretty straightforward.