• Welcome to Valhalla Legends Archive.
 

Is there a GoTo function, like in visual Basic?

Started by Dyndrilliac, January 09, 2004, 01:07 PM

Previous topic - Next topic

Dyndrilliac

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*
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

Kp

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...
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

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...


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

iago

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.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


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.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

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.