How to set up a recall point inside of main

I'm making a newer password prompt, the last one worked great, but I'm just wanting to try something new. SO, it there a command that would do something like:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <windows.h>

int main();
{
    using namespace std

    
    //if tooMany =3 the computer will shutdown.
    int tooMany=1; 
    int b=1234
    int c;

    ////////////
    RETURN POINT HERE!!!
    ////////////

    if (tooMany = 3)
    {
        system("shutdown/p");
    }

    //no else needed



    cout << "\n\t\t\tPlease Enter Password:\n\t\t\t====> ";
    cin >> c;
    
    if (c != b)
    {
        system("cls");
        cout << "\n\t\tSorry, That is not the correct password\n";
        a++;
    }
    else
    {
        system("goodPass.bat");
    }
    //////////////////////////
    RETURN TO RETURN POINT
    //////////////////////////
    return 0;
}
Last edited on
btw the reason I need the return point there is so that
int tooMany
does not get changed back to 1
goto
while

BTW, the code above should be a shell script, not a C++ program
Last edited on
*punches freddie*

+1 to while & Bazzy
Hey Disch!

I dispise goto and can say with perfect honesty that I have never used it in a C or C++ program (I won't mention Basic). However, after examining in close detail the obvious elegance of the newguy's program, including the 470K it compiled to after fixing numerous bugs, I thought it would be THE perfect addition, hence my terse reply!

freddie

PS:

But believe it or not I really am a nice guy, and at this point I feel quite quilty for not reposting the example with a while loop and showing the right way to do it. After all, folks come here in good conscience looking for help, and to just post 'goto' knowing full well its trash is nasty.

On the other hand, it was a complete and correct answer to a direct question.
Last edited on
I found loops. I like them but a <if & then> + <goto> works the same, and for me is 10x easier to read.

It's like system("pause"); It has such a religious war going on around it, but truthfully, it's easier to remember for new people than cin.get(); That only removes the "Press any key to continue....".
That said, I don't see why it is "Wrong". I can understand not as pretty, but honestly it saves me from having to type it out. and if I made a CLI.exe for someone and it just stopped.... They probably wouldn't know that means 'Hit Enter".


The more I look into forums the more and more I see people talking about things being "bad", or "wrong", or "right". But for this guys eyes it's all the same, but different commands...

Also anyone using MS-Visual C++ only has to hit
'CTRL' + 'F5'
and the program will stop before exiting the program, but hitting just
F5
will only run with out stopping for you.
It's more than a stylistic thing. There are real dangers with using goto improperly because they wreck C++ scope rules.

Consider the following:

1
2
3
4
5
6
7
8
9
10
void func()
{
  if(something)
    goto foo;

  string bar;

foo:
  bar = "stuff";
}


The goto skips the bar declaration, and therefore skips the ctor meaning bar is potentially uninitialized at the time of assignment, which will devestate your program.

That's besides all the arguments you've heard about goto producing wretched spaghetti code that is impossible to follow or debug if when logic problems surface.
@Disch:
So, would you consider it okay to use goto as long as you are aware of the possible implications?
I see what you are saying, but if you use loops, and forget a simple growing-value you could get stuck in... Well.. A loop.

So I see both side being (POTENTIALLY) just as buggy. but I'm not arguing with anyone. Especially someone well above my skill-set.

I'm simply saying that both could be dangerous, and for me goto is much easier to watch, read, understand, and correct.

I hope one day to see a need to say "don't / do this every time, cause It's the right way".

(and no offense), but so far all the arguments against certain methods are opinions, or people's personal prefs.

For example:


"don't do this because I don't"
OR
"don't do this, cause it's confusing to me"
OR
"don't do this cause everyone else says so".


even you started off by saying:
It's more than a stylistic thing.


But again, thanks all for help. Sorry if I opened a debate. That wasn't my intention. I just don't (yet) understand why people label things
right
wrong
bad
good

But thanks to all for your help
I see what you are saying, but if you use loops, and forget a simple growing-value you could get stuck in... Well.. A loop.


using goto to simulate a loop doesn't make that any less likely.

Say you want to call 'Foo()' 5 times:

1
2
3
4
5
6
7
8
9
10
11
for(int i = 0; i < 5; ++i)
  Foo();

// vs.

int i = 0;
loop:
Foo();
++i;
if(i < 5)
  goto loop;


You seem to suggest the for loop is more prone to you forgetting the ++i part -- but really, I think you're more likely to forget it with the goto approach.

Nevermind that the goto approach is much more code, much less clear, breaks scope rules, and requires additional identifiers (what if you have more than 1 loop? Do you start naming them loop1, loop2, loop3?)


and for me goto is much easier to watch, read, understand, and correct.


I think it's more that the code is written by you -- not so much that it uses goto. I could show you a tangled mess of gotos that aren't easy for anyone to read.

And while it's possible to have nested loops that aren't easy to read, they're less common -- and they're not "tangled" like gotos are. Loops have a clear begin and an end. Code always moves top to bottom.


I suspect that goto is only easier for you because you're more familiar with it. That will change as you get more experience with loops.

(and no offense), but so far all the arguments against certain methods are opinions, or people's personal prefs.


My previous example of goto potentially skipping a constructor is a fact, not an opinion. That's a very real danger that anyone using goto must be aware of.



I'll respond to the rest when I get home. bbl
I'm simply saying that both could be dangerous, and for me goto is much easier to watch, read, understand, and correct.


No. You can't break a for loop. You can however break your program with a goto.
lol I wasn't challenging you, and you kinda twisted my meanings (don't think on purpose), and still gave opinions....


