Can I ask on how to use the goto statement because here in my program I am using Function and inorder to go back from the top I use goto statement.. Why isn't it working??

#include<iostream>

using namespace std;

void rankE(){
    cout<<"Rank: E "<<endl<<endl;
}
void rankD(){
    cout<<"Rank: D "<<endl<<endl;
}
void rankC(){
    cout<<"Rank: C "<<endl<<endl;
}
void rankB(){
    cout<<"Rank: B "<<endl<<endl;
}
void rankA(){
    cout<<"Rank: A "<<endl<<endl;
}
void bugo(){
    cout<<"Bugo kaayu ka!! HAHA "<<endl<<endl;
}
void genius(){
    cout<<"Char!!subraan ra sad ka's ka bright !! ABNO ka na !! hakhak'... "<<endl<<endl;
}

int main(){
    int grade; 
    char goBack,y,n;

back:

    cout<<"Enter Grade:";
    cin>>grade;

        if(grade==50|grade==51|grade==52|grade==53|grade==54|grade==55|grade==56|grade==57|grade==58|grade==59|grade==60){
            rankE();
        }

        if(grade==61|grade==62|grade==63|grade==64|grade==65|grade==66|grade==67|grade==68|grade==69|grade==70){
            rankD();
        }

        if (grade==71|grade==72|grade==73|grade==74|grade==75|grade==76|grade==77|grade==78|grade==79|grade==80){
            rankC();    
        }
        if(grade==81|grade==82|grade==83|grade==84|grade==85|grade==86|grade==87|grade==88|grade==89|grade==90){
            rankB();
        }
        if(grade==91|grade==92|grade==93|grade==94|grade==95|grade==96|grade==97|grade==98|grade==99|grade==100){
            rankA();
        }
        if(grade<50){
            bugo();
        }
        if(grade>100){
            genius();
        }

        cout<<"Do you want to go back (y/n)??";
        cin>>goBack;

        if(goBack==y){
            goto back;
        }
        if(goBack==n){
            cout<<"Sayonara !! ^_^ ";
        }


}

Recommended Answers

All 9 Replies

Line 63 should read if(goBack=='y'){ y is a char variable(undefined afais) which is compared with the input of the goBack char variable. 'y' is a char litteral.
The use of goto is not advised. It is not because it exsist you should use it.
Your if statement could use improvement Line 36 (and following): just test grade is between(and including) 50 and 60.
Suppose you had to test a grade between 0 and 1000? What would you do?

Thank you so much for that advice... Okey I'll try to improve it..

You seem to have the right spirit. Continue learning and happy programming! :)

If you don't mind me asking, did your instructor actually teach you to use goto, and more importantly, were you actually directed to use it? I would find it deeply concerning if a professor were to even mention goto in an introductory course. Actually teaching its use is simply unacceptable.

The main problem with goto is that it is unstructured; it can be used (and abused) to create an incomprehensible flow of control if it isn't used with great care. The goto statement is more powerful than is generally needed for flow control, capable of creating labyrinthine programs often called spaghetti code because of the twists and turns it takes.

It also interferes with the ability of the compiler to produce optimized code; because it is harder for the compiler to analyze, it means that many optimization techniques cannot be applied.

More importantly, it is simply unnecessary for ordinary flow control - more structured alternatives such as while() and for() combined with restricted forms of goto such as continue or break can express almost all flow processes needed even more effectively. Most newer languages don't even provide goto statements because of the potential for misuse, and it is a once-in-a-career event for a C/C++ programmer to find themselves in a position where they would want to use it, something you would do only if all of the alternatives were worse. There are some exceptions, where goto makes the code easier to read - certain kinds of state machines, for example - but most of those cases are ones where you are generating the functions using it rather than writing them by hand.

Let me be as blunt as possible: any programming instructor that teaches the use of goto in an introductory C++ course should not be teaching programming at all. The risks of using goto were well established by 1970; novice programmers should be discouraged with all possible force from using it, not taught it. If your professor is teaching goto as a normal practice, your best recourse is to petition the administration to remove them from their position.

Okey! Thank you for that information. Actually I am just exploring it in google and I find it very uncomplicated I actually thought that its good for a program and now as what you've said. I learned that its not good to use it. Actually our Instructor does not even mention it in our class and I asked him why he did not inform us to use such statement and he told me that most of the good programs does not use goto statement. Now I think I learned my mistake. Thank you so much...

he told me that most of the good programs does not use goto statement

That's not necessarily true. There's a lot of (well deserved) hate for goto, but it's not inherently broken or bad. The problem is that a lot of poorly written code has abused it in the last half decade.

There are certainly good reasons to use goto, but if you find yourself simulating a comparable control flow structure such as the various loops available, it's probably not a good use unless there's a measurable benefit.

One good rule of thumb is that if you're using goto to jump upward, it's wrong. It's go-to, not come-from, after all. ;)

There's a lot of (well deserved) hate for goto, but it's not inherently broken or bad.

True; there are places where it is justified, and where it makes sense to use it. However, such cases are few and far between. More importantly, it takes some experience to know when it is reasonable to use it, which is why novice coders are generally steered away from it. The goto statement is very powerful, more powerful than is usually necessary, and that power takes discipline to use effectively.

However, such cases are few and far between.

Yes. I'm not advocating goto, simply adding some perspective. ;)

The goto statement is very powerful

I agree and disagree at the same time. People describe it like a jmp instruction, but goto is relatively quite restricted (even more so in C99+). The key idea is that one should use the feature needed and nothing more. In the case of goto there are more restricted constructs that do the job just as well, often more clearly, and save you the hassle of being yelled at by elitist snobs[1]. ;)

[1] One of my bigger flame attacks was when said snobs blasted me for using goto in a legitimate way and I schooled them on why I used it and why it was the best choice for the given problem. Ah, memories. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.