Needing Assistance;Unreachable code in function main()

This is a discussion on Needing Assistance;Unreachable code in function main() within the C++ Programming forums, part of the General Programming Boards category; I am new to C++ and have just installed my Borland Compiler. Here is a very simple program I have ...

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

    Question Needing Assistance;Unreachable code in function main()

    I am new to C++ and have just installed my Borland Compiler.

    Here is a very simple program I have written:

    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    int main(){
    cout<< "My lucky number is ";
    int lnum;
    lnum = 3;
    lnum++;
    return lnum;
    return 0;
    }
    However, whenever I try to compile it, I get the following message:

    Code:
    myfirstprogram.cpp:
    Warning W8066 myfirstprogram.cpp 10: Unreachable code in function main()
    When I run the program, the "cout" is fine, and the output is "My lucky number is " as specified, but my variable "lnum" is not returned.

    Thanks for reading, and please help me with my problem.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    It does not make sense to return 0 after returning lnum since control can never reach the line that returns 0. It is only a warning because iit might not really matter, though it may be an indication of a bug. In fact, you might have a bug since it does not make sense to return lnum from main; you probably want to print lnum instead.
    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
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    Thank you very much for your help. So, would I simply omit:

    Code:
    return 0;

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Well, while you could do that, what you really want to do is to print lnum instead of returning it.
    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

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    You would also do well to indent your code.
    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.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    Laserlight: Thanks a lot, I've got it working now =).

    Elysia: Thanks, I'll keep that in mind.

    Thanks a lot, I can get going on other stuff now! Man, that mistake looks so silly now...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to change it into one main() function code..
    By transgalactic2 in forum C Programming
    Replies: 10
    Last Post: 12-13-2008, 09:02 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM

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