what can u use instead of goto and why is this a bad way to
write your codes
what can u use instead of goto and why is this a bad way to
write your codes
It is not liked by many because people think it creates sloppy code.
It can be prevented by making the code a function or using loops. It matters how you are using it.
somthing like this
cout<<"what's your answer";
int answer;
cin>>answer;
//if the answer is more than 10 answer again
if(answer>10)
//.........what now
>what can u use instead of goto
You can usually avoid goto with a simple change in program flow or an extra variable, it's rarely hard.
>and why is this a bad way to write your codes
It's not a bad way to write code, but goto is easily abused. It's that abuse that everyone fears.
-Prelude
My best code is written with the delete key.
I was advised that goto's are only useful for efficiently escaping deeply nested loops. But good programming shouldnt require more than about 4 levels of nesting (that'll probaby start an argument!) and I have still never used a goto statement
s how should i write the code above ^^^
I don't know if it's the best way but instead of goto i use a while statement that continues and restarts until you make the while statement false and then it moves on.
int main(){
int 1st, 2nd;
int 1st = 1;
int 2nd = 1;
while(1st==2nd)
{
//your code
int 2nd = 2; //Set this to one and it will continue to the code again
}
return 0;
}
~Silencer~
What is this "C.O.D.E." you speak of?
change it around so it all goes inside a while loop:Code:cout<<"what's your answer"; int answer; cin>>answer; //if the answer is more than 10 answer again if(answer>10) //.........what now
if you wanted to warn after a bad answer:Code:int answer=99999; while (answer>10) { cin >> answer; }
Code:int answer; cin >> answer; while (answer>10) { cout << endl << "sorry, try again" << endl; cin >> answer; }
yes but i see u used cin>> one more time but that's exactly what i dont want to do
while (answer>10) {
cout << endl << "sorry, try again" << endl;
cin >> answer; /// see what i mean!
}
duno why u dont want cin>> again but try this...
int answer = 0;
cout << "Answer: ";
do
{
cin>>answer;
if(answer>10)
{
cout << "ERROR! " << endl;
}
}while(answer<10);
should work
i've never heard of goto, how do you use it? i may need to use it someday in like someone said a REALLY nested loop...or should i never learn it? i say knowledge is power
Paro
Goto is pretty self explanatory, it makes the program goto a certain spot in the code.
Code:int dummy; cin>>dummy; if (dummy==7) goto LABEL1; cout<<"you did not enter 7"; return 0; LABEL1: cout<<"you entered 7"; return 0;
it seems to be VERY helpful! why shouldnt i use it? it looks like it is the best idea since sliced bread!!!
SOMEONE TELL ME WHY I CANT USE IT OR I WILL!!!
Paro
No one is saying that you cannot use it, but, it is just not liked by programmers. It creates very hard to read code because you have to look all over the place.
ok, i think i will use it then, because it makes it easier for me
Paro