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".
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <setjmp.h>
usingnamespace 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
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <setjmp.h>
usingnamespace 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...