Search code examples
c++goto

How to go to initial stage of Program without using "goto" and using "do while" loop?


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.


Solution

  • 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');