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

  1. #16
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If you're still encountering the same problem you described initially, then perhaps it would be best for you to create a dummy "main()" function which calls your function and still exhibits the behavior you're seeing, and post it up here. This way, others would be able to compile it and perhaps be able to offer better explanations.

  2. #17
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Quote Originally Posted by Matticus View Post
    If you're still encountering the same problem you described initially, then perhaps it would be best for you to create a dummy "main()" function which calls your function and still exhibits the behavior you're seeing, and post it up here. This way, others would be able to compile it and perhaps be able to offer better explanations.
    As requested. and YES I UNDERSTAND "IM USING MALLOC and wrong practice blah blah blah" but honestly can we please keep the criticism constructive pertaining to the error itself.

    Code:
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    #include <cmath>
    #include <stdio.h>
    #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")
        {
            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
        {
            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 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 locomotive_counter; // this used to return the value 3 that I used just as an example. But in reality I use the variable locomotive_counter
                        }
                }
                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;
    }
    
    int main ()
    {
        int output = locomotives_required (55, 2986, 13);
        cout<< output;
        return 0;
    }
    And then you guys might need to save a text file called LoadTables.txt in your C drive. The contents of the file are as follows (copy and paste it in your text file:

    2 7
    GC 0 1 2 3 4 5 6
    1 0 290 590 880 1170 - -
    2 0 340 680 1020 1360
    3 0 390 770 1160 1400
    4 0 430 860 1290 1400
    5 0 470 940 1400
    6 0 510 1010 1400
    7 0 540 1090 1400
    8 0 580 1170 1400
    9 0 610 1220 1400
    10 0 670 1340 1400
    11 0 720 1400
    12 0 770 1400
    13 0 820 1400
    14 0 860 1400
    15 0 960 1400
    16 0 1090 1400
    17 0 1190 1400
    18 0 1230 1400

    7 12.5
    GC 0 1 2 3 4 5 6
    1 0 340 690 1030 1380 - -
    2 0 410 820 1230 1630
    3 0 470 940 1410 1880
    4 0 530 1060 1590 2120
    5 0 590 1180 1770 2360
    6 0 650 1290 1940 2500
    7 0 700 1400 2110 2500
    8 0 770 1530 2300 2500
    9 0 810 1620 2430 2500
    10 0 910 1820 2500
    11 0 1010 2010 2500
    12 0 1100 2190 2500
    13 0 1180 2370 2500
    14 0 1270 2500
    15 0 1490 2500
    16 0 1810 2500
    17 0 2080 2500
    18 0 2210 2500

    12.5 17
    GC 0 1 2 3 4 5 6
    1 0 360 720 1080 1430 - -
    2 0 430 850 1280 1710
    3 0 490 990 1480 1980
    4 0 560 1120 1680 2240
    5 0 620 1250 1870 2500
    6 0 690 1380 2060 2750
    7 0 750 1500 2250 3000
    8 0 820 1650 2470 3290
    9 0 870 1740 2610 3400
    10 0 990 1980 2960 3400
    11 0 1100 2200 3300 3400
    12 0 1210 2420 3400
    13 0 1310 2630 3400
    14 0 1420 2830 3400
    15 0 1700 3400
    16 0 2120 3400
    17 0 2480 3400
    18 0 2680 3400

    17 19
    GC 0 1 2 3 4 5 6
    1 0 360 730 1090 1460 - -
    2 0 430 870 1300 1740
    3 0 500 1010 1510 2010
    4 0 570 1140 1710 2290
    5 0 640 1280 1920 2550
    6 0 700 1410 2110 2820
    7 0 770 1540 2310 3080
    8 0 850 1690 2540 3390
    9 0 900 1790 2690 3590
    10 0 1020 2040 3060 3800
    11 0 1140 2280 3420 3800
    12 0 1260 2510 3770 3800
    13 0 1370 2740 3800
    14 0 1480 2960 3800
    15 0 1790 3590 3800
    16 0 2260 3800
    17 0 2680 3800
    18 0 2900 3800

    19 20
    GC 0 1 2 3 4 5 6
    1 0 370 730 1100 1460 - -
    2 0 440 870 1310 1750
    3 0 510 1010 1520 2030
    4 0 580 1150 1730 2300
    5 0 640 1290 1930 2570
    6 0 710 1420 2130 2840
    7 0 780 1550 2330 3100
    8 0 850 1710 2560 3420
    9 0 910 1810 2720 3620
    10 0 1030 2060 3090 4000
    11 0 1150 2310 3460 4000
    12 0 1270 2540 3820 4000
    13 0 1390 2780 4000
    14 0 1500 3000 4000
    15 0 1820 3650 4000
    16 0 2310 4000
    17 0 2740 4000
    18 0 2980 4000
    Last edited by Vespasian; 01-17-2014 at 11:43 AM.

  3. #18
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Turn on warnings; DO NOT ignore the warnings!!!!

    Code:
    mingw32-g++.exe -Wall -fexceptions -g -Wmissing-include-dirs -Wfatal-errors -Wno-unused-local-typedefs  -c E:\OpenSourceCode\Test\testcpp\main.cpp -o obj\Debug\main.o
    E:\OpenSourceCode\Test\testcpp\main.cpp:87:33: warning: multi-character character constant [-Wmultichar]
         for (int i = 1; *wordptr != 'EOF'; i++) //select the correct table
                                     ^
    E:\OpenSourceCode\Test\testcpp\main.cpp:113:21: warning: multi-character character constant [-Wmultichar]
         if (*wordptr == 'EOF')
                         ^
    E:\OpenSourceCode\Test\testcpp\main.cpp: In function 'float ceiling(float, float)':
    E:\OpenSourceCode\Test\testcpp\main.cpp:23:1: warning: control reaches end of non-void function [-Wreturn-type]
     }
     ^
    E:\OpenSourceCode\Test\testcpp\main.cpp: In function 'int locomotives_required(double, double, double)':
    E:\OpenSourceCode\Test\testcpp\main.cpp:115:1: warning: control reaches end of non-void function [-Wreturn-type]
     }
     ^
    "...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. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    *wordptr != 'EOF'
    should be
    wordptr != nullptr
    or just
    wordptr

    strtok (which you shouldn't use, btw) returns a nullptr when no more matches are found.
    You should use streams or string's find functions instead of strtok. You are leaking memory and potentially wandering into undefined land.
    Also fix the warnings, as pointed out already.
    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.

  5. #20
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    As requested. and YES I UNDERSTAND "IM USING MALLOC and wrong practice blah blah blah" but honestly can we please keep the criticism constructive pertaining to the error itself.
    Don't worry about me - I'm not even a C++ programmer

    Code:
    ||=== scrap_cpp, Debug ===|
    main.cpp|87|warning: multi-character character constant|
    main.cpp|113|warning: multi-character character constant|
    main.cpp||In function 'int locomotives_required(double, double, double)':|
    main.cpp|115|warning: control reaches end of non-void function|
    main.cpp||In function 'float ceiling(float, float)':|
    main.cpp|23|warning: control reaches end of non-void function|
    ||=== Build finished: 0 errors, 4 warnings ===|
    On top of this, when I patch the 'EOF' problem, I keep getting seg faults. Are you sure everything is otherwise compiling/running correctly for you?

    [EDIT] I see you're getting responses now, so I'll bow out here. Best of luck.

  6. #21
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Quote Originally Posted by stahta01 View Post
    Turn on warnings; DO NOT ignore the warnings!!!!

    Code:
    mingw32-g++.exe -Wall -fexceptions -g -Wmissing-include-dirs -Wfatal-errors -Wno-unused-local-typedefs  -c E:\OpenSourceCode\Test\testcpp\main.cpp -o obj\Debug\main.o
    E:\OpenSourceCode\Test\testcpp\main.cpp:87:33: warning: multi-character character constant [-Wmultichar]
         for (int i = 1; *wordptr != 'EOF'; i++) //select the correct table
                                     ^
    E:\OpenSourceCode\Test\testcpp\main.cpp:113:21: warning: multi-character character constant [-Wmultichar]
         if (*wordptr == 'EOF')
                         ^
    E:\OpenSourceCode\Test\testcpp\main.cpp: In function 'float ceiling(float, float)':
    E:\OpenSourceCode\Test\testcpp\main.cpp:23:1: warning: control reaches end of non-void function [-Wreturn-type]
     }
     ^
    E:\OpenSourceCode\Test\testcpp\main.cpp: In function 'int locomotives_required(double, double, double)':
    E:\OpenSourceCode\Test\testcpp\main.cpp:115:1: warning: control reaches end of non-void function [-Wreturn-type]
     }
     ^
    My compiler seemed to have ignored those even though, if you saw, on my previous comment at the end of tha last page that I said I switched it on. PS: Please repaste the code I posted above, I made a small edit.

  7. #22
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Quote Originally Posted by Elysia View Post
    *wordptr != 'EOF'
    should be
    wordptr != nullptr
    or just
    wordptr

    strtok (which you shouldn't use, btw) returns a nullptr when no more matches are found.
    You should use streams or string's find functions instead of strtok. You are leaking memory and potentially wandering into undefined land.
    Also fix the warnings, as pointed out already.
    I tried this. And whilst it still compiles, the error is still there. With all due respect, are my funny results coming from these warnings or are they just a side note?

  8. #23
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You may want to start by increasing your compiler warning level and fixing all the warnings being generated by your code:

    main.cpp|87|warning: multi-character character constant [-Wmultichar]|
    main.cpp|113|warning: multi-character character constant [-Wmultichar]|
    main.cpp||In function ‘float ceiling(float, float)’:|
    main.cpp|15|warning: no previous declaration for ‘float ceiling(float, float)’ [-Wmissing-declarations]|
    main.cpp|19|warning: comparing floating point with == or != is unsafe [-Wfloat-equal]|
    main.cpp|21|warning: comparing floating point with == or != is unsafe [-Wfloat-equal]|
    main.cpp||In function ‘char* tokenizer(std::string)’:|
    main.cpp|25|warning: no previous declaration for ‘char* tokenizer(std::string)’ [-Wmissing-declarations]|
    main.cpp||In function ‘double slot_capacity_required(double, double, double, double)’:|
    main.cpp|48|warning: no previous declaration for ‘double slot_capacity_required(double, double, double, double)’ [-Wmissing-declarations]|
    main.cpp||In function ‘double wagons_required(double, double, double)’:|
    main.cpp|53|warning: no previous declaration for ‘double wagons_required(double, double, double)’ [-Wmissing-declarations]|
    main.cpp||In function ‘double tons_per_axle(double)’:|
    main.cpp|58|warning: no previous declaration for ‘double tons_per_axle(double)’ [-Wmissing-declarations]|
    main.cpp||In function ‘double train_tons_to(double, double, double)’:|
    main.cpp|63|warning: no previous declaration for ‘double train_tons_to(double, double, double)’ [-Wmissing-declarations]|
    main.cpp||In function ‘double train_tons_from(double, double)’:|
    main.cpp|68|warning: no previous declaration for ‘double train_tons_from(double, double)’ [-Wmissing-declarations]|
    main.cpp||In function ‘int locomotives_required(double, double, double)’:|
    main.cpp|74|warning: no previous declaration for ‘int locomotives_required(double, double, double)’ [-Wmissing-declarations]|
    main.cpp|87|warning: comparison is always true due to limited range of data type [-Wtype-limits]|
    main.cpp|94|warning: comparing floating point with == or != is unsafe [-Wfloat-equal]|
    main.cpp|113|warning: comparison is always false due to limited range of data type [-Wtype-limits]|
    main.cpp||In function ‘float ceiling(float, float)’:|
    main.cpp|23|warning: control reaches end of non-void function [-Wreturn-type]|
    main.cpp||In function ‘int locomotives_required(double, double, double)’:|
    main.cpp|115|warning: control reaches end of non-void function [-Wreturn-type]|
    ||=== Build finished: 0 errors, 17 warnings (0 minutes, 1 seconds) ===|
    With all due respect, are my funny results coming from these warnings or are they just a side note?
    Yes, it is most likely one of these warnings that are causing your problems.

    Jim
    Last edited by jimblumberg; 01-17-2014 at 11:53 AM.

  9. #24
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Anybody here using code::blocks? I switched warnings to the max or atleast so I think I did.

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    They are more than likely part of your problems.
    Comparing against 'EOF' is wrong and will never give you sensible results unless you're very lucky.
    Your functions not returning something can result in undefined behaviour, returning garbage to you, or crashing if you're unlucky.
    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.

  11. #26
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Anybody here using code::blocks?
    Yes, I use Code::Blocks, with g++ 4.8.1, and my warning messages are as posted.

    Jim

  12. #27
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Vespasian View Post
    I tried this. And whilst it still compiles, the error is still there. With all due respect, are my funny results coming from these warnings or are they just a side note?
    strtok can never ever ever ever ever (ever) return anything like 'EOF'. Plus you're leaking memory like crazy, since you malloc a bunch of memory and then throw it away with strtok (which just sets your pointer to point inside the original string, not copy into your new string). Also I'm not sure atof will be happy about getting NULL pointers for those lines that are short -- you should check that, rather than blindly sending the pointer on.

  13. #28
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Quote Originally Posted by jimblumberg View Post
    Yes, I use Code::Blocks, with g++ 4.8.1, and my warning messages are as posted.

    Jim
    Im using it on windows and I go to compiler settings and Im trying to switch on enable warnings.
    Last edited by Vespasian; 01-17-2014 at 12:02 PM.

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I strongly suggest that you use braces even when they are unnecessary because the body of the if statement (etc) consists of a single statement: you still have not fixed the logic error that I pointed out related to the indentation problem that I noted.
    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

  15. #30
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    How did you try to increase warning levels?

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

    Try selecting most of the warning flags contained on this menu item.

    Jim

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