Jump to content
  • Advertisement
Sign in to follow this  
Avont29

C++ labels

This topic is 4681 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey, how do you make a label or identifier or whateve rin C++, in darkbasic pro i would do Do text: print "Dope" Loop Do if mouseclick()=1 then goto text Loop ^^^ how do you make labels like text: and goto them, gosub or something

Share this post


Link to post
Share on other sites
Advertisement
In C/C++ labels are identifiers with a colon afterwards. Ex:

int main(int, char **) {
std::string cow("Moo");

infinite:
std::cout << cow;
goto infinite;

return 0;
}


Keep in mind in most cases goto is not necessary. In most cases you can use program control constructs like functions, do, while, for or switch to accomplish what you desire.

Share this post


Link to post
Share on other sites
C++ is a very different language than you are used to. While it does have labels and gotos they are not for beginners. Anyone who teaches them to you would be doing a disservice. I recommend getting at least two years experience with C++ before you start using them in C++. I've been programming in C++ for years now and only once have I had the desire to use goto. Even then I forgot that goto existed so I solved it with the standard looping features of C++! C++ does not need goto. C++ is a far more powerful language and has features that you might not be familiar with. Here are the two most common types of loops:


while( mouseclick() )
{
cout<<"Dope"
}

for(int i=0; i<count; ++i)
{
cout<<i;
}



note that "mouseclick()" is not a standard C++ function, so you will have to use something else. The "while" loop checks the condition and if it is true it does what is inside of the braces. Then it checks again and again until the condition is false. The for loop is kind of complex looking but as long as you make it look like that it will go through count times. Count can be a variable that holds a number, say 10. Then it will go through 10 times. Printing to the screen uses "cout", which stands for "console output"

If you have more questions ask away. Remember that C++ is a very different language. So don't ask specific questions, like how to I make a label. You might get a correct answer but it won't be the best answer and it might not be useful at all. Instead tell us what your goal is so that we can give you the best help possible.
[/source]

Share this post


Link to post
Share on other sites
Arguably however, C++ could use Java-style labelled statements since it makes exiting from deep loops and/or switch statements a lot cleaner, as the following code sequences demonstrate.

In JAVA:

my_label: while(some_condition)
{
while(somethin_else)
{
switch(some_integer)
{
case 0:
//...;
break;

default:
break my_label;
}
}
}



In C++:

while(some_condition)
{
while(somethin_else)
{
switch(some_integer)
{
case 0:
//...;
break;

default:
goto exit_label;
}
}
}exit_label:;



Its only a minor point, but it can affect readability in much larger programs/functions. Also, asthetically, the C++ version just looks a bit shit.


arm.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!