Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is m code where i want to re-call main function in tabletfind(). what can i do for this?

What I have tried:

C++
#include <iostream>
#include <string>
using namespace std;


int tabletfind(int x){
	if(x%2==0)
	{
		cout<<"Searching for a solution within 21 moves..."<<endl;
		cout<<"No solution found within 21 moves. Sorry."<<endl;
		string ans;
	cout<<"Would you like to try again [Y/N]?";
      cin>>ans;
     if(ans=="y" || ans=="Y"){

    //here i need to call main function part again what should i do? i can't understand
      }
	  	else if (ans=="n" || ans=="N"){
		
            exit;}
           }
}

}
	int main() {
	int number;
	
	tryagain:
	cout<<"How many tablets are you starting with ? ";
	cin>>number;
	 
	if(number>0 && number<=10000){
	 tabletfind(number);
    }
        
else   {cout<<"Number of Tablets must be greater than 0 and no more than 10000"<<endl;
	              goto tryagain;}
    	return 0;
}
Posted
Updated 19-Apr-17 9:49am
v3

Quote:
here is m code where i want to re-call main function in tabletfind(). what can i do for this?

A C program never call the main function, no matter what.
You have to organize your another way.

You should think about using a loop, anf avoid the goto.
 
Share this answer
 
v2
Organise your program so that main() calls another function which you can then call again from anywhere else e.g.
C++
#include <iostream>
#include <string>
using namespace std;

int aFunction()
{
	int number;

	tryagain:
	cout<<"How many tablets are you starting with ? ";
	cin>>number;

	if(number>0 && number<=10000)
	{
		tabletfind(number);
	}
	else {cout<<"Number of Tablets must be greater than 0 and no more than 10000"<<endl;
	goto tryagain;
	}
	return 0;
}

int tabletfind(int x)
{
	if(x%2==0)
	{
		cout<<"Searching for a solution within 21 moves..."<<endl;
		cout<<"No solution found within 21 moves. Sorry."<<endl;
		string ans;
		cout<<"Would you like to try again [Y/N]?";
		cin>>ans;
		if(ans=="y" || ans=="Y")
		{

		        x = AFunction()		}
		else if (ans=="n" || ans=="N")
		{
			exit;
		}
	}
 }
 
 int main() 
 {
	int number;
	number = aFunction();
 } 

Please note I have not tested any of the above and I may have let a tear fall from my eye as I copied the goto.
 
Share this answer
 
Comments
Rick York 19-Apr-17 16:17pm    
The code for the default windows procedure in v3.1 was published in a book many years ago and there was a goto in it. The target label was IcantBelieveIactuallyUsedAgoto. True Story. :)
CHill60 20-Apr-17 3:44am    
:laugh: That made me feel better, thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900