-11

goto is useful FOR BEGINNERS but not recommended !!!!

I am editing my post as I got the right answer on this website.

In this program where user enters subject marks, (after entering marks)he/she is asked whether he/she wants to enter more marks of subjects after declaring in the initial stage that of how many subjects he wants to enter numbers of and if he/she replies Y then program asks him to enter marks again. Look I am a university student of 1st semester and I found goto easier to make my program go to initial stage of program after using so many loops. All I need is to not use goto but use another loop so how can I do this (problem solved by eerorika who answered me).

#include<iostream>
using namespace std;
int main (){
int subjec;
retran:


cout<<"please enter number of  subjects : " ;
cin>>subjec;

int marks[subjec];
for ( int u=0;u<subjec;u++){

cout<<"enter marks of subject "<< u+1 << "  ";
cin>>marks[u];

}

char q='Y';
cout<<"do you want TO ENTER MORE MARKS : "<<endl;

cout<<"enter \"Y\" for Yes and \"N\" or any other character for No :       ";          
cin>>q;
while (q=='Y')
goto retran;
return 0;

Here is a request if you can tell me how I can go to initial stage of program again when user press Y without using goto statement.

  • 8
    Yelling will not help you. – François Andrieux Oct 11 '19 at 19:08
  • 4
    You already have a `while` loop to take care of that for you. Just move it up, wrap your code in it and get ride of the `goto`. Sure, `goto` might havea few defensible uses and can be powerful in some cases. But this is one of the worst instances for a `goto` you could come up with. It's literally reimplemeting a `while` loop using a `goto` *and* a `while`. – François Andrieux Oct 11 '19 at 19:10
  • 1
    `goto` should not be used for loops. We have loops for that. What `goto` is useful for is if you need to break out of a nested loop to some other point. It gets very convoluted to do that if you don't use `goto`. Here a `do while` loop would have done exactly what you wanted. – NathanOliver Oct 11 '19 at 19:10
  • 1
    `goto` can be useful. I don't think anyone denies that. It can also be hard to get right, harder to prove that you did get it right, and harder still to prove to others that it was justified. Usually `goto` can be replaced with loops and calls to functions. – user4581301 Oct 11 '19 at 19:14
  • of course `goto` can be useful, otherwise it would not exist in the first place. I suspect you heard that `goto` should not be used and to be honsest I think you completely missed the point of the story. `goto` is the killer for readability and makes it hard up to impossible to reason about code. Long time ago it was realized that almost any use of `goto` can be avoided without any of these downsides – 463035818_is_not_an_ai Oct 11 '19 at 19:15
  • 1
    I'm (supposed to be ;-) ) in charge of a multi-million line code base. We don't have a single `goto`. But I haven't banned its use (that would be working against the *spirit* of a language): it's just that there has always proven to be a better way. By the way, your code snippet proves nothing! – Bathsheba Oct 11 '19 at 20:03

1 Answers1

7

GOTO statements are useful ?

Yes. But not for this use case.

how I can go to initial stage of program again when user press Y

There's a control flow structure for going back and repeating. It is called a loop. An example:

do {
    // do some stuff

    cin>>q;
} while(q=='Y');
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 2
    Note that `char q;` must be defined outside the body of the loop for it to be visible to `while(q=='Y')`. If you define `q` inside the body of the loop, its scope ends before it can be tested. The compiler will catch this for you, fortunately, but the error message that results can be puzzling to the new programmer. – user4581301 Oct 11 '19 at 19:23
  • THANK YOU eerorika SO MUCH FOR THIS TIP IT SOLVED MY PROBLEM .THANK YOU SO MUCH THNKS TO STACKOVERFLOW FOR THIS PRECIOUS PLATFORM – Killer Saver Army Oct 12 '19 at 19:38