3 Replies - 3707 Views - Last Post: 01 June 2009 - 04:25 PM Rate Topic: -----

#1 tomspencer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-May 09

How do you go back to the main function from a user defined function&#

Posted 01 June 2009 - 01:28 PM

Um.....................Hello there Everyone!

This is my first time posting anything, but anyway, I am very new to C programming. I have to write a programme (like a calculator) that takes the user from the main menue to a sub menue of the user's choice. Then from that menue the user would pick a type of math or computation, conversion etc. (ex. find the factorial of a number, find the sqaure root of a number etc)

After the computation is done I can make the programme go back to the sub menue but I DONT KNOW how to go from the sub-menue BACK to the main menue!

I mean, that when you press the relevant number for "EXIT" (to the main menue) in the sub-menue it doesnt go to the main meneu! I dont know how to call the main function!

Please help!
p.s the smiley that comes at the VERY end of the programme is actually the variable "b" I dont know how it keeps changing into a smiley!

//Programme for main menue
#include<stdio.h>
#include<conio.h>
#include<math.h>

//prototyping
void basic_function(void);
void trigonometry(void);
void conversions(void);
void matrix_math(void);
void other_functions(void);
void string_functions(void);

int main()
{
main_menue:
clrscr();
char selection;
printf("*****************************[+] [-] [*] [/] [%]********************************\n\t\t\t\tHello There!\n\n\t WELCOME TO 'Fun with Math' Calculator\n\t\t\t (*FUM CAL* for short!)\n");

printf("\n To get started with our calculator, please select one of the following options.");

printf("\n\n\t\t[1] Basic Functions\t[2] Trigonometry\n\n\n\t\t[3] Conversions\t\t[4] Matrix Math\n\n\n\t\t[5] Other Functions\t[6] String Functions");

printf("\n\n\n\t\t\t\t [7] Exit");

printf("\n\n\n\t\tPlease Enter your selection here : ");

scanf("%c",&selection);


switch(selection)
{
case'1':clrscr();printf("\n\n\t\t You have chosen [1] - Basic Functions");basic_function();break;

case'2':clrscr();printf("\n\n\t\t You have chosen [2] - Trigonometry");trigonometry();break;

case'3':clrscr();printf("\n\n\t\t You have chosen [3] - Conversions");break;

case'4':clrscr();printf("\n\n\t\t You have chosen [4] - Matrix Math");break;

case'5':clrscr();printf("\n\n\t\t You have chosen [5] - Other Function");break;

case'6':clrscr();printf("\n\n\t\t You have chosen [6] - String Functions");break;

case'7':clrscr();printf("\n\n\n\n\t\t\t You have chosen [7] - Exit");
printf("\n\n\n\n\t\t We hope you have ENJOYED using our 'FUM CAL'\n\n\n\n\t\t\t Thank you for using 'FUM CAL' \n\n\n\n\t\t\t Good bye! and Take Care!\n\n\n\t\t\t ");break;

default: clrscr();printf("\n\n\n\n\t\t\t ERROR - INVALID SELECTION!\n\n\t\t\t\tDear User!\n\n\t\tEither you typed in a value other than 1 - 7\n\n\t\t\t\t\ OR\n\n\t\t You have not read the User Guide carefully!");

fflush(stdin);printf("\n\n\n\t If you wish to go back to the Main menue (I am sure you do!)\n\n\n\t\t press ENTER KEY to go there: ");

getchar();goto main_menue;
}
return(0);
}

