newb questions

Hi everyone

I am new to the forum and completely new to programming.

i started to learn C++ today with no previous experience in coding at all (apart from some very basic HTML).
i am using a book that at first seemed great, but as im getting into it it seems that it might be quite inaccurate.

for example, i was building the first program, which is the hello world program, and in the book it tells me to put this code

1
2
3
4
5
6
7
#include <iostream.h>

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


which did not work at all. after some googling and some helpful threads on this forum i managed to piece the problems together and ended up with this code


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;

int main()
{
cout << "Hello World";

 cout << "Press ENTER to continue...";
cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

	return 0;
}


there are huge differences in these two codes, which makes me doubt the author of the book.

is the first code ok? does it depend on the compiler?

i used microsoft visual C++ express to build and compile it.
is that compiler any good? are there better free compilers around?


thank you all for the help, and sorry for the complete newb questions
You might be using an older book if it still has <iostream.h> instead of <iostream>. Which book are you using and when was it published?

This modification of your original code should work. I'm just using a prefix of std:: on the cout instead of bringing the whole namespace std; line.

You can click on the icon at the top right of the code box to run the code from the forum.

1
2
3
4
5
6
7
#include <iostream>

int main()
{
    std::cout << "Hello World!\n";
    return 0;
}



The tutorial on this site has info on current compilers, just scroll down on this page. http://www.cplusplus.com/doc/tutorial/introduction/
Last edited on
This is why I typically don't encourage the use of books to learn C++. I would suggest using the references of this forum, and the rest of the web (as I did). It's tough, but it's possible.

Here are a couple of resources to get you started:

http://www.cplusplus.com/reference

www.cppreference.com

the latter is a community attempt at formal documentation of the language. It's not great for learning from as a beginner, but when you come to understand the more advanced concepts, you will keep a copy of it on your computer like I do: it's incredibly handy to have a copy of the reference around in case you want to know somthing specific about somthing in the Standard.

:) hope this helps!
thank you for the quick replies!

@wildblue

the book is "Teach Yourself C++ in 21 Days":

http://www.amazon.co.uk/Sams-Teach-Yourself-21-Days/dp/067232072X

it is from 2001 so that must be the problem.

i tested your version in the browser and it worked perfectly. one problem i encountered with the other code was that the console closed instantly, which i solved using the following code, which i got from the stickied thread here:

1
2
cout << "Press ENTER to continue...";
cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );


would the console still close with your version? or is that because of my compiler?

thank you for the link, i will try one of those other compilers if i cant get on with this one.



@
iwishiknew

thank you for the great links, i will read through them and learn what i can from them.
one thing that caught my eye about the book i chose was that it seemed to start from the absolute ground and work from there, which is something i found hard to find.

hopefully the tutorials here will start at a level i can keep up with.


thank you both very much for your help.
Topic archived. No new replies allowed.