Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C++ Label and goto
01-03-2010, 03:08 AM
Post: #1
C++ Label and goto
This is pretty the same as using labels and goto in Batch file or in Pascal.
You just need to declare where label is and to put goto on a place where you want to jump to the label.
Syntax of label is :
Code:
name_of_the_label:
Syntax of goto :
Code:
goto name_of_the_label;
That is all about syntax that you need to know !
Now let's make a small example :
Code:
# include <iostream>
# include <cstdio>
using namespace std;
int main()
{
    cout<<"This is the first line"<<endl;
    goto mynewlabel;
    cout<<"This is the second line"<<endl;
    mynewlabel:
    cout<<"This is the third line"<<endl;          
    system("pause");
}
As you can see output in this case will be :
Code:
This is the first line
This is the third line
That is because after writing first line it will goto "mynewlabel" and jump over the second line.
Now let's make something that can be useful. For example if you need to enter a number bigger than zero (positive number) and you want to do it with label and goto. Of course that you can do it with while, but this can be used too. So here is the code :
Code:
# include <iostream>
# include <cstdio>

using namespace std;

int main()
{    
    int n;
    mynewlabel:
    cout<<"Enter positive number"<<endl;
    cin>>n;
    if (n<0) //If n is smaller than zero it will jump on the beginning
    goto mynewlabel;    
    cout<<"You have entered a positive number !"<<endl;          
    system("pause");
}
Also if you make some program, let's take a simple example, addition of 2 numbers, and you want to ask if the user want's to try again, you can do it with label and goto. But also you can clear screen by using "system("cls")" function. So it will ask you to enter 2 numbers than it will output addition and it will tell to press Enter to try again, and than it will return on top :
Code:
# include <iostream>
# include <cstdio>

using namespace std;

int main()
{    
    int a,b;
    top:
    cout<<"Enter first number"<<endl;
    cin>>a;
    cout<<"Enter second number"<<endl;
    cin>>b;
    cout<<(a+b)<<endl;
    cout<<"Press Enter to try again !"<<endl;
    cin.get();
    cin.get();
    system("cls");  
    goto top;            
    system("pause");
}
Also to explain what 2 cin.get() functions are for. They are there to simulate reading, or getting line. That is because program would be at the beginning on every calculation so user couldn't see the result and it would ask him to enter first number again. And one cin.get() doesn't work, so you need to use it two times.
Another task would be looping with label and goto. Let's make a program for writing numbers from 1 to n :
Code:
# include <iostream>
# include <cstdio>

using namespace std;

int main()
{    
    int i,n;
    cout<<"Enter n"<<endl;
    cin>>n;
    i=1;
    loop_label:
     cout<<i<<endl;
     i++;
    if (i<=n)
     goto loop_label;            
    system("pause");
}
So value of counter "i" is 1 and it will write it and check if i is smaller of equal to n, and if it is, it will return to loop_label and if it is not, it will abort the process.
I think that is everything taht you need to know about labels and goto.
Hope that you like this small tut !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
01-10-2010, 03:26 PM (This post was last modified: 01-10-2010 03:29 PM by PoZHx.)
Post: #2
RE: C++ Label and goto
(01-03-2010 03:08 AM)drdebcol Wrote:  
Code:
# include <iostream>
# include <cstdio>

using namespace std;

int main()
{    
    int n;
    mynewlabel:
    cout<<"Enter positive number"<<endl;
    cin>>n;
    if (n<0) //If n is smaller than zero it will jump on the beginning
    goto mynewlabel;    
    cout<<"You have entered a positive number !"<<endl;          
    system("pause");
}

Hi,
Few suggestions:
Avoid using GOTO
Endl is not needed just use \n
why are you using an signed number if you want postives only?
unsigned is for postives only
signed is for postive/negatives (if you dont define it will use signed by default)

Here example for you:
Code:
#include <iostream>
using namespace std;
int main()
{    
    short unsigned n;
    while(true)
    {
          cout<<"Enter positive number between 0-65535\n";
          cin>>n;
          if(!cin >> n)
           {
              // Code to add: Clear buffer etc and error handling and output negative number  or invalid input
             continue;
            }
            cout << "You inputted a postive number of:\t" << n << "\n";
            break;

}
       return 0;
}
I know could of used better loop such as do while etc but while is more easier to understand for beginners
Find all posts by this user
Quote this message in a reply
01-10-2010, 03:28 PM
Post: #3
RE: C++ Label and goto
Yea goto statement in large programs make the programs hard to read and maintain.
Visit this user's website Find all posts by this user
Quote this message in a reply
01-10-2010, 10:45 PM
Post: #4
RE: C++ Label and goto
Very good. That was just an example, but i like your reply. Using break and continue is good. And i usually avoid using goto and label, this was just an explanation in case i use it !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: