Hey guys, ok i got a kinda decent working calculator id say, i know can compete with some out there, but its a start. Ok it works and everything, but id like to modify it this way. I would for main to be a seperate entity whereby it waits for the user to enter input, basically sits and waits for input from keyboard, and then i would like to have a seperate function to do the calculations, some like "float calc (float num1,float num2, char operator); /**calculates result of num1 and num2. So can anyone help me split the calculation part into a seperate function. My coding is below, i know it aint all that neat either, thanks!!
Code:
#include<stdio.h>
int main(void)
{
float numone,numtwo,ans;
char operation;
int loop=1;
system ("Cls");
printf("\n Welcome to calculator in C");
printf("\n These are the following supported operations");
printf("\n + -> addition \n - -> subtraction");
printf("\n * -> multiplication \n / -> division");
printf("\n\n\t\t Welcome to calculator in C Programming");
printf("\n\t\t\t Press 'Q' to EXIT Program");
printf("\n\t\t\t Matus' Calculator\n\n");
while(loop){
printf("\n\n Press any key to continue....");
getch();
system ("Cls");
printf("\t\t Welcome to calculator in C Programming");
printf("\n\t\t\t Press 'Q' to EXIT Program");
printf("\n\t\t\t Matus' Calculator");
printf("\n\n\n Enter the operation: ");
operation=getch();
switch(operation){
case '+': printf("\n Enter two numbers to add, ");
printf("\n\t\tnum 1: ");
scanf("%f",&numone);
printf("\t\tnum 2: ");
scanf("%f",&numtwo);
ans=numone+numtwo;
printf("\n\n %f + %f = %f",numone,numtwo,ans);
break;
case '-': printf("\n Enter two numbers to subtract, ");
printf("\n\t\tnum 1: ");
scanf("%f",&numone);
printf("\t\tnum 2: ");
scanf("%f",&numtwo);
ans=numone-numtwo;
printf("\n\n %f - %f = %f",numone,numtwo,ans);
break;
case '*': printf("\n Enter two numbers to multiply, ");
printf("\n\t\tnum 1: ");
scanf("%f",&numone);
printf("\t\tnum 2: ");
scanf("%f",&numtwo);
ans=numone*numtwo;
printf("\n\n %f * %f = %f",numone,numtwo,ans);
break;
case '/': printf("\n Enter two numbers to divide, ");
printf("\n\t\tnum 1: ");
scanf("%f",&numone);
printf("\t\tnum 2: ");
scanf("%f",&numtwo);
ans=numone/numtwo;
printf("\n\n %f / %f = %f",numone,numtwo,ans);
break;
case 'q': exit(0); //Exits Program Cleanly
break;
default: system("Cls");
printf("\n\n Unoperatable keys in program !!");
printf("\n\n Strike any key....");
getch();
loop=1;
} //End switch
}//End While
}//End Main