Loop Entire Program?

This is a discussion on Loop Entire Program? within the C Programming forums, part of the General Programming Boards category; Originally Posted by Spidey Also, how is that an infinite loop ? It runs only once on my compiler Right, ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    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?
    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

  2. #17
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,700
    >> 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:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(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,700
    >> 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:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(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,700
    >> 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:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(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,302
    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"

Page 2 of 2 FirstFirst 12
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, 09: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, 05:54 PM

Tags for this Thread


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