The attached program needs to run to accept an input from a user of one of 5 com
ID: 3881011 • Letter: T
Question
The attached program needs to run to accept an input from a user of one of 5 commands, X for max, N for min, A for Absolute value, S for square a value, and Q for quit. The commands work fine, but every time the program runs a case from the switch, it seems to compute and then output a default case as well. I've tried a number of workarounds, but nothing works.
#include <stdio.h>
#include <stdlib.h>
#define FALSE 0
#define TRUE 1
/*
*
*/
int main(void) {
char comm;
int in1,in2,output;
int runFlag;
runFlag = TRUE;
do {
printf("Enter: 'A' for absolute value, 'X' for maximum, 'N' for minimum, 'S' for the square, Or 'Q' to quit. ");
scanf("%c",&command);
switch (command){
case 'A':
case 'a':
printf("Enter an integer: ");
scanf("%d",&in1);
if (in1 >= 0) {
output = in1;
printf("Absolute value of %d is %d. ",in1,output);
}else{
output = -in1;
printf("Absolute value of %d is %d. ",in1,output);
}
break; /*This break causes default to kick in */
case 'X':
case 'x':
printf("Enter two integers separated by a space: ");
scanf("%d %d",&in1,&in2);
output = (in1>in2)?in1:in2;
printf("The maximum is %d. ",output);
break;
case 'N':
case 'n':
printf("Enter two integers separated by a space: ");
scanf("%d %d",&in1,&in2);
output = (in1<in2)?in1:in2;
printf("The minimum is %d. ",output);
break;
case 'S':
case 's':
printf("Enter an integer: ");
scanf("%d",&in1);
output = in1*in1;
printf("The square of %d is %d. ",in1,output);
break;
default:
printf("Invalid Entry.");
break;
case 'Q':
case 'q':
runFlag = FALSE;
break;
}
}while (TRUE == runFlag);
return (0);
}
Explanation / Answer
THE PROBLEM WITH CODE IS THAT WHEN YOU ENTER COMMAND LIKE A,X,N etc. AND PRESS "ENTER" KEY THEN IT WILL READ ENTER AS " " APART FROM ENTERED CHARCTER. HENCE DEFAULT CASE WAS ALWAYS EXECUTING.
THE SOLUTION FOR THIS PROBLEM IS CHANGE THIS LINE
scanf(" %c", &comm); TO
scanf(" %c", &comm);
PLEASE REFER BELOW WORKING CODE
#include <stdio.h>
#include <stdlib.h>
#define FALSE 0
#define TRUE 1
/*
*
*/
int main(void) {
char comm;
int in1,in2,output;
int runFlag;
runFlag = TRUE;
do {
printf("Enter: 'A' for absolute value, 'X' for maximum, 'N' for minimum, 'S' for the square, Or 'Q' to quit. ");
scanf(" %c", &comm);
switch (comm){
case 'A':
case 'a':
printf("Enter an integer: ");
scanf("%d",&in1);
if (in1 >= 0) {
output = in1;
printf("Absolute value of %d is %d. ",in1,output);
}else{
output = -in1;
printf("Absolute value of %d is %d. ",in1,output);
}
break; /*This break causes default to kick in */
case 'X':
case 'x':
printf("Enter two integers separated by a space: ");
scanf("%d %d",&in1,&in2);
output = (in1>in2)?in1:in2;
printf("The maximum is %d. ",output);
break;
case 'N':
case 'n':
printf("Enter two integers separated by a space: ");
scanf("%d %d",&in1,&in2);
output = (in1<in2)?in1:in2;
printf("The minimum is %d. ",output);
break;
case 'S':
case 's':
printf("Enter an integer: ");
scanf("%d",&in1);
output = in1*in1;
printf("The square of %d is %d. ",in1,output);
break;
case 'Q':
case 'q':
runFlag = FALSE;
break;
default:
printf("Invalid Entry.");
break;
}
}while (TRUE == runFlag);
return (0);
}
PLEASE REFER BELOW OUTPUT
khushal@khushal-ubuntu:~/Desktop/Chegg$ gcc -g Commands_op.c
khushal@khushal-ubuntu:~/Desktop/Chegg$ ./a.out
Enter:
'A' for absolute value,
'X' for maximum,
'N' for minimum,
'S' for the square,
Or 'Q' to quit.
A
Enter an integer: 23
Absolute value of 23 is 23.
Enter:
'A' for absolute value,
'X' for maximum,
'N' for minimum,
'S' for the square,
Or 'Q' to quit.
X
Enter two integers separated by a space: 21
21
The maximum is 21.
Enter:
'A' for absolute value,
'X' for maximum,
'N' for minimum,
'S' for the square,
Or 'Q' to quit.
N
Enter two integers separated by a space: 12 34
The minimum is 12.
Enter:
'A' for absolute value,
'X' for maximum,
'N' for minimum,
'S' for the square,
Or 'Q' to quit.
S
Enter an integer: 12
The square of 12 is 144.
Enter:
'A' for absolute value,
'X' for maximum,
'N' for minimum,
'S' for the square,
Or 'Q' to quit.
Q
khushal@khushal-ubuntu:~/Desktop/Chegg$
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.