Look at the code here:
if (pos != -1)
{
goto badsearch;
}
I think you meant:
if (pos == -1)
{
goto badsearch;
}
Another thing is that string::search() returns string::npos if the search function does not find what it was told to find, and not -1, so the code should be:
if (pos == string::npos)
{
goto badsearch;
}
cout << search;
pos = search.find("=");
if (pos != string::npos)
{
cout << "\n" << pos;
}
And one last thing: you can avoid using goto in your 'if' statement by replacing it with the statement continue; - this will jump to the beginning of the while loop:
if (pos == string::npos)
{
continue;
}
unbeatable0
Junior Poster in Training
90 posts since Sep 2008
Reputation Points: 42
Solved Threads: 13
^ nice answer.
except one thing needs a bit stronger emphasis.
never, ever use "GOTO" this is not BASIC programming. You will be tarred and feathered.
(there may be an exception to the absolute ban on using GOTO, but that's not available until you reach Level 70.)
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
>except one thing needs a bit stronger emphasis.
>never, ever use "GOTO" this is not BASIC programming.
Note to the OP: Avoid listening to programming advice that says you absolutely must or must not do something.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Avoid listening to programming advice that says you absolutely must or must not do something.
Good metaprogramming advice ;)Never forget to declare C++ variables before...
To ignore or not to ignore? ;)
Is it an advice or a requirement? Where is the border?...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
>Where is the border?...
Common sense, of course. Note that I said "avoid listening to" and not "never listen to". ;) The whole reason we have this "goto is teh EBIL!!!!!" crap is because a bunch of people were mindlessly following orders rather than thinking for themselves.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>A. Whats wrong with goto
Nothing. The problem is people using it in an undisciplined way such that the code becomes an unruly mess where control flow is difficult to follow.
>B. still dosn't awnser my origonal question
Be patient. When someone is ready to give you an answer, you'll get it. I notice you didn't even bother to thank unbeatable for taking the time to help you. Did you make the suggested changes?
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
"goto is teh EBIL!!!!!" crap is because a bunch of people were mindlessly following orders rather than thinking for themselves.
goto is teh ebil, because beginners mindlessly use it as a crutch to compensate for poorly-planned designs, and this perpetuates on into their professional work, where they cleverly develop a tangled mess of unreadable code that they only understand
then they finally realize that it's really crappy design, so they rationalize it as "job security" (with a wink wink nudge nudge),which is all funny and collegial, until they finally quit and leave the mess to be sorted out by someone else.
GOTO should be discouraged with extreme prejudice in any sort of forum that is dedicated to teaching beginners the basics of C language.
if an experienced programmer chooses to use a sparingly few well-positioned GOTOS in, for instance, some error handling routines of hardware drivers, then that's their business.
but beginners should be taught how to complete their pedantic CSC 101 programs using traditional C-language loop structures.
.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
I can certainly agree with well justified recommendations. But banning a feature just because it has the potential for abuse is hardly the way to educate anyone. And "discourage with extreme prejudice" does indeed amount to banning, if my experience is any indicator.
From what I've seen, the presence of goto in anyone's code always results in some know-it-all saying "never use goto!". I've even had people attempt to chastise me for using it both here and on other forums. You can imagine how well that goes. :twisted:
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
i have had to debug 100K lines of production code littered with GLOBALS and GOTO statements, after it had been hacked to death by every stripe of self-taught jackleg programmer.
i do not pretend to lecture experienced developers using a few sparing GOTOs. Such as yourself: you are obviously a very experienced programmer who understands the concept of writing maintainable code. Even more than me.
But I have a serious aversion to any beginner asking how to code their "hello world!" program and using GOTOs and GLOBALS to do it.
GOTOS and GLOBALS taught at the beginner's level inevitably leads to years of shiite programming that eventually has to either be continually maintained at high cost of time and labor, or just tossed out and rewritten by someone else.
i, personally, am tired of being the" someone else" who seems to keep inheriting this crap.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
There's nothing wrong with using the goto-statement, but overwhelming use of it makes your code very difficult to understand and extremely hard to debug, so my advice is: avoid it as much as you can and use it only when there's no other possibility left :) ...
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243