Author | Message | Time |
---|---|---|
Dyndrilliac | I need to use something like the "GoTo" function like in visual basic. The way i want it to work is: [code]int a; int b; if (a=b) { cout<<"Correct, you may continue."<<endl; *Continue* } else { cout<<"Wrong, try again!"<<endl; cout<<"Please enter value: "; cin>>b; *Goes Back to Start Of If Statement*[/code] | January 9, 2004, 7:07 PM |
Kp | [quote author=Dyndrilliac link=board=30;threadid=4664;start=0#msg39031 date=1073675228] I need to use something like the "GoTo" function like in visual basic. The way i want it to work is: [code]int a; int b; if (a=b) { cout<<"Correct, you may continue."<<endl; *Continue* } else { cout<<"Wrong, try again!"<<endl; cout<<"Please enter value: "; cin>>b; *Goes Back to Start Of If Statement*[/code] [/quote] You're thinking of a while loop. Perhaps you should read through a basici C++ book? This kind of thing should've been in there... | January 9, 2004, 7:43 PM |
MoNksBaNe_Agahnim | there IS a goto function although it can be really annoying and buggy, I just use subfunctions...it is a C function and it would look like this... [code] int main() { int a; int b=1; menu: cout << "Input 1 now: "; cin >> a; if(a == b) { cout << "yay you got it right!" << endl; } else { goto error; // if the user does not input 1 then it will go to error: } error: // where goto sends the user to if the answer is wrong cout << "I'm sorry your input the wrong number!" << endl; cout << "Lets try again!" << endl << endl; goto menu: // brings you back to were menu: is return 0; } [/code] Not sure if this is what you wanted, but yes there is a goto function in c/c++, although i find using switch cases that load subfunctions easier and more reliable | January 9, 2004, 9:12 PM |
iago | [quote author=MoNksBaNe_Agahnim link=board=30;threadid=4664;start=0#msg39045 date=1073682758] there IS a goto function although it can be really annoying and buggy, I just use subfunctions...it is a C function and it would look like this... [code] int main() { int a; int b=1; menu: cout << "Input 1 now: "; cin >> a; if(a == b) { cout << "yay you got it right!" << endl; } else { goto error; // if the user does not input 1 then it will go to error: } error: // where goto sends the user to if the answer is wrong cout << "I'm sorry your input the wrong number!" << endl; cout << "Lets try again!" << endl << endl; goto menu: // brings you back to were menu: is return 0; } [/code] Not sure if this is what you wanted, but yes there is a goto function in c/c++, although i find using switch cases that load subfunctions easier and more reliable [/quote] I see what you mean about being buggy. In that program, if you give the right number, it prints the good message, the error message, and loops again. | January 9, 2004, 10:05 PM |
Kp | Also, he used 'goto menu:' instead of goto menu;' (colon instead of semicolon). The goto statement in itself is quite stable and behaves exactly as you would want on any modern compiler. The problem is lots of people tend to use it in situations where other constructions are more appropriate (or they use it and later make a change that works poorly with the goto, for instance moving an assignment to lie between the goto and the label it goes to), and then wonder why their code behaves incorrectly. | January 9, 2004, 10:42 PM |
Adron | Gotos typically also make the code harder to read - with structured loop constructs like for, do or while, you know what to expect. With gotos, you have to study the code much more carefully to understand exactly what it's doing. | January 9, 2004, 11:56 PM |