Menu-Driven Program

The program will not end when typing the correct 'sentinel' va

Page 1 of 1

3 Replies - 8168 Views - Last Post: 08 October 2008 - 02:29 PM Rate Topic: -----

#1 investyoung  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-October 08

Menu-Driven Program

Post icon  Posted 07 October 2008 - 03:21 PM

I have to build a program that has a menu-driven program that has a user input the selection he/she wants and loops until the user types 'q'. This program does not end like it is supposed to when I type in the selction of 'q'. Instead it says 'Incorrect choice, please enter a new selection' when it should be ending. I had to make the program so that it would catch incorrect characters, but I need it to recognize 'q' as my sentinel value so it will end. Any suggestions are appreciated. Thank you.


/*prototype Math Calculator*/
#include <stdio.h>
 
int calculate_sum(x, y){ /*function to calculate sum of two integers*/
			int sum = x + y; /*equation*/
				return sum; /*the sum of x,y*/
		} /*end function*/

int calculate_difference (x, y){/*function to calculate distance of two integers*/
			int difference = x - y; /*equation*/
				return difference; /*difference of x,y*/
		}/*end function*/

int calculate_product (x, y){/*calculate product of two integers*/
			int product = x * y; /*equation*/
			return product; /*returns the product of x,y*/
		} /*end function*/

float calculate_quotient (int x, int y){ /*calculates the quotient of two values*/
			
			while (y == 0) {
			printf ("Sorry, you cannot divide by 0, enter a new menu selection\n\n");		   
			} /*end while*/
			if ( y != 0){ /*begin if statement*/
			int quotient = x / y;
				return quotient;
			} /*end if*/
			
		}/*end function calculate_quotient*/
 
int calculate_intPower (x, y){/*function to calculate base to power exponent*/
			int intPow = 1;/*initialize variable intPow*/
			int i; /*initialize i*/
 
			if (y <1){ /*begin if*/
				return 1;
			}/*end if*/
					
			for ( i = 1; i <= y; ++i ) {
					  
			intPow = intPow * x;			
			}/*end for*/
				return intPow;
		}/*end function intPower*/

float calculate_gcd (int x, int y){/*function to calculate GCD*/
			if  (x < y){ /*begin if*/
				int swap(x,y);
					} /*end if*/
			while (y != 0) {
				int remainder = x % y;
				x = y;
				y = remainder;
					return x;
			} /*end while*/
		}/*end function*/
 
int calculate_factorial (int x){ /*calculate factorial of an integer*/
			int factorial = 1; /*initialzie variable*/
			int counter = 0; /*initialzie variable*/
			
			if ( x <= 1) {/*begin if*/
					return 1;
			}/*end if*/
			for ( counter = x; counter >= 1; counter -- ){/*begin for*/
						factorial *= counter;
				}/*end for*/
					return factorial;
			}/*end funtion calculate_factorial*/

