So what am I doing wrong?

Started by
11 comments, last by Spoonbender 17 years, 11 months ago
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.
Advertisement
what errors?
What errors are you getting?

theTroll
Quote:Original post by ItsNotATumaaa
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.


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.
#include <iostream>using namespace std;int main(void){ cout << "Hello World" << endl; return 0;}


edit: Damn too slow

Steven Yau
[Blog] [Portfolio]

#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
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
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.
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.)
What are you using as ani IDE/Compiler?

Steven Yau
[Blog] [Portfolio]

This topic is closed to new replies.

Advertisement