Hi guys this is my first post here. I have coded a perfectly working program. But my teacher says that you should code it without using single "goto" statement. It's very complex for me like that. Please can anyone of you re-code it without using "goto" even once?
#include <iostream>
usingnamespace std;
int main()
{
cout<<"Hi! Please enter a positive number : ";
A: int a=0;
cin>>a;
B: for ( ; a < 0 ; )
{
cout<<" \nSorry, the number you just entered is not postive. \n \n";
cout<<"Plese re-enter : ";
cin>>a;
} //End of 1st 'for' loop.
for ( ; a == 0 ; )
{
int c1=0;
cout<<" \nAre you sure you want to continue with '0'(zero)? \n \n";
C: cout<<"1 - Yes \n";
cout<<"2 - No \n \n";
cout<<"Enter your choice (1 or 2) : ";
cin>>c1;
if (c1 == 1)
{
goto D;
}
if (c1 == 2)
{
cout<<" \nPlease re-enter the number : ";
cin>>a;
goto B;
}
if (c1 != 1 && c1 != 2)
{
cout<<" \nWrong Choice!! Please select again if you want to continue with '0'(zero). \n \n";
goto C;
}
} //End of 2nd 'for' loop.
for ( ; a>0 ; )
{
D: int c2=0;
cout<<" \nWhat would you like to do with the selected number? \n \n";
E: cout<<"1 - Find its factorial (Warning: The entered number should be less than 13 or \n";
cout<<" \t \t \telse factorial will be calculated wrong!) \n";
cout<<"2 - Find the sum of all numbers upto the entered number \n \n";
cout<<"Enter your choice (1 or 2) : ";
cin>>c2;
if (c2 == 1)
{
if (a == 0)
{
cout<<" \nThe factorial of 0 is : 1 (which is true)";
goto F;
}
int facto=1;
for (int n=1 ; n<=a ; n++)
{
facto=facto*n;
}
cout<<" \nThe factorial of selected number is : ";
cout<<facto;
goto F;
}
if (c2 == 2)
{
if (a == 0)
{
cout<<" \nSum of all numbers upto the entered number is : 0";
goto F;
}
int sum=0;
for (int m=1 ; m<=a ; m++)
{
sum=sum+m;
}
cout<<" \nSum of all numbers upto the entered number is : ";
cout<<sum;
goto F;
}
if (c2 != 1 && c2 != 2)
{
cout<<" \nWrong choice!! Choose again : \n \n";
goto E;
}
} //End of 3rd 'for' loop.
F: int c3=0;
cout<<" \n \nNow choose what to do : \n \n";
G: cout<<"1 - Repeat cycle (program) \n";
cout<<"2 - End program \n \n";
cout<<"Enter your choice (1 or 2) : ";
cin>>c3;
if (c3 == 1)
{
cout<<" \nPlease enter a positive number : ";
goto A;
}
if (c3 == 2)
{
cout<<" \n \t Good Bye and have a nice day. ( Remember : TRUST THE PROGRAMMER!!) \n \n \t \t \t";
}
if (c3 != 1 && c3 != 2)
{
cout<<" \nWrong choice!! Choose again : \n \n";
goto G;
}
return 0;
}
I am expecting that you guys will help me. Waiting for a reply.
Unfortunately, we do expect you to do the thinking.
You do need nested loops. For example, repeating overall is the out-most loop which terminates once c3 becomes 2, but getting a valid c3 before that is an inner loop of its own.
Generally, for statements that need to repeat indefinitely until a certain condition stops being met, a do-white is more suitable. Most of your gotos can be replaced by loops. The one that cannot is F, but another keyword related to loops can be used. Can you guess which?
No, I can't recall any other keyword that can be used for loops. It would be kind enough to tell me that, Albatross. I don't think this program is very tough for you people.
Did you ever learn any statements that allow you to instantly jump out of loops?
Alternatively, you can just set a variable that's part of the condition for your loops to run and your if statements that have goto Fs in them and wait until the loops end naturally.
The only statement that I learned is goto. You can tell me any other that you might know.
Alternatively, you can just set a variable that's part of the condition for your loops to run and your if statements that have goto Fs in them and wait until the loops end naturally.
As for the example, sure. It's a very inelegant example and using one of the statements from the above article is (in my opinion) a cleaner solution, but for the purposes of illustrating what I mean...