Thread: Return statement is NOT exiting the function!!!

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181

    Return statement is NOT exiting the function!!!

    EDIT: Title of thread should read "Return statement affected by other return statements"

    Hi all, I have a piece of code that resembles the following:

    Code:
    int locomotives_required (double tons_per_wagon, double train_tons, double gradient)
    {
        double tons_per_axle_variable = tons_per_axle(tons_per_wagon);
        ifstream ifs;
        ifs.open ("C:\\C++ Projects\\Coal Logistics\\LoadTables.txt", std::ofstream::out | std::ofstream::app);
        string lineread;
        getline (ifs, lineread);
        char* wordptr = tokenizer(lineread);
        int number = atoi(wordptr);
        int j;
        int locomotive_counter = 0;
        if (tons_per_axle_variable < number)
            return ERROR_LOW_WAGON_WEIGHT;
        for (int i = 1; *wordptr != 'EOF'; i++) //select the correct table
        {
            wordptr = tokenizer("NULL");
            if (tons_per_axle_variable < atof(wordptr))
                for (j = 1; j <= 18; j++)
                {
                    getline (ifs, lineread);
                    if (gradient == atof(tokenizer(lineread)))
                        for (int k = 0; k <= 6; k++)
                        {
                            locomotive_counter++;
                            if (train_tons <= atof(tokenizer("NULL")))
                                return 3;
                        }
                }
                if (j == 19)
                    return ERROR_HIGH_GRADIENT;
            else
            {
                getline(ifs,lineread);
                while (lineread != "")
                    getline(ifs,lineread);
                getline(ifs,lineread);
                wordptr = tokenizer(lineread);
            }
        }
        if (*wordptr == 'EOF')
            return ERROR_HIGH_WAGON_WEIGHT;
    }
    Neglecting all the distractions, please look at line number 26. I have written this piece of code such that this called function always returns at line 26, which should return the number 3. I ran a debugger and checked step by step to make sure the function returns at line 26 and it does.

    Note that in theory, the return keywords at lines 30 and 41 should have no bearing on the outcome of returning the value "3" at line 26.

    Yet when I run this program and print the outcome I always get a 6-8 digit random number every time like: 767172 and 393620 etc...

    To solve this problem I COMMENT OUT lines 30 and 41 and rather make an empty statement like " ;" and finally the function returns the value 3 as it should.

    Can anybody explain this???
    Last edited by Vespasian; 01-16-2014 at 11:58 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function exit with out return statement
    By Martin.Ericsson in forum C Programming
    Replies: 3
    Last Post: 11-09-2012, 06:47 AM
  2. Replies: 6
    Last Post: 07-14-2012, 09:26 AM
  3. Pausing before exiting function?
    By Matus in forum C Programming
    Replies: 3
    Last Post: 10-21-2008, 03:00 PM
  4. function - return statement
    By chungt004 in forum C Programming
    Replies: 5
    Last Post: 02-27-2008, 08:33 PM
  5. exiting program and function
    By jumpy in forum C++ Programming
    Replies: 2
    Last Post: 01-17-2003, 02:36 AM