Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: Dyndrilliac on January 09, 2004, 01:07 PM

Title: Is there a GoTo function, like in visual Basic?
Post by: Dyndrilliac on January 09, 2004, 01:07 PM
I need to use something like the "GoTo" function like in visual basic. The way i want it to work is:

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*
Title: Re:Is there a GoTo function, like in visual Basic?
Post by: Kp on January 09, 2004, 01:43 PM
Quote from: Dyndrilliac on January 09, 2004, 01:07 PM
I need to use something like the "GoTo" function like in visual basic. The way i want it to work is:

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*


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...
Title: Re:Is there a GoTo function, like in visual Basic?
Post by: MoNksBaNe_Agahnim on January 09, 2004, 03:12 PM
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...


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;

}


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
Title: Re:Is there a GoTo function, like in visual Basic?
Post by: iago on January 09, 2004, 04:05 PM
Quote from: MoNksBaNe_Agahnim on January 09, 2004, 03:12 PM
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...


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;

}


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


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.
Title: Re:Is there a GoTo function, like in visual Basic?
Post by: Kp on January 09, 2004, 04:42 PM
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.
Title: Re:Is there a GoTo function, like in visual Basic?
Post by: Adron on January 09, 2004, 05:56 PM
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.