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?Originally Posted by Spidey
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?Originally Posted by Spidey
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
>> 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; }
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;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);
The basic principle is this -
now in main you could do something like this -Code:enum State{ MENU, GAME, GAME_OVER, TOTAL, };
Now you can easily manage your entire game using this framework without it getting to messy.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; } }
>> 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; }
Well, as they say, one hand is better than noneWell, 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!
But, I agree. My mistake, shouldn't have shown that example.
>> 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; }
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.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.
Hell:
Goto sucks
I have only one thing to say about goto
goto Hell;
Did you see the while loop I added?[/QUOTE]
no i didnt. thanks man. ill check it out
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"