Thread: Newbie Help

  1. #1
    C++ and Flash Programmer
    Join Date
    Jul 2004
    Posts
    7

    Newbie Help

    I need some help with the first part of some code I got with my book.
    it reads:

    void main () {
    // write some stuff to screen
    cout << using C++";
    }

    and it does not work
    this is how the code is written in my book
    I am using bloodshed dev the latest version on beta. Is there a way to get this to work as it currently does not.

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    You might wanna check out the tutorial on this site also if you haven't.
    Anywayzz..
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout << "Hello world\n";
    
        return 0;
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    C++ and Flash Programmer
    Join Date
    Jul 2004
    Posts
    7
    I got that code to work but from a book that i bought it had the code i just posted (i forget the <iostream.h>)

    So is the book bogus

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    1:
    void main is tottaly wrong you should look into getting a new book
    2:
    your code should look like this if you want it to work
    Code:
    #include <iostream>
    using namespace std;
    
    int main(void)
    {
      cout<<"Using C++"; //you forgot the opening quote
      cin.get();//so your program won't open then close 
      return 0;//you have to return 0
    }
    But you should really really really get a new book
    Woop?

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    13
    What book is this from?

    I only ask so I can avoid any works from that publisher.

    'Main' must return a value of 0 to the operating sytem, so "void main ()" is wrong. It must return a zero to complete succesfully.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by derrick
    'Main' must return a value of 0 to the operating sytem, so "void main ()" is wrong. It must return a zero to complete succesfully.
    The C++ rule is that programs must return an int when they exit (back to the Operating System or back to whatever other application program invoked them).

    Historically command-line scripts (batch files for you Windows users) and programs that invoke other applications have used a return value of 0 to mean "normal execution and exit", whereas positive ints 1, 2, 3, etc. were used by the application to indicate more-or-less severe error conditions. The actual return value is defined by an application to be anything (any int, that is) it wants. The program that invokes the application may or may not use the returned value; since each application gets to define the meaning of returned values this must have been made known to any calling programs. (For example, in the Applications Programming Interface documentation, if it exists.)


    Dave

  7. #7
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Quote Originally Posted by derrick
    'Main' must return a value of 0 to the operating sytem, so "void main ()" is wrong. It must return a zero to complete succesfully.
    Not to mention compile under some compilers. Mingw (aka gcc) refuses to compile if I try to use void.

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    1 - Yes, your book is wrong, and/or outdated.*

    2 - This does NOT make your book useless! If you continue to program, you will need more books. But, don't throw this one away unless you find other problems with it.

    3 - When you type-in examples from the book, get in the habit of:
    - int main()
    - #include using namespace std.
    - Leave the dot-h off of the standard header files.


    You can search the board for more information about namespaces. (It's an intermediate to advanced topic, so just use namespace std for now.)

    You will always get flamed for using void main() here. And you might get a friendly reminder if you use .h headers.

    * As I understand it...
    - void main() may have been valid in C. Has NEVER been valid in C++.
    - The C language used .h headers.
    - The first C++ language standard used .h headers, but they are "depreciated" and should not be used in new C++ code. They are still valid to keep compilers backward-compatable with C.

    [EDIT]
    Thanks jlou.
    Stroustrup is usually a pretty reliable source.
    Last edited by DougDbug; 09-01-2004 at 01:58 PM.

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Quote Originally Posted by DougDbug
    * As I understand it...
    - void main() may have been valid in C. Has NEVER been valid in C++.
    - The C language used .h headers.
    - The first C++ language standard used .h headers, but they are "depreciated" and should not be used in new C++ code. They are still valid to keep compilers backward-compatable with C.
    As I understand it...
    - void main() has never been valid C either. (source)
    - There has really only been one C++ standard, but before that was finished the C++ library headers did have a .h. In the standard, the old versions of the C++ library headers aren't mentioned, so they are completely non-standard. People with older compilers, books, and tutorials still use them. On the other hand, the C library headers with the .h are standard, but they are deprecated. So to be standards compliant you should not use the C++ headers with .h (e.g. iostream.h or fstream.h) but it is ok to use C headers with the .h (e.g. stdlib.h, math.h). Of course it is preferred to use the new headers for everything if you compiler supports it, and consider upgrading your compiler if it doesn't. (source)

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>"depreciated"
    Deprecated you mean?

    >>e.g. stdlib.h
    Shouldn't that be <cstdlib> and (I'm not sure about this one) <cmath>?

    And Nexus, I'd be very careful about using code from that book, especially since:
    a) It uses 'void' main
    b) It uses old headers
    b) It has a missing quotation mark on the cout line

    If they can't get a "Hello World" program right, can you really expect them to get anything else right?

    **EDIT**
    Ah yes, and as alphaoide mentioned, here's a link to the tutorial on this site (it's got the guaranteed approval of all the gurus around here ):
    http://www.cprogramming.com/tutorial.html
    Last edited by Hunter2; 09-01-2004 at 02:45 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Quote Originally Posted by Hunter2
    >>e.g. stdlib.h
    Shouldn't that be <cstdlib> and (I'm not sure about this one) <cmath>?
    In that context I meant stdlib.h and math.h, because I was saying that those headers, while deprecated, are still standard. And yes, <cstdlib> and <cmath> are the appropriate replacements.

    By the way, if you use the tutorials on this site, you will have a similar problem as that old book. They still use non-standard headers. I've read around here that they are supposed to be updated at some point, but it doesn't look like it has happened yet.

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>those headers, while deprecated, are still standard.
    Oh, excellent news! I thought all my programs were nonstandard
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    ARRRGGGGHHH!!!
    Join Date
    Sep 2004
    Posts
    2
    I've recently begun trying to teach myself C++ and I've been recommended to several tutorial sites. The problem is, they all begin with a HelloWorld app that looks like this:

    // My First Program in C++

    #include <iostream.h>

    int Main ()
    {
    cout <<"Hello World!";
    return 0;
    }

    As you probably realize, this does not work. My compiler yelled at me for using a depreciated header (the .h) which I then removed. Now I get a 'linker' error that tells of am 'undefined reference to WinMain@16' and I cannot comprehend this statement. Little help please?

  14. #14
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    It depends on your compiler....

    Make sure you created a Console Application, not a Windows application. When the compiler looks for WinMain, it is looking for the entry point to a windows app, but for hello world you only need the console.

    Also, make sure you make main() all lower case. Here is program that should run on a compiler that complains about deprecated headers:
    Code:
    #include <iostream>
    
    int main()
    {
        std::cout << "Hello World!";
        return 0;
    }

  15. #15
    Teenage Mutant Ninja Nerd MMD_Lynx's Avatar
    Join Date
    Aug 2004
    Posts
    65
    it might be a typo. int Main() should be int main()
    Stupid people are useful. You can make them do all the mindless tasks you are too lazy to do yourself.

    Sphynx cats are just bald and wrinkly, like old people, and we don't reject them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 07:49 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM