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

  1. #31
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Hi Jim, as shown below

    Return statement is NOT exiting the function!!!-screenshot-jpg

    Return statement is NOT exiting the function!!!-capture-jpg

  2. #32
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Remember to look at "build log" NOT the "message log" to see the Compiler output in the Code::Blocks IDE/editor.
    I suggest making sure full compiler logging is on, also.
    FAQ-Compiling (errors) - CodeBlocks

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #33
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Also, do NOT use Build and Run in CB; sometimes its hard to see the build log before its over written by the run log information.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #34
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Did all that. Enabled all warnings as screen shot above. Did it for both the project and for global. Did it for both the debug and the release. Looking at the build log and all other logs.

    No warnings whatsoever.

  5. #35
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    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

  6. #36
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Quote Originally Posted by laserlight View Post
    What is your current code?
    Pretty much the same as the opening code, here it is again (I havent yet corrected the illogical if statement as you have suggested, still need to do that):

    Code:
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    #include <cmath>
    #include <string>
    #include <cstring>
    
    #define ERROR_LOW_WAGON_WEIGHT 101
    #define ERROR_HIGH_WAGON_WEIGHT 102
    #define ERROR_HIGH_GRADIENT 103
    
    using namespace std;
    
    float ceiling(float unrounded, float increment)
    {
        float remainder = fmod (unrounded, increment);
        int quotient = unrounded/increment;
        if (remainder == 0)
            return (unrounded);
        else if (remainder != 0)
            return (quotient*increment + increment);
    }
    
    char* tokenizer (string inputstream)
    {
        if (inputstream != "NULL")  // If parameter is equal to string "NULL", then invoke the strtok function by tokenizing the first token in parameter
        {
            char* cstringcpy = (char*)malloc (50 * sizeof(char));
            const char* cstr = inputstream.c_str();
            strcpy(cstringcpy,cstr);
            char* wordptr = (char*)malloc (50 * sizeof(char));
            wordptr = strtok(cstringcpy,"\t");
            return wordptr;
        }
        else // Else parameter is equal to string non-"NULL", then invoke the strtok function by tokenizing the second and n'th token in the parameter
        {
            char* cstringcpy = (char*)malloc (50 * sizeof(char));
            const char* cstr = inputstream.c_str();
            strcpy(cstringcpy,cstr);
            char* wordptr = (char*)malloc (50 * sizeof(char));
            wordptr = strtok(NULL,"\t");
            return wordptr;
        }
    
    }
    
    double slot_capacity_required (double volume_per_month, double tons_per_wagon, double wagons_per_train, double operating_weeks_per_year)
    {
        return (volume_per_month/(tons_per_wagon * wagons_per_train)) * 12 / operating_weeks_per_year;
    }
    
    double wagons_required (double slot_capacity_required, double turn_around_time, double wagons_per_train)
    {
        return ceiling((slot_capacity_required/(168/turn_around_time))*wagons_per_train, wagons_per_train);
    }
    
    double tons_per_axle (double tons_per_wagon)
    {
        return tons_per_wagon/4;
    }
    
    double train_tons_to (double tons_per_wagon, double wagons_per_train, double wagon_weight)
    {
        return (tons_per_wagon + wagon_weight) * wagons_per_train;
    }
    
    double train_tons_from (double wagons_per_train, double wagon_weight)
    {
        return (wagon_weight) * wagons_per_train;
    }
    
    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:\\LoadTables.txt", std::ofstream::out | std::ofstream::app);
        string lineread = "";
        getline (ifs, lineread);
        char* wordptr = tokenizer(lineread);
        int locomotive_counter = 0;
    
        if (tons_per_axle_variable < atoi(wordptr))
            return ERROR_LOW_WAGON_WEIGHT;
    
        for (int i = 1; *wordptr != EOF; i++)
        {
            wordptr = tokenizer("NULL");
            if (tons_per_axle_variable < atof(wordptr))
                for (int j = 1; j <= 18; j++)
                {
                    getline (ifs, lineread);
                    if (gradient == atof(tokenizer(lineread)))
                        for (int k = 0; k <= 6; k++)
                        {
                            if (train_tons <= atof(tokenizer("NULL")))
                                return locomotive_counter;
                            locomotive_counter++;
                        }
                }
    
            else
            {
                getline(ifs,lineread);
                while (lineread != "")
                    getline(ifs,lineread);
                getline(ifs,lineread);
                wordptr = tokenizer(lineread);
            }
        }
    
        if (*wordptr == EOF);
            return ERROR_HIGH_WAGON_WEIGHT;
    }
    
    int main ()
    {
        int output = locomotives_required (55, 2986, 13);
        cout<< output;
        return 0;
    }

  7. #37
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    EDIT: It seems to show the warnings now i.e. only when I deliberately insert a compiler error like a simple syntax error. Then all warnings show. Otherwise if there are no compiler errors then it wont show anything.

  8. #38
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Non-warning changes: - I changed the text file name to ldtbl.txt and used local directory instead of C:\. I added "endl" to the cout in main().

    Warning changes: I changed ceiling() to use doubles instead of floats, and cast the division result to (int) to get rid of warnings. At the end of locomotives(), I changed line 126 to remove the trailing semicolon and added a return ERROR_UNKNOWN statement. Code seems to be working.

    Code:
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    #include <cmath>
    #include <string>
    #include <cstring>
    
    
    #define ERROR_LOW_WAGON_WEIGHT 101
    #define ERROR_HIGH_WAGON_WEIGHT 102
    #define ERROR_HIGH_GRADIENT 103
    #define ERROR_UNKNOWN 104
    
    
    using namespace std;
    
    
    double ceiling(double unrounded, double increment)
    {
        double remainder = fmod (unrounded, increment);
        int quotient = (int)(unrounded/increment);
        if (remainder == 0)
            return (unrounded);
        return (quotient*increment + increment);
    }
    
    
    char* tokenizer (string inputstream)
    {
        if (inputstream != "NULL")  // If parameter is equal to string "NULL", then invoke the strtok function by tokenizing the first token in parameter
        {
            char* cstringcpy = (char*)malloc (50 * sizeof(char));
            const char* cstr = inputstream.c_str();
            strcpy(cstringcpy,cstr);
            char* wordptr = (char*)malloc (50 * sizeof(char));
            wordptr = strtok(cstringcpy,"\t");
            return wordptr;
        }
        else // Else parameter is equal to string non-"NULL", then invoke the strtok function by tokenizing the second and n'th token in the parameter
        {
            char* cstringcpy = (char*)malloc (50 * sizeof(char));
            const char* cstr = inputstream.c_str();
            strcpy(cstringcpy,cstr);
            char* wordptr = (char*)malloc (50 * sizeof(char));
            wordptr = strtok(NULL,"\t");
            return wordptr;
        }
    
    
    }
    
    
    double slot_capacity_required (double volume_per_month, double tons_per_wagon, double wagons_per_train, double operating_weeks_per_year)
    {
        return (volume_per_month/(tons_per_wagon * wagons_per_train)) * 12 / operating_weeks_per_year;
    }
    
    
    double wagons_required (double slot_capacity_required, double turn_around_time, double wagons_per_train)
    {
        return ceiling((slot_capacity_required/(168/turn_around_time))*wagons_per_train, wagons_per_train);
    }
    
    
    double tons_per_axle (double tons_per_wagon)
    {
        return tons_per_wagon/4;
    }
    
    
    double train_tons_to (double tons_per_wagon, double wagons_per_train, double wagon_weight)
    {
        return (tons_per_wagon + wagon_weight) * wagons_per_train;
    }
    
    
    double train_tons_from (double wagons_per_train, double wagon_weight)
    {
        return (wagon_weight) * wagons_per_train;
    }
    
    
    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 ("ldtbl.txt", std::ofstream::out | std::ofstream::app);
        string lineread = "";
        getline (ifs, lineread);
        char* wordptr = tokenizer(lineread);
        int locomotive_counter = 0;
    
    
        if (tons_per_axle_variable < atoi(wordptr))
            return ERROR_LOW_WAGON_WEIGHT;
    
    
        for (int i = 1; *wordptr != NULL; i++)
        {
            wordptr = tokenizer("NULL");
            if (tons_per_axle_variable < atof(wordptr))
                for (int j = 1; j <= 18; j++)
                {
                    getline (ifs, lineread);
                    if (gradient == atof(tokenizer(lineread)))
                        for (int k = 0; k <= 6; k++)
                        {
                            if (train_tons <= atof(tokenizer("NULL")))
                                return locomotive_counter;
                            locomotive_counter++;
                        }
                }
    
    
            else
            {
                getline(ifs,lineread);
                while (lineread != "")
                    getline(ifs,lineread);
                getline(ifs,lineread);
                wordptr = tokenizer(lineread);
            }
        }
    
    
        if (*wordptr == NULL)       // removed semi-colon from this statement
            return ERROR_HIGH_WAGON_WEIGHT;
        return ERROR_UNKNOWN;
    }
    
    
    int main ()
    {
        int output = locomotives_required (55, 2986, 13);
        cout << output << endl;
        return 0;
    }
    Last edited by rcgldr; 01-20-2014 at 11:04 AM.

  9. #39
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Vespasian View Post
    EDIT: It seems to show the warnings now i.e. only when I deliberately insert a compiler error like a simple syntax error. Then all warnings show. Otherwise if there are no compiler errors then it wont show anything.
    If you want the warnings fixed post the Full Compiler Re-build log in code tags.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #40
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Quote Originally Posted by stahta01 View Post
    If you want the warnings fixed post the Full Compiler Re-build log in code tags.

    Tim S.
    Cant copy results effectively, I hope a screen tag will do?

    Return statement is NOT exiting the function!!!-log-jpg

    Also, I cannot seem to get the warning fields jblumberg gets. I like the -Wtype-limits and -Wreturn-type. I dont get these despite enabling everything

  11. #41
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Quote Originally Posted by rcgldr View Post
    Non-warning changes: - I changed the text file name to ldtbl.txt and used local directory instead of C:\. I added "endl" to the cout in main().

    Warning changes: I changed ceiling() to use doubles instead of floats, and cast the division result to (int) to get rid of warnings. At the end of locomotives(), I changed line 126 to remove the trailing semicolon and added a return ERROR_UNKNOWN statement. Code seems to be working.
    I think you're onto something here and its your last return statement.

    Let me explain to everyone if this makes sense.

    I cleaned the code up until there were no warnings left or atleast there were some warnings about comparing int to float but that aside, nothing serious. Also I couldnt enable -Wreturn-type and -Wtype- as my compiler couldnt find these warnings. The former warning is the culprit as I will soon show!

    Anyways, so I then ran the code as clean as I could using my compilers settings using rcgldr advice (barring his last return statement) and I still got the random return number.

    I then added the last rcgldr return statement and voila, the code runs without the funny random number error.

    So my question is, nobody has fully explained this error despite all the warnings being cleaned up (APART from -Wreturn-type and -Wtype-limits which I cant seem to get my compiler to enable)

    So I have a theory that I would like your thoughts on.

    Is the random number generated because all my return statements were embedded in conditional loops? I mean look at the two return statements, theyre both in conditional statements. I also did some reading on the net and a bloke on stack overflow wrote about the -Wreturn-type error as follows:

    The compiler cannot tell from that code if the function will ever reach the end and still return something.
    This then explains why rcgldr's last added return statement works: Its not within a conditional statement.
    This also explains why adding a trailing ; after the last if statement and leaving
    Code:
            return ERROR_HIGH_WAGON_WEIGHT;
    out of the if statement works.

    Thoughts??

  12. #42
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Vespasian View Post
    Cant copy results effectively, I hope a screen tag will do?

    Return statement is NOT exiting the function!!!-log-jpg

    Also, I cannot seem to get the warning fields jblumberg gets. I like the -Wtype-limits and -Wreturn-type. I dont get these despite enabling everything
    The "Build Log" NOT the "Build Message" and maybe you would be able to be helped!!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  13. #43
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Quote Originally Posted by stahta01 View Post
    The "Build Log" NOT the "Build Message" and maybe you would be able to be helped!!

    Tim S.
    Whoops, sorry, see below:

    Code:
    -------------- Build: Debug in b ---------------
    
    mingw32-g++.exe -Wall  -Wshadow -Winit-self -Wredundant-decls -Wcast-align -Wundef -Wfloat-equal -Winline -Wunreachable-code -Wmissing-declarations -Wmissing-include-dirs -Wswitch-enum -Wswitch-default -Weffc++ -Wmain -pedantic -Wextra -Wall -g  -Wshadow -Winit-self -Wredundant-decls -Wcast-align -Wundef -Wfloat-equal -Winline -Wunreachable-code -Wmissing-declarations -Wmissing-include-dirs -Wswitch-enum -Wswitch-default -Weffc++ -Wmain -pedantic-errors -pedantic -Wextra -Wall -g   -I"C:\Program Files (x86)\CLIPS\Projects\Libraries\Microsoft"  -c "C:\Users\D\Documents\C Code\b\Untitled4.cpp" -o obj\Debug\Untitled4.o
    C:\Users\D\Documents\C Code\b\Untitled4.cpp: In function 'double ceiling(double, double)':
    C:\Users\D\Documents\C Code\b\Untitled4.cpp:28: warning: comparing floating point with == or != is unsafe
    C:\Users\D\Documents\C Code\b\Untitled4.cpp:30: warning: comparing floating point with == or != is unsafe
    C:\Users\D\Documents\C Code\b\Untitled4.cpp: In function 'int locomotives_required(double, double, double)':
    C:\Users\D\Documents\C Code\b\Untitled4.cpp:102: warning: comparing floating point with == or != is unsafe
    C:\Users\D\Documents\C Code\b\Untitled4.cpp:119: error: expected primary-expression before '.' token
    C:\Users\D\Documents\C Code\b\Untitled4.cpp:121: error: expected unqualified-id before 'if'
    C:\Users\D\Documents\C Code\b\Untitled4.cpp:121: error: expected ';' before 'if'
    Process terminated with status 1 (0 minutes, 0 seconds)
    3 errors, 3 warnings
    Build log saved as: 
    file://C:%5cUsers%5cD%5cDocuments%5cC%20Code%5cb%5cb_build_log.html

  14. #44
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I did NOT see any option that looked wrong.
    Could be an IDE Bug or user error.

    I suggest looking at "Build Log" whenever things are NOT working right. And, doing a re-build when things are NOT working right.

    The warning and pedantic options sorted with dups removed.
    In case someone else see a conflict.
    Code:
    -Wall
    -Wcast-align 
    -Weffc++ 
    -Wextra 
    -Wfloat-equal 
    -Winit-self 
    -Winline 
    -Wmain 
    -Wmissing-declarations 
    -Wmissing-include-dirs 
    -Wredundant-decls 
    -Wshadow 
    -Wswitch-default 
    -Wswitch-enum 
    -Wundef 
    -Wunreachable-code 
    -pedantic 
    -pedantic-errors
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

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