Thread: Loop Entire Program?

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Spidey
    Also, how is that an infinite loop ?
    It runs only once on my compiler
    Right, it simulates a do while loop with a condition that will be evaluated to false after the first iteration. But why even suggest goto to a beginner?
    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

  2. #17
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> ok, so I simplified it a lot to show what I want, thought it would be easier on everyone

    Alright, well using the example you posted:

    Code:
    int main()
    {
    	float 
    		course, 
    		pi, 
    		output;
    	int 
    		start_over;
    
    	for( ;; )
    	{
    		printf("enter a course to travel in degrees: ");
    		scanf("%f", &course);
    		
    		// calculations here //
    		pi = 3.14;
    		output = course * (pi/180);
    		
    		printf("your course in radians is: %.1f", output);
    		
    		printf("would you like to end program or enter another course\n"
    		"enter 1 for another course, or 2 to end program");
    		scanf("%d" , & start_over);
    		
    		if(start_over!=1)
    			break;
    	}
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #18
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    ok, so I simplified it a lot to show what I want, thought it would be easier on everyone

    int main()
    {
    float course, pi, output;
    int start_over;

    printf("enter a course to travel in degrees: ")
    scanf("%f", course);

    // calculations here //
    pi = 3.14
    output = course * (pi/180);

    printf("your course in radians is: %.1f", output);

    printf("would you like to end program or enter another course\n
    enter 1 for another course, or 2 to end program")
    scanf("%d" , & start_over);

    if(start_over==1)
    // want to start program over here;
    else
    // want to end program here;

    }
    return(0);
    Well, I would suggest you look into the code Sebastiani just posted. Thats pretty much what you wanted. Also,you could consider a simple state machine to control your overall game;
    The basic principle is this -
    Code:
    enum State{
          MENU,
          GAME,
          GAME_OVER,
    
          TOTAL,
    };
    now in main you could do something like this -
    Code:
    State currentState = Menu;
    
    while(true)
    {
        switch(currentState)
      {
    
         case MENU:
        //Show instructions etc
         break;
    
         case GAME:
        //run game code etc
         break;
    
        case GAME_OVER:
       //display info and set the current state back to MENU
        break;
    
       }
    
    }
    Now you can easily manage your entire game using this framework without it getting to messy.

  4. #19
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Quote Originally Posted by laserlight View Post
    Right, it simulates a do while loop with a condition that will be evaluated to false after the first iteration. But why even suggest goto to a beginner?
    Ah, I just thought he/she should know how to use it. GOTO's can come in handy some times. Although point noted, I enjoyed goto a bit too much when I first learned about it.

  5. #20
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Ah, I just thought he/she should know how to use it. GOTO's can come in handy some times. Although point noted, I enjoyed goto a bit too much when I first learned about it.

    Well, I can tell you that I've written well over a million of lines of code in my lifetime and can literally count the number of times I've used goto on one hand!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #21
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Well, I can tell you that I've written well over a million of lines of code in my lifetime and can literally count the number of times I've used goto on one hand!
    Well, as they say, one hand is better than none

    But, I agree. My mistake, shouldn't have shown that example.

  7. #22
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> My mistake, shouldn't have shown that example.

    There's nothing wrong with posting such an example, per se, but knowing the drawbacks of using goto it should be strongly qualified with a "not recommended" disclaimer of some sort.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #23
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    There's nothing wrong with posting such an example, per se, but knowing the drawbacks of using goto it should be strongly qualified with a "not recommended" disclaimer of some sort.
    Yes, I agree. I've had a few bad experiences with goto myself, so I know what its capable of. Especially when your a beginner. But, I'm sure anyone who reads this thread will get the hint that its not recommended anyway. I'll keep that in mind for next time.

  9. #24
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Hell:
    Goto sucks

    I have only one thing to say about goto

    goto Hell;

  10. #25
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Did you see the while loop I added?[/QUOTE]

    no i didnt. thanks man. ill check it out

  11. #26
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Sebastiani View Post
    >> Ah, I just thought he/she should know how to use it. GOTO's can come in handy some times. Although point noted, I enjoyed goto a bit too much when I first learned about it.

    Well, I can tell you that I've written well over a million of lines of code in my lifetime and can literally count the number of times I've used goto on one hand!
    Ditto.

    Everyone should do themselves a favour and try to forget it even exists.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program using structs to calculate grades
    By TampaTrinDM88 in forum C Programming
    Replies: 4
    Last Post: 07-06-2009, 12:33 PM
  2. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  3. Wondering about repeating an entire program
    By Chrisab508 in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2003, 10:22 PM
  4. detect infinite loop in c program
    By abhivyakat in forum C Programming
    Replies: 19
    Last Post: 10-01-2003, 06:55 AM
  5. Need Help on Program Using For Loop
    By Unregistered in forum C++ Programming
    Replies: 10
    Last Post: 02-26-2002, 06:54 PM

Tags for this Thread