Math calculator and compiler errors
Hi,
I'm writing a program that is supposed to be a math calculator. For the most part, I'm doing well, but when I compile it, I get some errors, that I don't understand, let alone know how to solve. Anyways, I was just hoping somebody could look at the errors and tell me what I'm doing wrong and suggestion some way to fix it. For completeness, I'm going to post all of my code. I'm sorry if I break any etiquette rules, but this is my first post.
Code:
#include <stdio.h>
/* function prototypes */
int sum( int x, int y );
int subtract( int x, int y );
int mult( int x, int y);
float divide( int x, int y );
int po( int x, int y );
int gcd( int x, int y );
int fact( int x );
/* function main begins program execution */
int main ()
{
int x; /* first variable for function input */
int y; /* second variable for function input */
int z; /* variable for menu selection */
/* menu options given to user */
printf( "Welcome to the math calculator!\n" );
printf( "Select a numerical option from the menu.\n" );
printf( "\n" );
printf( "1 Sum two integers.\n2 Subtract two integers.\n3 Multiply two integers.\n4 Divide two integers." );
printf( "5 Calculate an integer raised to an integer.\n 6 Compute GCD of two integers.\n 7 Factorial of integer.\n" );
printf( "8 Quit Program.\n" );
scanf( "%d", &z ); /* inputs menu option */
/* while loop calls the function, performs the computation, and returns to the menu */
while( z != 8 ) {
switch ( z ){
case ' 1 ' :
printf( "Enter x and y" ); /* prompts user for input values */
scanf( "%d%d", &x, &y ); /* inputs values to x and y */
int a = sum ( x,y ); /* defines sum to a variable */
printf( "Sum is %d.\n", a ); /* outputs sum */
break; /* breaks from switch...case */
case '2' :
printf( "Enter x and y" ); /* same as before */
scanf( "%d%d", &x, &y ); /* same as before */
int b = subtract( x,y ); /* same as before with "sum" replaced as "subtract" */
printf( "Difference is %d.\n", b ); /* same as before with "sum" replaced as "subtract" */
break; /* same as before */
case '3' :
printf( "Enter x and y" );
scanf( "%d%d", &x, &y );
int c = mult( x,y );
printf(" Product is %d.\n", c );
break;
case '4' :
printf( "Enter x and y" );
scanf( "%d%d", &x, &y );
/* ensures denominator is non-zero */
while ( y == 0 ) {
printf( "Please enter non-zero y.\n" );
scanf( "%d", &y );
}
float d = divide( x,y );
printf( "Quotient is %f.\n", d );
break;
case '5' :
printf( "Enter x and y" );
scanf( "%d%d", &x, &y );
int e = po( x,y );
printf( "Power is %d.\n", e );
break;
case '6' :
printf( "Enter x and y" );
scanf( "%d%d", &x, &y );
int j = gcd( x,y );
printf( "GCD is %d.\n", j );
break;
case '7' :
printf( "Enter x" );
scanf( "%d%d", &x );
/* ensures value is non-negative */
while ( x < 0 ) {
printf( "Please enter a non-negative value of x.\n" );
scanf( "%d", & x );
}
int k = fact( x );
printf("Factorial is %d.\n", k );
break;
/* prompts for menu options again */
printf( "Select a numerical option from the menu.\n" );
printf( "\n" );
printf( "1 Sum two integers.\n2 Subtract two integers.\n3 Multiply two integers.\n4 Divide two integers." );
printf( "5 Calculate an integer raised to an integer.\n 6 Compute GCD of two integers.\n 7 Factorial of integer.\n" );
printf( "8 Quit Program.\n" );
scanf( "%d", &z );
}
printf( "Goodbye!\n" );
return 0; /* indicate program ended succesfully */
} /* end function main */
/* function definitions */
/* definition of sum */
int sum( int x, int y )
{
return( x + y );
} /* end function sum */
/* definition of subtract */
int subtract( int x, int y )
{
return( x - y );
} /* end function subtract */
/* defintion of mult */
int mult( int x, int y )
{
return( x * y );
} /* end function mult */
/* definition of divide */
float divide( int x, int y)
{
float q; /* quotient variable */
q = ( float ) x / y;
return( q );
} /* end function divide */
/* definition of power */
int po( int x, int y )
{
int ret_value = 1;
int i; /* counter variable */
for ( i = 1; i <= y; i++ ){
ret_value *= x;
}
return( ret_value ); /* returns value at the end of the loop */
} /* end function power */
/* definition of gcd */
int gcd( int x, int y )
{
int r;
if ( x > y ) {
while ( y != 0 ){
r = x % y;
x = y;
y = r;
return( x );
}
else if ( y > x ) {
while ( x != 0 ){
r = y % x;
y = x;
x = r;
}
return ( y );
}
} /* end fuction gcd */
/* definition of factorial */
int fact( int x )
{
int f = 1;
int c;
for (c = x; c >= 1; c-- ) {
f *= c;
}
return( f );
} /* end function factorial */
I get the following compile errors:
project3.c:36:11: warning: multi-character character constant
project3.c: In function âgcdâ:
project3.c:188: error: expected expression before âelseâ
project3.c:211: error: expected declaration or statement at end of input
project3.c: In function âmainâ:
project3.c:211: error: expected declaration or statement at end of input
I was wondering if somebody could help me make sense of them. Also, in general, is there a way to decipher what these statements are?
Thanks.