Question: Write a code in C that converts binary to decimal, If the user enters number other then 0 or 1 please write Number is not

Write a code in C that converts binary to decimal, If the user enters number other then 0 or 1 please write "Number is not a 0 or 1" if the user enters a blank please say "nothing entered" and print 0. if the user types in letters please indicate that the user typed in a letter. here is the starter code and instructors. Write a code in C that converts binary to decimal, If theuser enters number other then 0 or 1 please write "Number isnot a 0 or 1" if the user enters a blank please

Instructions: You will write a program bindec.c that will take binary numbers as input, and output the decimal equivalent. I provide code you will use as a starting point. 5. Your code should respond to empty input with value 0, and should respond to invalid input with message 'input must contain only zeros and ones', and exit with value 1, indicating an error. Your code should be able to handle inputs of at least 20 binary digits. Some examples of how your program should work: $ gcc -o bindec bindec.c $ ./bindec > 1001 9 $ ./bindec > 11111111 255 $ ./bindec > hello input must contain only zeros and ones $ ./bindec (no input given, just hit 'enter') 0 > 2 hw1 ) hw1 ) c main) No Selection #include 3 #include 4 #include 5 6 // max length input string 7 #define MAXSTR 25 8 9 // convert input binary string to a number 10 11 int main() { 12 13 // user input string 14 char s[MAXSTR+3]; 15 16 // prompt for input if input from terminal 17 if (isatty(fileno(stdin))) { 18 printf("> "); 19 } 20 21 // read input string; at most MAXSTR+1 chars accepted 22 // Note: this is tricky. If we accept only MAXSTR chars, 23 // we can't see if user entered more chars and they are 24 // being dropped by fgets. 25 fgets(s, MAXSTR+3, stdin); 26 27 // check input length; n does not include final carriage return 28 int n = strlen(s)-1; 29 if (n > MAXSTR) { 30 printf("input cannot be more than %d characters ", MAXSTR); 31 exit(1); 32 } 33 34 // convert s from a string in binary, to an int, and output 35 36 // YOUR CODE HERE 37 38 } 39 40 |

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!