Thread: Needing Assistance;Unreachable code in function main()

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    Well, while you could do that, what you really want to do is to print lnum instead of returning it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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, 10: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