Thread: Program Displays menu twice after making a selection

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    5

    Angry Program Displays menu twice after making a selection

    I am working on a "Complex Arithmeatic" assignment for a programming class. I have everything working except for one minor issue. The first run through the program the menu displays and I make a selection and enter my numbers and the program returns the answer. However, after this the menu displays twice in a row before allowing me to make another selection. I cannot figure out why this is happening. Any suggestions would be highly appreciated! I have two .c files which I will paste below. The menu is at the end of the second .c file.

    MAIN.C
    Code:
    /*
    Jacob L. Jones
    Computer Programming II
    Lab #5
    */
    
    #include <stdio.h>
    #include "boolean.h"
    #include "complex.h"
    
    
    int main(void) {
    
      complex num1;
      complex num2;
      complex ans;
      char choice;
      boolean quit = 0;
      boolean show_menu = 1;
    
    while (quit == 0){
    
      if(show_menu == 1){
        menu();
        scanf("%c", &choice);
      }
      else{
        printf("Enter your selection: ");
        scanf("%c", &choice);
      }
     
    
       switch (choice){
           case '+':
                 num1 = Read_Complex();
                 num2 = Read_Complex();
                 ans = Add_Complex(num1, num2);
                 Write_Complex(ans);
           break;
    
           case '-':
                num1 = Read_Complex();
                 num2 = Read_Complex();
                 ans = Subtract_Complex(num1, num2);
                 Write_Complex(ans);
           break;
    
           case 'x':
           case 'X':
                 num1 = Read_Complex();
                 num2 = Read_Complex();
                 ans = Multiply_Complex(num1, num2);
                 Write_Complex(ans);
           break;
    
           case '/':
                 num1 = Read_Complex();
                 num2 = Read_Complex();
               if(num2.real == 0 && num2.img == 0){
                 printf("ERROR! Division by zero\n");
               }
                 else{
                 Divide_Complex(num1, num2);
                 }
           break;
    
           case 'c':
           case 'C':
                 num1 = Read_Complex();
                 ans = Conjugate_Complex(num1);
                 Write_Complex(ans);
           break;
    
           case '~':
                 num1 = Read_Complex();
                 ans = Negate_Complex(num1);
                 Write_Complex(ans);
           break;
    
           case 'a':
           case 'A':
                 num1 = Read_Complex();
                 Absolute_Complex(num1);
           break;
    
           case 'm':
           case 'M':
           if(show_menu == TRUE){
             show_menu = FALSE;
             }
           else{
             show_menu = TRUE;
           }
           break;
    
       case 'q':
       case 'Q':
         if(quit == 0){
           quit = 1;
         }
         break;
       }     
     }
    
    }


    COMPLEX.C
    Code:
    #include <stdio.h>
    #include <math.h>
    #include "complex.h"
    
    complex Read_Complex(void){
      complex num;
      printf("Enter real part:");
      scanf("%d", &num.real);
      printf("Enter imaginary part: ");
      scanf("%d", &num.img);
      return num;
    }
    
    void Write_Complex(complex num){
      if(num.img < 0){
        num.img = -1 * num.img;
        printf("The answer is %d - %di\n", num.real, num.img);
      }
      else{
        printf("The answer is %d + %di\n", num.real, num.img);
      }
    }
    complex Add_Complex (complex X1, complex X2){
      complex answer;
    
      answer.real = X1.real + X2.real;
      answer.img = X1.img + X2.img;
      return answer;
    }
    
    complex Subtract_Complex (complex X1, complex X2){
      complex answer;
    
      answer.real = X1.real - X2.real;
      answer.img = X1.img - X2.img;
      return answer;
    }
    
    complex Multiply_Complex (complex X1, complex X2){
      complex answer;
    
      answer.real = X1.real * X2.real - X1.img * X2.img;
      answer.img = X1.real * X2.img + X2.real * X1.img;
      return answer;
    }
    
    complex Negate_Complex (complex num){
      complex answer;
    
      answer.real = -1 * num.real;
      answer.img = -1 * num.img;
      return answer;
    }
    
    complex Conjugate_Complex (complex num){
      complex answer;
    
      answer.real = num.real;
      answer.img = -1 * num.img;
      return answer;
    }
    
    void Absolute_Complex (complex num){
      double answer;
    
      answer = sqrt(num.real * num.real + num.img * num.img);
      printf("The answer is %f\n", answer);
    }
    
    void Divide_Complex (complex X1, complex X2){
      complex answer;
      int den;
      float ar, ai;
     
      answer.real = X1.real * X2.real + X1.img * X2.img;
      answer.img = X1.img * X2.real - X1.real * X2.img;
      den = X2.real * X2.real + X2.img * X2.img;
      ar = answer.real / (float)den;
      ai = answer.img / (float)den;
      printf("The answer is %f + %fi\n", ar, ai);
    }
    
    void menu(){
      printf("+ Add\n");
      printf("- Subtract\n");
      printf("X Multiply\n");
      printf("/ Divide\n");
      printf("C Conjugate\n");
      printf("~ Negate\n");
      printf("A Absolute Value\n");
      printf("M Toggle Menu\n");
      printf("Q Quit\n");
      printf("Enter Selection: ");
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You press enter after you make your choice. scanf("%c", ...) reads just one character, and leaves the enter in the input buffer. The next time through your loop, scanf reads in the enter from the previous time. It doesn't match anything in your case statement, nothing happens and you're back to the top of the loop, printing your menu again. Quick fix: scanf(" %c", ...). That space before the %c ignores leading whitespace, including leftover new lines. Also, read up on flushing the input buffer: FAQ > Flush the input buffer - Cprogramming.com.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    That was it!! Thank you so much!

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    @jacobj86 i think you can try this at the selection menu while loop........
    while( menu == 1){menu();d = getch(stdin());


  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    @Nyah Check:

    That is not a good solution for two reasons. First, getch is not a standard C function, so jacobj86 might not even have it available to him. The standard alternative is getchar. Second, getchar will also leave new lines in the input buffer just like scanf did.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [C++] Need Help Making Menu Program
    By Bartvo in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2010, 12:14 AM
  2. Folder selection menu using Common Dialog Box?
    By Devil Panther in forum Windows Programming
    Replies: 6
    Last Post: 04-29-2007, 01:09 PM
  3. Program using menu selection and subprograms
    By hopeolicious in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2005, 12:36 PM
  4. menu selection trouble
    By Spectrum48k in forum C Programming
    Replies: 13
    Last Post: 11-20-2002, 11:06 PM
  5. Menu Selection
    By Ruski in forum C++ Programming
    Replies: 13
    Last Post: 10-12-2002, 09:11 PM