Starting a function all over?

This is a discussion on Starting a function all over? within the C++ Programming forums, part of the General Programming Boards category; Hello there.. I need some creative help Problem is this: Got a function which needs to check for multiple conditions. ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    4

    Starting a function all over?

    Hello there..

    I need some creative help

    Problem is this:

    Got a function which needs to check for multiple conditions. After one condition has been checked it has to start the function all over again. This should be done numerous times untill all conditions have been met.

    I've been thinking of several solutions to this, but they all seem to have flaws:

    * Re-function call inside function: Creates overflow after a certain number of run-thews, since the old instance of the function is still in the memory.

    * Goto start of function: Well, works the best, but everyone who saw the code would kill me - so this is a no-no due to personal preference.

    * 2x Do/while around all code and use break to exit the nested scope, and start the nested do/while all over: I wouldn't be able to use do/while's or while's inside the code that should check conditions, due to breaking out only 1 scope.

    Anyone has any ideas?

    Example of code would be like:

    Code:
    main()
    {
       if (condition1)
          {//code}
    
       if (condition2)
          {//code}
    
       ......
    
       //go back to start of main()
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    After one condition has been checked it has to start the function all over again. This should be done numerous times untill all conditions have been met.
    What do you mean? It really looks like you are making it more difficult than it should be.

    For example, your example code could be written as:
    Code:
    int main()
    {
        for (;;)
        {
            if (condition1)
                {//code}
    
            if (condition2)
                {//code}
    
            ......
    
           //go back to start of main()
        }
    }
    Last edited by laserlight; 11-23-2006 at 11:52 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Just put the code as a separate function (not main()) and put an infinite loop around it, like while(true).


    Then just return from the function when all conditions are met.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Also, to be picky (it's in laserlight's post), main should really have a type:
    Code:
    int main ( void )
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    recursion?

    Quote Originally Posted by http://www.cprogramming.com/cgi-bin/quiz.cgi?parse1
    Code:
    int fn(int v)
    {
    if(v==1 || v==0)
    return 1;
    if(v%2==0)
    return fn(v/2)+2;
    else
    return fn(v-1)+3;
    }

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Also, to be picky (it's in laserlight's post), main should really have a type: int main ( void )
    This is the C++ forum. laserlight's post was correct. The void inside the parentheses does nothing and is not required in C++.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    what about int argc, char argv[]?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    That is optional for the programmer. There are two types of declarations that must be allowed by the compiler. The compiler can allow other declarations as longs they specify a return value of int.

    http://www.research.att.com/~bs/bs_faq2.html#void-main

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    In fact, while valid, the only reason T function( void ) is allowed is because of backward compatibility.

    Or so I seem to recall reading some place... don't trust me too much on this one
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I was trying to point out the int part, I just put in ( void ) out of habit.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376
    int main ( int argc, char *argv[] )
    int main ()

    [...]

    The second option is used when you do not require access to the command line arguments, and is equivalent to the int main(void) option used by C99.
    There's no mention of (void) being deprecated, but again, don't quote me on this.

    [edit] Another source: http://publib.boulder.ibm.com/infoce...c07funcdef.htm
    (C) If the function does not take any parameters, use the keyword void rather than an empty parameter list to indicate that the function is not passed any arguments. In C, a function with an empty parameter list signifies a function that takes an unknown number of parameters; in C++, it means it takes no parameters.
    [/edit]

    [edit=2] Another source: http://www.icce.rug.nl/documents/cpl...lusplus02.html
    2.5.6: The `void' parameter list

    Within C, a function prototype with an empty parameter list, such as
    void func();
    means that the argument list of the declared function is not prototyped: the compiler will not warn against improper argument usage. In C, to declare a function having no arguments, the keyword void is used:
    void func(void);
    As C++ enforces strict type checking, an empty parameter list indicates the absence of any parameter. The keyword void can thus be omitted: in C++ the above two function declarations are equivalent.
    [/edit]

    [edit=3] More evidence: http://groups.google.ca/group/comp.l...762d7b4fcae853
    > (void) is depreciated in c++, it just means no arguments
    > expected.


    Is it deprecated? I found no mention of that in the Standard. 8.3.5 states
    "The parameter list (void) is equivalent to the empty parameter list." No
    mention of deprecation. Annex D (the list of deprecated features) does not
    mention it, either.
    Perhaps you are thinking of the change in C, where "int f();" which used to
    mean "an unknown number of arguments" is now obsolete (as outlined in Annex C
    in the C++ Standard - hey, how appropriate; Annex "C" is for "C"
    compatibility - was that deliberate I wonder :-)
    [/edit]

    [edit=4] It seems that the void parameter list isn't deprecated in C++. (You can quote me on that, because of the word "seems". ) [/edit]
    Last edited by dwks; 11-24-2006 at 04:36 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    What I meant was exactly because of this:

    > In C, a function with an empty parameter list signifies a function that takes an unknown number of parameters; in C++, it means it takes no parameters.

    I didn't say deprecated.

    My doubt was, wouldn't C fail to compile with int main()?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    gcc will let you compile int main().
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    And yet the C99 says it isn't allowed.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Mario F.
    And yet the C99 says it isn't allowed.
    Hm?
    http://david.tribble.com/text/cdiffs.htm#C99-empty-parm
    One more thing: declaration or definition?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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