I'm trying to make a binary calculator that terminates the the program when q
is entered. As a result I made a variable called quit
to act as the flag q
is entered. However when I made the scanf
for the variable q
it seems to take away split the inputs up when q
is not pressed. for example, if I enter 110 + 110
I get:
110 + 110 = 1000
as if it's doing 10 + 110
.
The input has to be all in one line. The format of the input is always going to be:
Operand operator Operand
Which is why I have the:
scanf(" %s %c %s",Binary1, &op, Binary2);
on my code. However if the user input is just q
or Q
it terminates the program.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double convertDouble(char Bin []);
double convertBinary(double theAns);
double calculate(double num1,double num2,char op);
main(void) {
char op;
float num1,num2;
char quit;
double n1,n2,ans;
double binAns;
char Binary1[20];
char Binary2[20];
char Input[100];
char myChar;
int i = 0;
// quit = 'k';
while (quit != 'q'){
printf("Enter an expression using binary numbers or Q to quit: ");
// scanf("%s", &quit);
if(quit == 'q' ){
exit(EXIT_SUCCESS);
}else{
}
scanf(" %s %c %s",Binary1, &op, Binary2);
/* while(Binary1[i] > 49 || Binary2[i] > 49){
if(Binary1[i] >= 'q' || Binary1[i]<= 'Q' ){
exit(0);
quit = 'q';
}else{
printf("please enter binary: ");
scanf("%s %c %s\n",&Binary1, &op, &Binary2);
}
i++;
} */
// quit[0] = Binary1[0];
printf("quit = %c\n", quit);
printf("Binary1 =%s\n", Binary1);
printf("op =%c\n", op);
printf("Binary2 =%s\n", Binary2);
n1 = convertDouble(Binary1);
n2 = convertDouble(Binary2);
ans = calculate(n1,n2,op);
binAns = convertBinary(ans);
// printf("value of n1 = %f\n", n1);
// printf("value of n2 = %f\n", n2);
// printf("value of binAns = %f\n", binAns);
// printf("ans = %f", ans);
printf(" = %f\n",binAns);
} // end of while
printf("quit = %c\n", quit);
printf("Binary1 =%s\n", Binary1);
printf("op =%c\n", op);
printf("Binary2 =%s\n", Binary2);
printf("quit");
exit(EXIT_SUCCESS);
}
Note:
More info about my program: the reason why I have the input binaries as strings was because it needs to convert them to a double
. It does this in a different function called convertDouble
which is not pasted here. It calculates the answer in the function calculate(also not pasted). The answer is converted to a binary in the convertBinary
function and then is displayed.
Example of How it's supposed to run
Enter an expression using binary numbers or Q to quit: 1101.001 * 101.1101
= 1001100.0100101
Enter an expression using binary numbers or Q to quit: 0.0000000001 * 0.0000000001
= 0.00000000000000000001
Enter an expression using binary numbers or Q to quit: 0.111 * 1000.0
= 111.0
Enter an expression using binary numbers or Q to quit: 11.0 * 11.0
= 1001.0
Enter an expression using binary numbers or Q to quit: 11.11 + 11.11
= 111.1
Enter an expression using binary numbers or Q to quit: 101.1 / 10.0
= 10.11
Enter an expression using binary numbers or Q to quit: 1001.11 - 11.01
= 110.1
Enter an expression using binary numbers or Q to quit: q