I cant figure out why my terminal will not ask me for a second binary number aft
ID: 3810704 • Letter: I
Question
I cant figure out why my terminal will not ask me for a second binary number after operation is entered.
#include <stdio.h>
#include <string.h>
#include <math.h>
void userinput(char* input[]);
void useroperation(char* operation);
void calculation(int binary1,int binary2,char operation);
void binarytodecimal(char binstring[]);
int main()
{
char binary1[17]={"0"};
char binary2[17]={"0"};
char operation='0';
userinput(&binary1);
useroperation(&operation);
while (strcmp(&operation, "E") != 0)
{
userinput(&binary2);
binarytodecimal(binary1);
binarytodecimal(binary2);
calculation(binary1,binary2,operation);
useroperation(operation);
}
return 0;
}
void userinput(char* input[])
{
char buffer[100] = { 0 };
printf("Enter a 16 bit binary number(* 1s and 0s only *) ");
fgets( buffer, sizeof(buffer), stdin );
while (strlen(buffer)>17)
{
printf("Error, enter correct bit count " );
printf("Enter a 16 bit binary number(* 1s and 0s only *) ");
fgets( buffer, sizeof(buffer), stdin );
}
sscanf(buffer,"%s",input);
}
void useroperation(char* operation)
{
printf("Enter operator (+, -, *, /, <<, >>, |, &, ^, E) ");
fgets( operation, 2, stdin );
sscanf(operation,"%c",&operation);
}
void calculation(int binary1,int binary2,char operation)
{
int Answer=0;
switch (operation)
{
case '+':
Answer= binary1 + binary2;
break;
case '-':
Answer= binary1 - binary2;
break;
case '*':
Answer= binary1 * binary2;
break;
case '/':
Answer= binary1 / binary2;
break;
case '%':
Answer= binary1 % binary2;
break;
case '&':
Answer= binary1 & binary2;
break;
case '|':
Answer= binary1 | binary2;
break;
case '^':
Answer= binary1 ^ binary2;
break;
case '>':
Answer= binary1 >> binary2;
break;
case '<':
Answer= binary1 << binary2;
break;
}
printf("Answer for operation entered is %d ", Answer);
}
void binarytodecimal(char binstring[])
{
unsigned decimalval = 0;
int stringpos;
for (stringpos=strlen(binstring)-1; stringpos>=0; stringpos--)
{
decimalval = decimalval<<1;
if (binstring[stringpos]=='1') { decimalval += 1;}
}
printf("The binary string %s is equal to %u in decimal. ", binstring, decimalval);
}
Macbooks-MacBook-Pro: desktop MacPro$ /midterm Enter a 16 it binary number 1s and 0s only Enter operator I, I, &, A, E) Enter a 16 bit binary number 1s and 0s only The binary string 11111111 is equal to 255 in decimal The binary string 0 is equal to 0 in decimal. Answer for operation entered is 568581696 Enter operator I, &, A, E) Segmentation fault 11 Macbooks-MacBook-Pro:desktop Mac ProsExplanation / Answer
The reason why you are not able to insert second binary string is...
In your main(), you are first taking userinput() for binary1, no issue with that.
Then you are asking to mention the operation which means you are taking input from the keyboard again.
Then again you are calling userinput(), now for binary2.
This does not allow to enter the string right...
The reason is: when you are typing an operator say + then you will hit an ENTER key right, this ENTER key is also a character in C language which is termed as ' ' i.e. new line character. So, both, the + and the characters goes inside the input stream.
Now, the + is assigned to operation variable, so it is removed from input buffer. But is still there. So, when you are calling the userinput() function again, in which you are getting a string using fgets(). This function works as : it takes the characters from input buffer until it gets a new line character which means it reads a complete line.
But, as we observed earlier, the is already available inside your input buffer so it is the first character scanned by fgets() and it thinks that ' ' is encountered so the string is taken so it leaves and you are not able to write second string. It does not know that this ' ' was generated in previous line, the string entry is still remaining.
So, to be able to write the string, we need to remove ' ' first from input buffer. So, if we get a character from input buffer, only is remaining so it is get by the getter function. We do not require this to store anywhere as we just wanted to clear the input buffer so that it can again save character and fgets() save the string till it finds the new after user entry of string 2.
So, to achieve this, we just need to get a character after asking for operator. So, this can be achieved using getchar() function. So, we need to write a getchar() function after useroperation() function and before the while loop in main(). So the code for main() becomes....
int main()
{
char binary1[17]={"0"};
char binary2[17]={"0"};
char operation='0';
userinput(&binary1);
useroperation(&operation);
getchar();
while (strcmp(&operation, "E") != 0)
{
userinput(&binary2);
binarytodecimal(binary1);
binarytodecimal(binary2);
calculation(binary1,binary2,operation);
useroperation(operation);
}
return 0;
}
So that the goes in the getchar() so you can enter your string now. This is the solution to the question you have asked.
Please comment if there is any query. Thank you. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.