-
The goto doesnt work!
After all that other posts that I did, the goto that I tried to do the other day didnt work today, here is the complete code:
------------------------------------------------------------------------------------
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
void main(void)
{
int X;
A:
X=X+1;
if (X==10000000)
goto O; //It says errors are here
char game4;
int game1=rand()%3;
if (game1==1)
{
int game2=rand()%10;
cout<<game2;
goto A;
}
int game3=rand()%8;
if (game3 == 1)
game4 = '!';
if (game3 == 2)
game4 = '@';
if (game3 == 3)
game4 = '#';
if (game3 == 4)
game4 = '$';
if (game3 == 5)
game4 = '%';
if (game3 == 6)
game4 = '^';
if (game3 == 7)
game4 = '*';
cout<<game4;
goto A;
O: //and here
return (0);
}
------------------------------------------------------------------------------------
Some one help me by telling me how to make it jump down the program using goto's please.
-
perhaps try using a complete word for a label and not just the letter O.
-
Ok, ive finally figured it out, I cant have the declaration statements of the variables in between the goto statements, that is, I cant "jump over" declaration statements. Ive finally got it!
-
if (X=10000000)
goto O; //It says errors are here
you are assigning not comparing, put another = in between
x==100000
except for this, your code seems alright.
-
Go to is evil and should be avoided at all costs!
-
yah yah
si seņor... it creates more problems, than it solves.. i don't know where your trying to go by using "GOTO" statements... why not try a different programming language.. like some old' assembly language somewhere... learning "goto" in c++ is going down the wrong road...
-
Im sorry, the power of the "Dark side" of goto has me in its grasp already, I cant get out of it, its too late for me.....
-
>>> I cant get out of it, its too late for me.....
Well there goes any chance of a professional career...
-
What do you mean there goes any chance of a profesional career?
-
he is saying that using the goto command in C++ is a very bad habit, and will only cause problems later.
i would have to agree. in C++ there is absolutely no reason to use goto, since you can merely structure your code to go back and forth exactly like you want it to.
and once you learn how to do that, you will never use goto again.
-
How do you structure the code to switch back and forth without using goto?
-
>>> is a very bad habit
It is a bit more than that I'm afraid. I have never yet worked for a company which did not specifically ban the use of the goto statement in it's house coding standards.
>>> without using goto?
There is no construction that you can do with goto that you cannot do with another language construct. Give an example of what you mean, and someone will show you a better way.
-
Using the other control statements in the language.
Mainly, switch statements, while and do while loops, in addition to if and else clauses.
-
Ok, so how would i change this bit of example code into proper code?
A:
if (thing1==100)
goto B;
game1=3000;
goto A;
B:
thing2=10;
goto A;
-
Man, I havent used goto since 1988 in BASIC on my old Amstrad CPC464.
The memories.....