Returning to main.

This is a discussion on Returning to main. within the C++ Programming forums, part of the General Programming Boards category; Im trying to add a code so that when itdoes somthing it will return back to the start of int ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    5

    Returning to main.

    Im trying to add a code so that when itdoes somthing it will return back to the start of int main. Im not sure though what the code is.

  2. #2
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Code:
    int main ()
    {
      while (1) {
        bool doesSomething = 0;
    
      //code here
    
        if (doesSomething == 1) {
          continue;
        }
    
      //code here
    
      }
    }
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    5
    Quote Originally Posted by Dae
    Code:
    int main ()
    {
      while (1) {
        bool doesSomething = 0;
    
      //code here
    
        if (doesSomething == 1) {
          continue;
        }
    
      //code here
    
      }
    }
    ya i knew someone was going to put that code. after you write alot down in the source code its hard to make soething with while. Like I need it so that Part of the code at the buttom is goinng to redirect you back to the int main. While wont work because the code connects to other stuff.Ill try it but i doubt it. Like what i mean is. If x dosent equal to y then return to int main. I cant use While because it uses other parts of code.

  4. #4
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    I see what you mean, so 1) the variable being checked would have to be global, and 2) you'd need the check for that relationship being constant throughout the program. Well.. I know in win32 you can have a while statement that constantly checks for messages, and if one shows up no matter where you are in your program it will process, but I'm not sure how to get that effect in normal c++. I assume you could have that effect in OpenGL, as it uses a library for the OS (like win32). Hmm.. wonder if there is a way to do it in normal c++, or the reason you can do it in win32 is because you're taking advantage of the OS.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    5
    ill see what i can do.

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You could use execptions. Something like this:
    Code:
    #include <iostream>
    
    int something_wrong = 0;
    void do_it() {
       something_wrong++;
       if ( something_wrong < 10 )
         throw( 1);
    }
    
    int main() {
          std::cout << "starting program." << std::endl;
    start:
       try {
          do_it();
       }
       catch (int i) {
          std::cout << "starting again " << something_wrong << std::endl;
          goto start;
       }
    }
    Kurt

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    5
    I decided to swtich the code. Around so that i can use a while statment. It might take me a couple of hours though

  8. #8
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Deltahawk12
    I decided to swtich the code. Around so that i can use a while statment. It might take me a couple of hours though
    Why would it take that long? put the while part at the beginning of code, and } at the end. Anyway it doesnt do exactly what you wanted remember.

    Exceptions would give you more detailed ability (like having many while statements) to return to the beginning of main 'if something is equal to something2', but still not a constant check, and it costs you having to put all the catch and try's in your code. .
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Dae, I don't know what you mean by constant check, but throwing an exception will unwind the stack until the exception is caught or, if it isn't, the program is terminated.

    Deltahawk12, your decision to rework the code to be more compatible with normal control constructs (such as loops) is a wise one. When exceptions are needed report non-exceptional conditions and goto statements are needed*, it often means that you have a flawed design.

    Often, rewriting/redesigning code you have written is a good exercise, and despite the annoyance, it is a good way to learn.

    *There are cases where they are necessary and/or good to use. These cases are fairly rare.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    Maybe I'm reading what he's asking for wrong, but it looks like all he's asking for is something like if(something) goto start;

    So you would have
    Code:
    int main()
    {
        start:
            //Do all your code here
        if(something) goto start;
        return 0;
    Of course, everyone hates goto, so try to avoid it.
    Last edited by Dweia; 09-10-2005 at 05:44 PM.

  11. #11
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Zach L.
    Dae, I don't know what you mean by constant check, but throwing an exception will unwind the stack until the exception is caught or, if it isn't, the program is terminated.
    I mean like is it possible to have something checking if the variable1 == variable2 at all times, and not just when the statement ends?

    Code:
    int variable1;
    int variable2;
    
    int main {
      while (variable2 != variable1) {
        cout << "test1";
    
        variable2 = variable1;
    
        cout << "test2";
    
      } //finally finds out variable2 is == variable1
    }
    The while statement will continue processing till the end of the statement before checking if variable2 != variable1, and therefor ends up printing "test2" as well. The same thing goes for the exceptions, if you included a large amount of code in the exceptions then the exception wont occur until the end of the statement, and therefor almost quite useless (although it does provide means to use the exceptions in other functions than main, but then again you could just use a while statement in functions other than main, and so it would be the same effect). Of course I dont know much about exceptions but testing the posted one above thats what resulted.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You can achieve the "continual testing" by means of encapsulation. If you have a class (perhaps only a wrapper class) with an overloaded operator=, for instance, then you can 'continually check'. I don't know much about Windows' messaging system, but I cannot think of anyway to do it without multi-threading (and then, it is only 'simulated' parallelism unless you actually have multiple processors). This requires a lot more design, and a lot more programming, though (and there is no standard way of doing it).

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  2. help on main()
    By braddy in forum C Programming
    Replies: 9
    Last Post: 03-21-2008, 11:16 AM
  3. Defining main function!
    By alvifarooq in forum C++ Programming
    Replies: 8
    Last Post: 09-19-2004, 02:00 PM
  4. what to do with the main window
    By stallion in forum Windows Programming
    Replies: 2
    Last Post: 01-28-2003, 07:58 PM
  5. void main
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 05-29-2002, 07:08 PM

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