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: ");
}