void basic_function()
{
void factorial();
void square_root();
void power();
void invert();
{
fflush(stdin);
bf_menue:
clrscr();
int n,i,answer;
char option;

printf("\n\t\t\t Basic Functions Menue!");

printf("\n\n\n\n\n\t\t[1] Factorial of n\t[2] Square root of n\n\n\n\n\t\t[3] X to the power Y\t[4] Invert n\n\n\n\n\t\t\t\t [5] Exit");

printf("\n\n\n\n\n\t\tChoose what Basic Function you wish to check : ");

scanf("%c",&option);
switch(option)
{
case'1':clrscr();printf("\n\n\t\t You have chosen [1] - Factorial of n");factorial();break;

case'2':clrscr();printf("\n\n\t\t You have chosen [2] - Square root of n");square_root();break;

case'3':clrscr();printf("\n\n\t\t You have chosen [3] - X to the power Y");power();break;

case'4':clrscr();printf("\n\n\t\t You have chosen [4] - Invert n");invert();break;

case'5':clrscr();printf("\n\n\t\t You have chosen [5] - Exit");break;

default: clrscr();printf("\n\n\n\n\t\t\t ERROR - INVALID SELECTION!\n\n\t\t\t\tDear User!\n\n\t\tEither you typed in a value other than 1 - 5\n\n\t\t\t\t\ OR\n\n\t\t You have not read the user manual carefully!");

fflush(stdin);printf("\n\n\n If you wish to go back to the Basic Functions menue (I am sure you do!)\n\n\n\t\t press ENTER KEY to go there: ");

getchar();goto bf_menue;
}
}
}
//1.1 Factorial
void factorial()
{
long int n;
printf("\n\n\n\n\n\n\n\t Enter the number you want to search for factorial: ");
scanf("%ld",&n);

long int answer=n;
int i=1;
while(i<n)
{
answer=(n-i)*answer;
i++;
}

printf("\n\n\n\n\t\t\tFactorial of %ld! = %d",n,answer);

fflush(stdin);

printf("\n\n\n\n\n\t Press Enter to go back to Basic Function Menue : ");
getchar();
basic_function();
}

//1.2 Square root of n
void square_root()
{
float number,a,b=1,c=1;
printf("\n\n\n\n\n\n\t\t Enter a number greater than 1: ");
scanf("%f",&number);
while(b==1)
{
a=c*c;
if(a>=number)
{
break;
}
c=c+0.000001;
}
printf("\n\n\n\n\n\t\t The Square Root of %.2f is %.5f\n",number,c);
fflush(stdin);
printf("\n\n\n\n\n\t\tPress Enter to go back to Basic Function Menue : ");
getchar();
basic_function();
}

//1.3 X to the power Y
void power()
{
int number,power;
printf("\n\n\n\n\n\t Enter the number you wish to know the power of : ");
scanf("%d",&number);

printf("\n\n\n\n\t\t\t Enter the power : ");
scanf("%d",&power);

int answer=number;
int i=1;
while(i<power)
{
answer=number*answer;
i++;
}
if(power==0)
{
answer=1;
}
printf("\n\n\n\n\t\t\t %d to the power %d = %d",number,power,answer);
printf("\n");
fflush(stdin);
printf("\n\n\n\n\t\tPress Enter to go back to Basic Function Menue : ");
getchar();
basic_function();
}

//Invert n
void invert()
{
float number;
double answer;

{
printf("\n\n\n\n\n\t What is the number that you wish to know the Invert of : ");
scanf("%f",&number);
answer = 1/number;
printf("\n\n\n\n\t\t The Invert of %f is %f",number,answer);
fflush(stdin);

printf("\n\n\n\n\n\t\tPress Enter to go back to Basic Function Menue : ");
getchar();
basic_function();
}
}

//2 Trigonometry
void trigonometry()
{
int sel;
double j;
float i,a,b,c;

printf("\n\n\n\n\t\t 1. Degrees\t2. Radians\n\n");
printf("\n\n\t\t Select the type you want: ");
scanf("%d",&sel);

switch(sel)
{

case 1: printf("\n\n\n\t\t Enter value in degrees: ");
scanf("%lf",&j);

i = (j * (22/7))/180;

a = sin(i);
printf("\n\t\t Sin %f = %f \n",j,a);

b = cos(i);
printf("\t\t Cos %f = %f \n",j,B);

c = tan(i);
printf("\t\t Tan %f = %f \n",j,c);
break;


case 2: if(sel==2) printf("\n\n\t\t Enter value in radians:");
scanf("%f",&i);

a = sin(i);
printf("\n\t\t\t Sin %f = %f \n",i,a);

b = cos(i);
printf("\t\t\t Cos %f = %f \n",i,B);

c = tan(i);
printf("\t\t\t Tan %f = %f \n",i,c);
break;
}
fflush(stdin);
printf("\n\n\n\t\tPress Enter to go back to Main Menue : ");
getchar();



}
Attached File  CALCULATOR_SAMPLE.txt (6.85K)
Number of downloads: 65Attached File  CALCULATOR_SAMPLE.txt (6.85K)
Number of downloads: 65

This post has been edited by tomspencer: 01 June 2009 - 01:35 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How do you go back to the main function from a user defined function&#

#2 Lillefix  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 204
  • Joined: 19-September 08

Re: How do you go back to the main function from a user defined function&#

Posted 01 June 2009 - 01:47 PM

Here, formatted pour toi
//Programme for main menue 
#include<stdio.h>
#include<conio.h>
#include<math.h>

