Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,133 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,035 people online right now. Registration is fast and FREE... Join Now!




Menu-Driven Program

 
Reply to this topicStart new topic

Menu-Driven Program, The program will not end when typing the correct 'sentinel' va

investyoung
post 7 Oct, 2008 - 02:21 PM
Post #1


New D.I.C Head

*
Joined: 1 Oct, 2008
Posts: 13

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.


CODE

/*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;

}
User is offlineProfile CardPM

Go to the top of the page

AmitTheInfinity
post 7 Oct, 2008 - 09:35 PM
Post #2


C Surfing ∞

Group Icon
Joined: 25 Jan, 2007
Posts: 1,015



Thanked 34 times

Dream Kudos: 125
My Contributions


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].
User is offlineProfile CardPM

Go to the top of the page

overlord_shimra
post 8 Oct, 2008 - 11:42 AM
Post #3


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 13



Thanked 1 times
My Contributions


I suggest this:
CODE
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;

}
User is offlineProfile CardPM

Go to the top of the page

UG Cyber
post 8 Oct, 2008 - 01:29 PM
Post #4


D.I.C Head

**
Joined: 24 Jul, 2008
Posts: 184



Thanked 3 times
My Contributions


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

cpp

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

User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 12:00PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month