Jumping to the main function

In this program I am trying to go from one of the void functions, back to the main function. But every time I try to compile it I get the error: "'main' was not declared in this scope".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <setjmp.h>

using namespace std;


void square()
{
	cout<<"This calculates the area of a square.  Please enter your sides:"<<endl;
	int s;
	cin>>s;
	int a=s*s;
	cout<<"The calculated area with given sides: "<<s<<" is: "<<a<<endl;
}

void circle()
{
	cout<<"This calculates the area of a circle.  Please enter your radius:"<<endl;
	int r;
	cin>>r;
	int pi=3.14159265;
	int a=pi*(r*r);
	cout<<"The calculated area with given radius: "<<r<<" is: "<<a<<endl;
}

void menu()
{
	int mid;
	
	cout<<"This is the menu.  Please choose an option number below:"<<endl;
	cout<<"For the area of a circle press (1)"<<endl<<"For area of a square press (2)"<<endl;
	cout<<"To exit press any other key"<<endl;
	cin>>mid;
	if(mid==1)
	{
		circle();
	}
	
	if(mid==2)
	{
		square();
	}
	
	if(mid!=1||2)
	{
		
		main();
		
	}

}


int main()
{
	start:
	string answ;
	cout<<"Do you want to calculate the area of a square or circle?  y/n"<<endl;
	cin>>answ;
	if(answ=="y")
	{
		menu:
		menu();
		goto menu;
	}
	if(answ=="n")
	{
		cout<<"Do you want to exit?  y/n"<<endl;
		cin>>answ;
		if(answ=="y")
		{
			goto end;
		}
		if(answ=="n")
		{
			goto start;
		}
	}
	end:
	system("pause");
	return 0;


}
On line 52 this statement is not valid main(); and on line 26 int pi=3.14159265; You are trying to store a double value in an integer variable so pi will be 3 not 3.14159265
return from that function and you will be back to main. is this a option ?
in line 52 main() function do not exists yet. One way to fix it would be

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <setjmp.h>

using namespace std;

void square();
void circle();
void menu();

int main()
{
	start:
	string answ;
	cout<<"Do you want to calculate the area of a square or circle?  y/n"<<endl;
	cin>>answ;
	if(answ=="y")
	{
		menu:
		menu();
		goto menu;
	}
	if(answ=="n")
	{
		cout<<"Do you want to exit?  y/n"<<endl;
		cin>>answ;
		if(answ=="y")
		{
			goto end;
		}
		if(answ=="n")
		{
			goto start;
		}
	}
	end:
	system("pause");
	return 0;
}

void square()
{
	cout<<"This calculates the area of a square.  Please enter your sides:"<<endl;
	int s;
	cin>>s;
	int a=s*s;
	cout<<"The calculated area with given sides: "<<s<<" is: "<<a<<endl;
}

void circle()
{
	cout<<"This calculates the area of a circle.  Please enter your radius:"<<endl;
	int r;
	cin>>r;
	int pi=3.14159265;
	int a=pi*(r*r);
	cout<<"The calculated area with given radius: "<<r<<" is: "<<a<<endl;
}

void menu()
{
	int mid;
	
	cout<<"This is the menu.  Please choose an option number below:"<<endl;
	cout<<"For the area of a circle press (1)"<<endl<<"For area of a square press (2)"<<endl;
	cout<<"To exit press any other key"<<endl;
	cin>>mid;
	if(mid==1)
	{
		circle();
	}
	
	if(mid==2)
	{
		square();
	}
	
	if(mid!=1||2)
	{
		
		main();
		
	}

}


Or you may prepend your code with a forward declaration:
int main();

But its usually not the best idea to call main() from anywhere inside your program. Its purpose is to serve as process entry point only scheduling some initialization and starting the show.
Don't call main explicitly. Just let the function end and it will return to the previous scope (main). What's getting you is the infinite loop wrapped around the function call.

1
2
3
4
label:
f();
goto label;
// execution won't make it here 


A great way to avoid this kind of confusion is to avoid goto altogether...
Or better: Forget forever those goto keyword of C++!
Topic archived. No new replies allowed.