//prototyping
void basic_function(void);
void trigonometry(void);
void conversions(void);
void matrix_math(void);
void other_functions(void);
void string_functions(void);

int main()
{
	main_menue:
	
	clrscr();
	
	char selection;
	printf("*****************************[+] [-] [*] [/] [%]********************************\n\t\t\t\tHello There!\n\n\t WELCOME TO 'Fun with Math' Calculator\n\t\t\t (*FUM CAL* for short!)\n");
	printf("\n To get started with our calculator, please select one of the following options.");
	printf("\n\n\t\t[1] Basic Functions\t[2] Trigonometry\n\n\n\t\t[3] Conversions\t\t[4] Matrix Math\n\n\n\t\t[5] Other Functions\t[6] String Functions");
	printf("\n\n\n\t\t\t\t [7] Exit");
	printf("\n\n\n\t\tPlease Enter your selection here : ");
	scanf("%c",&selection);
	
	
	switch(selection)
	{
		case'1':
			clrscr();
			printf("\n\n\t\t You have chosen [1] - Basic Functions");
			basic_function();
			break;
			
		case'2':
			clrscr();
			printf("\n\n\t\t You have chosen [2] - Trigonometry");
			trigonometry();
			break;
			
		case'3':
			clrscr();
			printf("\n\n\t\t You have chosen [3] - Conversions");
			break;
			
		case'4':
			clrscr();
			printf("\n\n\t\t You have chosen [4] - Matrix Math");
			break;
			
		case'5':
			clrscr();
			printf("\n\n\t\t You have chosen [5] - Other Function");
			break;
			
		case'6':
			clrscr();
			printf("\n\n\t\t You have chosen [6] - String Functions");
			break;
			
		case'7':
			clrscr();
			printf("\n\n\n\n\t\t\t You have chosen [7] - Exit");
			printf("\n\n\n\n\t\t We hope you have ENJOYED using our 'FUM CAL'\n\n\n\n\t\t\t Thank you for using 'FUM CAL' \n\n\n\n\t\t\t Good bye! and Take Care!\n\n\n\t\t\t ");
			break;
			
		default:
			clrscr();
			printf("\n\n\n\n\t\t\t ERROR - INVALID SELECTION!\n\n\t\t\t\tDear User!\n\n\t\tEither you typed in a value other than 1 - 7\n\n\t\t\t\t\ OR\n\n\t\t You have not read the User Guide carefully!");
			fflush(stdin);printf("\n\n\n\t If you wish to go back to the Main menue (I am sure you do!)\n\n\n\t\t press ENTER KEY to go there: ");
			getchar();
			goto main_menue;
	}
	return(0);
}

void basic_function()
{
	void factorial();
	void square_root();
	void power();
	void invert();
	{
		fflush(stdin);
		bf_menue:
		clrscr();
		int n,i,answer;
		char option;
		
		printf("\n\t\t\t Basic Functions Menue!");
		printf("\n\n\n\n\n\t\t[1] Factorial of n\t[2] Square root of n\n\n\n\n\t\t[3] X to the power Y\t[4] Invert n\n\n\n\n\t\t\t\t [5] Exit");
		printf("\n\n\n\n\n\t\tChoose what Basic Function you wish to check : ");
		
		scanf("%c",&option);
		switch(option)
		{
			case'1':
				clrscr();
				printf("\n\n\t\t You have chosen [1] - Factorial of n");
				factorial();
				break;
				
			case'2':
				clrscr();
				printf("\n\n\t\t You have chosen [2] - Square root of n");
				square_root();
				break;
				
			case'3':
				clrscr();
				printf("\n\n\t\t You have chosen [3] - X to the power Y");
				power();
				break;
				
			case'4':
				clrscr();
				printf("\n\n\t\t You have chosen [4] - Invert n");
				invert();
				break;
				
			case'5':
				clrscr();
				printf("\n\n\t\t You have chosen [5] - Exit");
				break;
				
			default:
				clrscr();
				printf("\n\n\n\n\t\t\t ERROR - INVALID SELECTION!\n\n\t\t\t\tDear User!\n\n\t\tEither you typed in a value other than 1 - 5\n\n\t\t\t\t\ OR\n\n\t\t You have not read the user manual carefully!");
				fflush(stdin);
				printf("\n\n\n If you wish to go back to the Basic Functions menue (I am sure you do!)\n\n\n\t\t press ENTER KEY to go there: ");
				getchar();
				goto bf_menue;
		}
	}
}
//1.1 Factorial
void factorial()
{
	long int n;
	printf("\n\n\n\n\n\n\n\t Enter the number you want to search for factorial: ");
	scanf("%ld",&n);
	
	long int answer=n;
	int i=1;
	while(i<n)
	{
		answer=(n-i)*answer;
		i++;
	}
	
	printf("\n\n\n\n\t\t\tFactorial of %ld! = %d",n,answer);
	
	fflush(stdin);
	
	printf("\n\n\n\n\n\t Press Enter to go back to Basic Function Menue : ");
	getchar();
	basic_function();
}

