Thread: Code will not compile - code from a book!

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User Chubz's Avatar
    Join Date
    Sep 2004
    Posts
    16

    Code will not compile - code from a book!

    Ok, I am using Bloodshed-Dev C++ to compile (I just started learning this C++ stuff) and with this book that I recently bought, "C++ In Plain English", the code:

    EDIT - NOW WITH "CODE TAGS"
    EDIT 2 - NOW I GOT IT RIGHT!

    Code:
    #include <stdio.h>
    void main () {
            printf("Can you C++ now?");
    {

    does NOT want to compile

    What is wrong!? The other weird thing is that from the book reviews from Amazon.com, most were positive and even the negative ones never mentioned the code examples in the book not compiling correctly.

    This is horrible - what is going on here!!??
    Last edited by Chubz; 09-11-2004 at 09:31 PM. Reason: Added CodeTags

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    First, just a convenience detail. Use code tags (read announcement at top of forum).

    Anyway, what you have there is a { where there should be a }. And, 'void main' is bad. Use 'int main'. A search on this board will reveal a dozen answers as to why this is so. As a result, you will also want a 'return 0;' after your print. And... Your window will probably disappear too quickly to see anything. Check this site's FAQ for information to prevent this.

    Cheers.

  3. #3
    Registered User Chubz's Avatar
    Join Date
    Sep 2004
    Posts
    16
    Ok, I appreciate the help.

    Oh, and sorry for not using code tags, I will use them next time.

    Once I read about how to use 'em I will edit my original post and add code-tags for the code example!


  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    The book is horrible. First, it uses void main. Main should always return int (as Prelude says, "always has, always will" ). Second, it uses C headers (<stdio.h> instead of <cstdio>). Third, its indentation is bad for newbies. Fourth, it's got a typo - the last bracket should be a CLOSING bracket, not another opening one. And last, it uses printf() instead of cout.

    Here's a fixed up version of the program:
    Code:
    #include <iostream>
     
    int main()
    {
       //Write "Can you C++ now?" and then go to the next line
       std::cout << "Can you C++ now?" << std::endl;
     
       //Pause the program so it doesn't close right away
       char c;
       std::cin.get(c);
      
       //Quit the program
       return 0;
    }
    Just Google It. √

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

  5. #5
    Registered User Chubz's Avatar
    Join Date
    Sep 2004
    Posts
    16
    Quote Originally Posted by Hunter2
    The book is horrible. First, it uses void main. Main should always return int (as Prelude says, "always has, always will" ). Second, it uses C headers (<stdio.h> instead of <cstdio>). Third, its indentation is bad for newbies. Fourth, it's got a typo - the last bracket should be a CLOSING bracket, not another opening one. And last, it uses printf() instead of cout.

    Here's a fixed up version of the program:
    Code:
    #include <iostream>
     
    int main()
    {
       //Write "Can you C++ now?" and then go to the next line
       std::cout << "Can you C++ now?" << std::endl;
     
       //Pause the program so it doesn't close right away
       char c;
       std::cin.get(c);
      
       //Quit the program
       return 0;
    }
    WOAH, MAN I AM READY TO BURNNN THAT BOOK!

    Oh well, looks like I'm gonna have to find another book that is actually worth a crap.

    Thanks for the help, though! I intend to stay here for a while.

  6. #6
    Registered User Chubz's Avatar
    Join Date
    Sep 2004
    Posts
    16
    Quote Originally Posted by Hunter2
    Code:
    #include <iostream>
     
    int main()
    {
       //Write "Can you C++ now?" and then go to the next line
       std::cout << "Can you C++ now?" << std::endl;
     
       //Pause the program so it doesn't close right away
       char c;
       std::cin.get(c);
      
       //Quit the program
       return 0;
    }
    Ok, Now i'm REALLY confused! :P

    First of all, your code uses std::cout and many other different things than the tutorials on this website (cprogramming.com , of course) do.

    For example, on their first tutorial, instead of placing std::cout and all of that other stuff, this is what it looks like:

    Code:
    #include <iostream.h> 
    int main() 
    {
      cout<<"HEY, you, I'm alive!  Oh, and Hello World!"; 
      return 0;    
    }
    Of course, you must add "cin.get();" before the "return 0;" to get it to show up without closing, at least I did. :P

    Anyways, what is going on! SO many different ways of doing things - i'm confused out the yin-yang!

  7. #7
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Here is a decent tutorial that answers your question. You may want to start off with a tutorial first and then for a more indepth look get a book, and one that isn't Archaic Actually I hear pretty good things about Bruce Eckel's Thinking in C++ 2 it's a free e-book with tons of source code.
    As to the FAQ I trully believe it needs to be updated, just keep coding and experimenting and coming back to the board, that's what it's hear for.

    Cheers
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  8. #8
    Registered User Chubz's Avatar
    Join Date
    Sep 2004
    Posts
    16
    Quote Originally Posted by caroundw5h
    Here is a decent tutorial that answers your question. You may want to start off with a tutorial first and then for a more indepth look get a book, and one that isn't Archaic Actually I hear pretty good things about Bruce Eckel's Thinking in C++ 2 it's a free e-book with tons of source code.
    As to the FAQ I trully believe it needs to be updated, just keep coding and experimenting and coming back to the board, that's what it's hear for.

    Cheers
    Ah, awesome links!

    I'm about to check out that "Bruce Ecekl's Thinking in C++ 2" here in a few minutes, so hopefully that will give me some help.

    Thanks for the support guys! It really means alot to me!

  9. #9
    Registered User
    Join Date
    Dec 2003
    Posts
    6
    Another awesome book that will introduce you to ideas C++ and then explain it well is "C++ How To Program" by the Deitels. I got a copy on either Ebay or Amazon.com for less than $10.

  10. #10
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by Chubz
    Ah, awesome links!

    I'm about to check out that "Bruce Ecekl's Thinking in C++ 2" here in a few minutes, so hopefully that will give me some help.

    Thanks for the support guys! It really means alot to me!
    Your welcome let's see what comes out of it
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Hmm... Hunter's answer is more complete... That's all you'll need to run the program. It'd be worthwhile to look into cin and such so that you know what he is doing and why.

    Cheers.

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Do a search for books on this forum too. It has been discussed at length, and additionally, the name of a site with reviews of many programming books has been posted.

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Don't burn it yet, it might still be useful... i.e. to get revenge on a longtime enemy

    J/K man, even if a book sucks... well, get a refund if you can, but if you can't then keep it anyway because there's almost always something you can learn from it even if only what NOT to do
    Just Google It. √

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

  14. #14
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    I love my C++ Black Book.

    But its really just preference, as long as it is up to date (with the standard), and explains the code thouroughly (the C++ Black Book has "In-Depth" sections in each chapter).
    What is C++?

  15. #15
    Registered User Chubz's Avatar
    Join Date
    Sep 2004
    Posts
    16
    Ah, thanks for being so kind and helpful guys.

    I will check out those things that you told me and see if I can learn anything from that.

    Oh well, wish me luck!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you compile the following template function code?
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 03-06-2008, 08:16 PM
  2. Compile time of C versus C++ code
    By circuitbreaker in forum C++ Programming
    Replies: 20
    Last Post: 02-06-2008, 06:26 PM
  3. why the code can compile -- about function pointer
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 03:25 AM
  4. Trying to Compile a Dhrystone C Code program
    By JAYMAN in forum Linux Programming
    Replies: 3
    Last Post: 01-28-2006, 11:17 AM
  5. prime numbers code compile error
    By Tony654321 in forum C Programming
    Replies: 5
    Last Post: 10-10-2004, 10:13 AM