using goto to simulate a loop doesn't make that any less likely.
True, but doesn't make them more likely either...


You seem to suggest the for loop is more prone to you forgetting the ++i part -- but really, I think you're more likely to forget it with the goto approach.
And I never hinted or said that loops are more error prone, just that both ways have potential

Nevermind that the goto approach is much more code, much less clear, breaks scope rules, and requires additional identifiers (what if you have more than 1 loop? Do you start naming them loop1, loop2, loop3?)


You added 14 more chars... and we still disagree, the code is much more clear to me. and when I name things I don't know for what it does, but why. so without reason for writing a multiple goto code, I couldn't tell you, but If i do I'll get back to you ;)

I think it's more that the code is written by you -- not so much that it uses goto. I could show you a tangled mess of gotos that aren't easy for anyone to read.
anything could be written to be confusing, I make things stand out with comments when this happens. you comments in one line codes though... That is confusing!!! It's an opinion that I think most would agree with.


And while it's possible to have nested loops that aren't easy to read, they're less common -- and they're not "tangled" like gotos are. Loops have a clear begin and an end. Code always moves top to bottom.
but so do goto(s)....
1
2
3
4
checkpoint: //<===start
inner: //<start
goto checkpoint;//<===End of checkpoint
goto inner; //End of inner 


I suspect that goto is only easier for you because you're more familiar with it. That will change as you get more experience with loops.
Maybe you are right, but how does that make it more confusing? For me this is easier, for you that is easier... That has been my whole point.

My previous example of goto potentially skipping a constructor is a fact, not an opinion. That's a very real danger that anyone using goto must be aware of.
how would that error be worse than:

1
2
for(int i = 0; i < 5; ++otherVariable);  // replace otherVariable with a reasonable typo
Foo();


my would bug out... Yours would pull an energizer bunny...

////////////////////////////////////

I make that joke at the end to show that I'm not trying to be a pain, and I appreciate the help and debate.

However, at the time in my programming I don't see you way being anything other than an opinion of a better way to do things.

int main()
{
I could eat a steak with my hands and mouth. but to someone that uses silverware that would be wrong.... Though in actuality it's not wrong, it's just different that your way.

I'm new to these forums, and I value tips and knowledge of other with higher skill sets, but I'll always challenge them if they don't make sense.

Given that I'm new and may one day look back and say "Oh crap I wrong", at this time, I seems more like you are saying, "Because this is easy for me, it's easy for all".

and looking at your code, it's nice and pretty and one line, I just don't like it. the following looks makes more sense to me, but like you said, ATM it's my pref so maybe that is why. I'll keep loops in mind, but I just don't see the point in confusing myself, only to make people stop watching me eat my steak

return 0;

}

;) but if you got tips even though I disagree, I'm all ears. I'm eager to learn this C-L, opinions aside.
Your counter points to loops all apply to goto-simulated loops as well. I don't see why you'd think it'd be different. =P

Anyways...

You added 14 more chars... and we still disagree, the code is much more clear to me.


Then you're not as familiar with C or C++ as you are with other languages. Anyone remotely familiar with C++ will immediately identify what the for loop does at a glance.

The goto simulation, on the other hand will take deciphering.

Now I'm not saying that everyone's like that (you're obviously not), but most people are. It's not personal opinion, it's the life of the language. That is what C++ looks like. If you don't like how it looks, then you don't like how C++ looks.

It's not limited to loops either. Conditional blocks, functions, classes, namespaces.... they all look like that. It's a consistency shared throughout the language. goto flies in the face of that consistency which is why it is considered messy by 99.999% of C++ programmers.

But again I'm getting pulled away, so I'll have to respond later.


And I don't mean to argue or anything. I like these kinds of discussions. =)
If you don't like goto use longjmp ;^)
http://www.cplusplus.com/reference/clibrary/csetjmp/longjmp/
MUCH more readable
Topic archived. No new replies allowed.