//1.2 Square root of n
void square_root()
{
	float number,a,b=1,c=1;
	printf("\n\n\n\n\n\n\t\t Enter a number greater than 1: ");
	scanf("%f",&number);
	while(b==1)
	{
		a=c*c;
		if(a>=number)
		{
			break;
		}
		c=c+0.000001;
	}
	printf("\n\n\n\n\n\t\t The Square Root of %.2f is %.5f\n",number,c);
	fflush(stdin);
	printf("\n\n\n\n\n\t\tPress Enter to go back to Basic Function Menue : ");
	getchar();
	basic_function();
}

//1.3 X to the power Y
void power()
{
	int number,power;
	printf("\n\n\n\n\n\t Enter the number you wish to know the power of : ");
	scanf("%d",&number);
	
	printf("\n\n\n\n\t\t\t Enter the power : ");
	scanf("%d",&power);
	
	int answer=number;
	int i=1;
	while(i<power)
	{
		answer=number*answer;
		i++;
	}
	if(power==0)
	{
		answer=1;
	}
	printf("\n\n\n\n\t\t\t %d to the power %d = %d",number,power,answer);
	printf("\n");
	fflush(stdin);
	printf("\n\n\n\n\t\tPress Enter to go back to Basic Function Menue : ");
	getchar();
	basic_function();
}

//Invert n
void invert()
{
	float number;
	double answer;
	
	{
		printf("\n\n\n\n\n\t What is the number that you wish to know the Invert of : ");
		scanf("%f",&number);
		answer = 1/number;
		printf("\n\n\n\n\t\t The Invert of %f is %f",number,answer);
		fflush(stdin);
		
		printf("\n\n\n\n\n\t\tPress Enter to go back to Basic Function Menue : ");
		getchar();
		basic_function();
	}
}

//2 Trigonometry 
void trigonometry()
{
	int sel;
	double j;
	float i,a,b,c;
	
	printf("\n\n\n\n\t\t 1. Degrees\t2. Radians\n\n");
	printf("\n\n\t\t Select the type you want: ");
	scanf("%d",&sel);
	
	switch(sel)
	{
			
		case 1:
			printf("\n\n\n\t\t Enter value in degrees: ");
			scanf("%lf",&j);
			
			i = (j * (22/7))/180;
			
			a = sin(i);
			printf("\n\t\t Sin %f = %f \n",j,a);
			
			b = cos(i);
			printf("\t\t Cos %f = %f \n",j,b);	//Corrected typo
				   
			c = tan(i);
			printf("\t\t Tan %f = %f \n",j,c);
			break;
				   
		case 2: 
			printf("\n\n\t\t Enter value in radians:");
			scanf("%f",&i);
				   
			a = sin(i);
			printf("\n\t\t\t Sin %f = %f \n",i,a);
			  
			b = cos(i);
			printf("\t\t\t Cos %f = %f \n",i,b);	//Type again
						  
			c = tan(i);
			printf("\t\t\t Tan %f = %f \n",i,c);
			break;
	}
					 
	fflush(stdin);
	printf("\n\n\n\t\tPress Enter to go back to Main Menue : ");
	getchar();
}

Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,140
  • Joined: 14-September 07

Re: How do you go back to the main function from a user defined function&#

Posted 01 June 2009 - 03:17 PM

When a function dies/finishes/etc... it will return to where it was called. Thus, if menuOne was called from main() it would return to main() upon completion. You can then format your menus/functions accordingly.
Was This Post Helpful? 0
  • +
  • -

#4 yohosuff  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 01-May 09

Re: How do you go back to the main function from a user defined function&#

Posted 01 June 2009 - 04:25 PM

You should consider putting your main menu in a loop of some sort, so that when your menu item function returns, the main menu will automatically be displayed again. If the user chooses to exit the main menu, include code that will set the looping condition to false so the program can exit normally. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1