Why doesn't.....

This is a discussion on Why doesn't..... within the C++ Programming forums, part of the General Programming Boards category; Dev-c++ warn about functions declared with return types, but don't return anything....example: Code: #include <iostream> using std::cout; int greatest(int, int); ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    719

    Why doesn't.....

    Dev-c++ warn about functions declared with return types, but don't return anything....example:

    Code:
    #include <iostream>
    using std::cout;
    int greatest(int, int);
    int main()
    {
          cout << greatest(1,2);
          return 0; //this line added in after "The Brains" post
                          //...unintentionally forgot to add this line
    }
    int greatest(int a, int b)
    {
           //typically, i would not code a function like this
           //...it's just for example
           int biggest;
           if(a < b)
                 biggest = b;
           else
                 biggest = a;
           //return biggest;  //it seems that i have mistakenly commented out the return statement.
    }
    the console appears:
    Code:
    _
    as if the program was within an infinite loop.


    1. is there a good reason why there is no warning or error?
    2. is there any compiler variables (in dev-c++) i can set that will throw an error?
    Last edited by misplaced; 03-28-2005 at 11:36 PM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I think one posibility is that int main( ) is declared to return an int but it never explicitly returns anything.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    for me, the program just exectues and exits like it should, but Dev (GCC) throws me this warning:

    C:\Program Files\Dev-Cpp\bin\John\Untitled1.cpp In function `int greatest(int, int)':
    20 C:\Program Files\Dev-Cpp\bin\John\Untitled1.cpp [Warning] control reaches end of non-void function

    this is how you do it:
    1. open the Tools menu
    2. click Compiler Options
    3. make sure you're on the Compiler tab
    4. check the box that says "Add the following commands when calling compiler:"
    5. add -Wall in the box (make sure the 'W' is capital)
    6. click OK
    7. hold [CTRL] and press [F9]

    that should recompile your program, but not run it. you'll the warnings then.

    --MORE INFO--

    Dev-C++ is called an Itegrated Development Environment, which is basically everything you need to make/run a program wrapped up in a nice package. for C++, you need at least a text editor, linker, and compiler. the linker and text editor are slightly less important than the compiler. the compiler, for the most part, determines what you can and cannot do. Dev-C++ uses a MingW port of the GCC compiler. this compiler is part of the *NIX kernel, and therefore is not native to windows (hence the MingW port).

    here's what the MAN page for gcc says about -Wall:
    Code:
     -Wall  All  of the above `-W' options combined.  These are
                  all the options which pertain to usage that we rec-
                  ommend  avoiding  and  that  we  believe is easy to
                  avoid, even in conjunction with macros.
    http://www.rt.com/man/gcc.1.html
    Last edited by major_small; 03-29-2005 at 12:23 AM. Reason: clarification of instructions and bolding
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    In VC6, a function with a return type, won't compile unless the function has a return statement. It seems like that should be an error in any compiler.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    thanks, i'll try it major small.


    another thing - find the logic error in this

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
       const int value1 = 0;
       const int value2 = 1;
       const int value3 = 2;
       const int value4 = 3;
       int zzz;
       int x;
       for(int i = 0; i < 5; i++)
       {
          switch(zzz)
          {
                 value1: x = value1; break;
                 value2: x = value2; break;
                 value3: x = value3; break;
                 value4: x = value4; break;
                 default: x = 999;                 //999 = myHead->rand(); //to show it's NOT of values 1-4
          }     
          cout << x << endl;
      }    
      system("PAUSE");	
      return 0;
    }
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    here's what I came up with - I'll post some working code in a sec...
    Code:
    #include <iostream>
    #include <stdlib.h>  //change this to cstdlib
    
    using namespace std;
    
    int main(int argc, char *argv[])  //you can use main() if you don't plan on using
    {                                 //command-line input
       const int value1 = 0;
       const int value2 = 1;
       const int value3 = 2;
       const int value4 = 3;
       int zzz;
       int x;
       for(int i = 0; i < 5; i++)
       {
          switch(zzz)   //where was zzz initialized?
          {
                 value1: x = value1; break; //you forgot 'case' see next three
                 case value2: x = value2; break;
                 case value3: x = value3; break;
                 case value4: x = value4; break;
                 default: x = 999;                 //999 = myHead->rand(); //to show it's NOT of values 1-4
          }
          cout << x << endl;
      }
      system("PAUSE");  //avoid using this.  try cin.get(); instead.
      return 0;
    }
    working:
    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {                                 
       const int value1 = 0;
       const int value2 = 1;
       const int value3 = 2;
       const int value4 = 3;
       int zzz;
       int x;
       
       std::cout<<"Enter 5 integers between 0 and 3:"<<std::endl;
       for(int i = 0; i < 5; i++)
       {
            std::cout<<'>';
            std::cin>>zzz;
            std::cin.ignore();
            
          switch(zzz)   
          {
                case value1: x = value1; break;
                case value2: x = value2; break;
                case value3: x = value3; break;
                case value4: x = value4; break;
                default: x = 999;                 //999 = myHead->rand(); //to show it's NOT of values 1-4
          }
          
          cout << x << endl;
      }
      
      std::cout<<"Press &#091;ENTER&#093; to exit";
      std::cin.get();
      return 0;
    }
    Last edited by major_small; 03-29-2005 at 01:13 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    zzz's not being initialized before being used. And case is missing on the switch statement's stuff.

    -edit-
    bleeeeeehhh

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    ok, i messed up and forgot to initialize 'zzz'. however, if i initliaze zzz = 2, i get the same result. (when i posted this, i knew 'case' was missing....that was the point )

    i knew the answer to my question before you answered. Dev-c++ let me compile this with no error, no warning. this could have taken me days to find (in my real program). luckily, it didn't. if i wrote this intentionally, i would expect the error message "label 'value1' has same name as 'int value1'". in other words, "error: you named a label and variable the same name, idiot". if nothing else, i would hope for some sort of warning saying "goto's and labels are deprecated". (i know they're not exactly deprecated, but i believe i've seen some sort of 'goto' warning before)
    Last edited by misplaced; 03-29-2005 at 01:24 AM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    oh yea, and as far as your 'knit picking' goes, that was automatically created by Dev-C++

    (*doesn't mind knit picking* - just saying....)
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    oh boy... you obvously didn't use -Wall

    C:\Program Files\Dev-Cpp\bin\John\Untitled1.cpp In function `int main(int, char**)':
    21 C:\Program Files\Dev-Cpp\bin\John\Untitled1.cpp [Warning] label `value4' defined but not used
    20 C:\Program Files\Dev-Cpp\bin\John\Untitled1.cpp [Warning] label `value3' defined but not used
    19 C:\Program Files\Dev-Cpp\bin\John\Untitled1.cpp [Warning] label `value2' defined but not used
    18 C:\Program Files\Dev-Cpp\bin\John\Untitled1.cpp [Warning] label `value1' defined but not used


    The best way to find any type of error: don't make it in the first place.
    Last edited by major_small; 03-29-2005 at 01:35 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by major_small


    The best way to find any type of error: don't make it in the first place.

    thanks, O sober one
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  12. #12
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    ........actually....... i just tried using -Wall ......followed your instructions to the T. it didn't work

    Dev-C++ 4.9.9.0
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  13. #13
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by misplaced
    ........actually....... i just tried using -Wall ......followed your instructions to the T. it didn't work

    Dev-C++ 4.9.9.0
    I'll admit I've customized Dev-C++ a little but, but taking away the -Wall is the only thing that makes those warnings dissapear for me... and most of the modificatifications I've made were for OpenGL... another thing: have you updated GCC recently? I remember looking through the updates in the Dev update manager and finding an update to GCC.

    Dev-C++ 4.9.9.1

    edit: uninstalling now and installing 4.9.9.2
    edit2: even with a fresh install, and the only thing changed is -Wall, it works.
    Last edited by major_small; 03-29-2005 at 02:22 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

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