int main ()/*begin main function*/
{

int user_choice;
int x;
int y;
int q = 0; 

printf ("Welcome to the Super Math Calculator!!!");
printf ( "\n\nPlease select one of the following menu items");
printf ( "\n\n1. Add two integers");
printf ( "\n2. Subtract two integers" );
printf ( "\n3. Multiply two integers" );
printf ( "\n4. Divide two integers" );
printf ( "\n5. Calculate an integer raised to a positive integer power" );
printf("\n6. Compute the Greatest Common Divisor of two integers" );
printf ( "\n7. Compute the factorial of an integer" );
printf ( "\nq. Quit\n\n" );
 
/*loop until user types  q*/
while ( (user_choice = getchar() ), user_choice != q ){

/*determine which choice was input*/
			switch (user_choice) {
					
						case '1':
						printf ( "\nEnter the first integer to add:" );
						scanf ( "%d", &x );
						printf ( "\nEnter the second integer to add:" );
						scanf ( "%d", &y);
						printf ("\nThe sum of %d and %d is %d\n\n", x,y, calculate_sum(x,y) );
						break; /*exit switch*/
 
						case '2':
						printf ( "\nEnter the first integer to subtract:" );
						scanf ( "%d", &x );
						printf ( "\nEnter the second integer to subtract:" );
						scanf ( "%d", &y);
						printf ("\nThe difference of %d and %d is %d\n\n", x,y, calculate_difference(x,y) );
						break; /*exit switch*/
 
						case '3':
						printf ( "\nEnter the first integer to multiply:" );
						scanf ( "%d", &x );
						printf ( "\nEnter the second integer to multiply:" );
						scanf ( "%d", &y);
						printf ("\nThe product of %d and %d is %d\n\n", x,y, calculate_product(x,y) );
						break; /*exit switch*/
 
						case '4':
						printf ( "\nEnter the first integer to divide:" );
						scanf ( "%d", &x );
						printf ( "\nEnter the second integer to divide by:" );
						scanf ( "%d", &y);
						printf ("\nThe quotient of %d and %d is %.2f\n\n", x,y, calculate_quotient(x,y) );
						break; /*exit switch*/
 
						case '5':
						printf ( "\nEnter the first integer base:" );
						scanf ( "%d", &x );
						printf ( "\nEnter the second integer exponent:" );
						scanf ( "%d", &y);
						printf ("\n%d to the %d power is %d\n\n", x,y, calculate_intPower(x,y) );
						break; /*exit switch*/
						
						case '6':
						printf ( "\nEnter the first integer:" );
						scanf ( "%d", &x );
						printf ( "\nEnter the second integer t:" );
						scanf ( "%d", &y);
						printf ("\nThe Greatest Common Denominator of %d and %d is %d\n\n", x,y, calculate_gcd(x,y) );
						break; /*exit switch*/			  
						
						case '7':
						printf ( "\nEnter an integer:" );
						scanf ( "%d", &x );
						printf ("\nThe factorial of %d is %.2f\n\n", x, calculate_factorial(x) );
						break; /*exit switch*/			  
 
						case '\n':
						case '\t':
						case ' ':
						break; /*exit switch*/
 
						default: /*catch all other characters*/
						printf ( "Incorrect choice, enter a new selection!\n\n");
						break; /*exit switch*/

						} /*end switch*/
			}   /*end while*/
 

printf ( "Thank you for using the Super Math Calculator\n\n" );
 
return 0;
 
} 


Is This A Good Question/Topic? 0
  • +

Replies To: Menu-Driven Program

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: Menu-Driven Program

Posted 07 October 2008 - 10:35 PM

I guess this while ( (user_choice = getchar() ), user_choice != q ){ is the problem.
try this while ( (user_choice = getchar() ), tolower(user_choice) != 'q' ){

and I would suggest to take that assignment to user_choice out to make it more readable and less complex [even though that is not creating any problem in your code].
Was This Post Helpful? 1
  • +
  • -

#3 overlord_shimra  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 07-October 08

Re: Menu-Driven Program

Posted 08 October 2008 - 12:42 PM

I suggest this:
int main ()/*begin main function*/
{

int user_choice;
int x;
int y;
int q = 0;

printf ("Welcome to the Super Math Calculator!!!");
printf ( "\n\nPlease select one of the following menu items");
printf ( "\n\n1. Add two integers");
printf ( "\n2. Subtract two integers" );
printf ( "\n3. Multiply two integers" );
printf ( "\n4. Divide two integers" );
printf ( "\n5. Calculate an integer raised to a positive integer power" );
printf("\n6. Compute the Greatest Common Divisor of two integers" );
printf ( "\n7. Compute the factorial of an integer" );
printf ( "\nq. Quit\n\n" );

/*loop until user types  q*/
while ( user_choice != q ){
			(user_choice = getchar() );

/*determine which choice was input*/
			switch (user_choice) {
					
						// cases go here.

						} /*end switch*/
			}   /*end while*/


printf ( "Thank you for using the Super Math Calculator\n\n" );

return 0;

} 

Was This Post Helpful? 1
  • +
  • -

#4 UG Cyber  Icon User is offline

  • D.I.C Addict

Reputation: 32
  • View blog
  • Posts: 556
  • Joined: 24-July 08

Re: Menu-Driven Program

Posted 08 October 2008 - 02:29 PM

You may want to add a way at the end so that the user can see the goodbye note.

printf ( "Thank you for using the Super Math Calculator\n\n" );
GetChar();
return 0;


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1