Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 25th, 2005, 6:55 AM   #1
emdiesse
Programmer
 
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 5 emdiesse is on a distinguished road
Road to C++

Hello there.

I have purchased the book: SAMS Teach Yourself C++ in 21 Days, after my hate towards purchasing the C++ book for dummies (HEAP OF TRIPE)

I have gotten through Lesson 1 (first day) so far and i have a few questions i would like to ask to help me understand it better.

This is the first program you write (obviously):
#include <iostream>                                  //what does this do? I'm guessing the book will explain this later one, but i want to know now! lol Unless if it is best not to yet?

int main()                                           //As previous comment
{                                                    
     std::cout << "Hello World!\n";                  //Whats the benifit of using std:: instead of declaring "using namespace std;"
     system ("PAUSE");                               //I had to add this so that I coukld see the output.
     return 0;                                       //what does this do?
}

My questions are in the comments


Also have question on this
#include <iostream>

int main()
{     
     int x = 5;
     int y = 7;
     std::cout << endl;                                               \\ dev C++ came up with an error with this and std::cout << end; so i removed them, what are they for?
     std::cout << x + y << " " << x * y;
     std::cout << end;                                                 \\ dev C++ came up with an error with this and std::cout << end; so i removed them, what are they for?
     system ("PAUSE");
     return 0;
}




Also, is my purchase of this book a good choice? (I understand it won't make me an advanced C++ programmer, but it will get me started says its aimed at levels begginer to intermediate, so i should be intermediate after, with lots of practice)
emdiesse is offline   Reply With Quote
Old Oct 25th, 2005, 7:18 AM   #2
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 314
Rep Power: 4 Klarre is on a distinguished road
Quote:
Originally Posted by emdiesse
std::cout << endl;
try
std::cout << std::endl;
instead.
std::endl = new line. Same as the escape sequence \n, if you have seen that one.
Klarre is online now   Reply With Quote
Old Oct 25th, 2005, 7:52 AM   #3
emdiesse
Programmer
 
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 5 emdiesse is on a distinguished road
Quote:
Originally Posted by Klarre
try
std::cout << std::endl;
instead.
std::endl = new line. Same as the escape sequence \n, if you have seen that one.

Thanks, i guessed it was the same as \n
emdiesse is offline   Reply With Quote
Old Oct 25th, 2005, 8:04 AM   #4
peace_of_mind
Professional Programmer
 
peace_of_mind's Avatar
 
Join Date: Sep 2004
Location: Hell if I know most of the time
Posts: 439
Rep Power: 5 peace_of_mind is on a distinguished road
Send a message via MSN to peace_of_mind Send a message via Yahoo to peace_of_mind
It appears that you really should read a bit more of the book. I have that book, just haven't read it yet, so I can't comment on how good it is. But if #include isn't explained within the first chapter or two you may want to consider another book. Your first two questioned lines should be covered quite well in the early chapters of the book.
__________________
Amateurs built the ark
Professionals built the Titanic

peace_of_mind is offline   Reply With Quote
Old Oct 25th, 2005, 8:20 AM   #5
pal
Programmer
 
pal's Avatar
 
Join Date: Mar 2005
Location: Washington
Posts: 91
Rep Power: 4 pal is on a distinguished road
Yes, read more of the book.
Do this
#include <iostream>
using namespace std;
and then get rid of all
std::
I would avoid the ones that says "teach yourself within # days".
pal is offline   Reply With Quote
Old Oct 25th, 2005, 8:39 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
std::endl = new line. Same as the escape sequence \n, if you have seen that one.
. Not true. "endl" causes the output buffer to be flushed, also.

Instructing the OP to use "using namespace std;" as a matter of course is also incorrect. The desirability of doing that is entirely circumstantial; relating to the coders use (and degree of use) of his/her own (possibly overlapping) namespaces. In simple, short programs, it's usually to be preferred, but it shouldn't be presented as a "do this instead of that."
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 25th, 2005, 9:07 AM   #7
pal
Programmer
 
pal's Avatar
 
Join Date: Mar 2005
Location: Washington
Posts: 91
Rep Power: 4 pal is on a distinguished road
My mistake if it sounded like a rant.
I meant to say it as a recommendation.
pal is offline   Reply With Quote
Old Oct 25th, 2005, 9:46 AM   #8
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Others have answered some of the questions. I'll answer some more;

#include <iostream> is a preprocessor directive, which literally pastes the contents of something (commonly called a header file, as it might actually be a text file containing declarations and definitions that are valid C++) named iostream in place of the #include directive. The thing called iostream is a standard header, described in the C++ standard, that allows use of basic stream I/O functionality. For example, std::cout is an output stream, corresponding to a device known as "standard output" that is declared in <iostream>.

int main() is an entry point to a program. When any program starts up, it is necessary to have some way of reaching code that the programmer wants executed. main() is referred to an an entry point and is (typically) the first function under programmer control to be called. There are three forms of main() that all C and C++ compilers must support: int main() -- which takes no arguments is one of them. All required forms of main() return int (which is normally interpreted by your operating environment as an error code).

The benefit of using "std::cout" instead of "using namespace std;" and simplifying it to "cout" is one of avoiding ambiguity (which typically causes a compiler to choke and refuse to compile a program). The std:: prefix means that the object named cout exists in a namespace (literally a universe of names) named std. It is possible to have an object named cout exist in another namespace, in which case using directives can result in ambiguity. In your example, it makes little difference. But here is an example where it does;
#include <iostream>
namespace X
{
    int cout;
}
using namespace std;
using namespace X;

int main()
{
    cout << "Hello\n";
}
This code will not compile, as the "cout" referred to in main() could refer to either std::cout or X::cout (the combination of using directives means both are equally viable candidates when the compiler sees the name cout). This sort of example is quite common when programmers reuse libraries, and is the main reason why using directives are discouraged in header files or library code (code that you write so other people can use it from their programs).
grumpy is offline   Reply With Quote
Old Oct 26th, 2005, 5:05 AM   #9
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
grumpy has explained your issues, and as to whether or not it's a good book, i think it is. it goes from hello world to fairly complex things such as multiple inheritance and polymorphism with good explanations. 21 days is a lie though, the book should probably take you 2-3 months to finish (and understand everything).
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Oct 26th, 2005, 5:20 AM   #10
ivan
Professional Programmer
 
ivan's Avatar
 
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 4 ivan is on a distinguished road
The time to understand the book depends on your programming experience. If you have not programmed before it will take you longer to understand it good.
ivan is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:20 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC