Are Branch Labels safe?

Which code is safer to use?

Code 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstdlib>
#include <cstring>
#include "conio.h"
using namespace std;
int main(){
    one:
        system("cls");
        cout << "Press any key to move on...";
        getch();
        goto two;
    two:
        system("cls");
        cout << "Press any key to move on...";
        getch();
        goto one;
}


Code 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cstdlib>
#include "conio.h"
using namespace std;
int main(){
    string quitButton;
    bool done = false;
    while(done==false){
        cout << "Type X to quit\n";
        cin >> quitButton;
        if(quitButton=="X"){
            done = true;
        }
        if(done==true){
            return 0;
        }
    }
}


Which code is safer to use? The one that uses branch labels, or the one which used a while loop?
@xcalibur0645

Definitely the while loop vesrion. Goto's are really bad. To quote vald from moscow: "You shall forget that C++ has the capabilty of goto"
What do you mean by "safe"? I don't think goto makes it any less safe given that you didn't make a mistake. It's not to recommend using though. system("cls"); might be considered unsafe. What if cls is a virus? Both programs have problems with termination. The first one never terminate. The second one fails to terminate if EOF is reached.
Last edited on
What do you mean CLS is a virus?
Topic archived. No new replies allowed.