Really new & Really clueless (lots of different questions)

This is a discussion on Really new & Really clueless (lots of different questions) within the C++ Programming forums, part of the General Programming Boards category; Hey there! I'd like to start out by saying that I'm learning C++ out of curiosity and boredom. I'm not ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    3

    Really new & Really clueless (lots of different questions)

    Hey there! I'd like to start out by saying that I'm learning C++ out of curiosity and boredom. I'm not taking any formal classes, but I do have a book (which I suspect is outdated) and I'm leaning heavily on this website's tutorial resources.

    Most of my questions (for there will probably be many) will deal with why something works, as opposed to just making it work. I figure that if I know the why of something then being able to work out the how on my own will be easier (problem solving ftw).

    The book I bought was gotten from a half price bookstore for pennies on the dollar, it's called 'Sam's Teach Yourself C++ in 21 days". The latest publication copyright on the inside is for 2000 and he doesn't mention any operating system later than Windows 2000. (I'm running on XP Pro)

    I've completed two lessons in his book and one or two lessons from this site and run into some discrepencies. I'll post my first problem now. (I'm using the free Code Blocks compiler btw)

    Code:
    #include <iostream.h>
    
    int main()
    {
      cout<<"Hello World!\n";
      return 0;
    }
    This is the code supplied in the book. It works, but the only problem is that when executed by double-clicking the exe file the program runs and immediately terminates.

    This site's tutorial for the same program advises this code:

    Code:
    #include <iostream.h>
    
    int main()
    {
      cout<<"Hello World!\n";
      cin.get();
    }
    The difference is return 0; and cin.get(); When I run this one the program properly waits for me to press enter once running before it completely terminates.

    What's the difference between these two lines of syntax? Is 'return 0;' working as it should? So far, I've been using the tutorials in the book (the book supplies a lot more whys and hows than this website) and just substituting 'cin.get();' for 'return 0;' whenever I run across it.

    Last night I ran into another problem with this code substitution that doesn't make any sense to me. I was calling functions, but right now I'm at work and don't have my source code. If nobody minds I'll go ahead and post my problem code up tonight or tomorrow.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,976
    In this very special case of the global main function, the return 0; is optional, so the difference is really between having the cin.get() and not having it.

    The cin.get() is an (incomplete) attempt to stop the Windows console from disappearing everytime you run your program. I prefer just running the program at the command prompt. Some IDE pause your program for you, but you can also set a breakpoint at the end.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    return 0 is necessary to satisfy the requirement that the main function return an integer. It can follow the cin.get(); call.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    When you close Word, do you expect the window to stay open? Why wouldn't any other program be the same way? return 0 just means the function returns to where it came from; since main is the main program function, that means the program itself returns to where it came from.

    Note that if you run the program inside Code::Blocks it stays open. But let's be honest, here: the point of console programs is to be run from the console (command prompt, whatever you want to call it). Open one up and run it there.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    3
    Allright, that helps.

    So basically I should get into the habit of running the program directly from Code::Blocks (which does put in a nice little pause) and not worry about my program terminating once it runs its course?

    Thank you all so much for your quick responses I'll get into my second and third questions tonight.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,180
    I do note that the book is outdated, because the iostream.h header is outdated.
    Today, we use iostream (note the missing .h) and you have to prefix cout and cin with std::.

    And yes to your "get into habit" question.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    3
    I've been using cout with no prefix so far fine, are you saying I should be using std::cout << "blah blah blah\n"; kind of thing?

    Also, I'm having trouble with end1; It's erroring at me, I'll post the specific error and code I'm using in a few hours.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,180
    Quote Originally Posted by Tigerfeet View Post
    I've been using cout with no prefix so far fine, are you saying I should be using std::cout << "blah blah blah\n"; kind of thing?
    It is because you are using an old header. Use the new header, and you will have to append std::.
    And if you do not have the newer header, it means you are using an old, deprecated compiler, which again means you should upgrade.

    Also, I'm having trouble with end1; It's erroring at me, I'll post the specific error and code I'm using in a few hours.
    There is no such thing as end1. There is such a thing as endl (ending with a lower-case L).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two questions. (Sorry for lots of them =( )
    By ozumsafa in forum C Programming
    Replies: 10
    Last Post: 09-21-2007, 03:57 AM
  2. Lots of questions
    By belhifet in forum C# Programming
    Replies: 5
    Last Post: 12-20-2006, 06:39 PM
  3. Lots Of Bitmap Texture Questions:
    By Krak in forum Game Programming
    Replies: 7
    Last Post: 07-10-2003, 08:16 PM
  4. Lots of RPG questions...
    By Kyoto Oshiro in forum Game Programming
    Replies: 22
    Last Post: 07-21-2002, 10:47 PM
  5. lots of questions
    By phantom in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2002, 10:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21