Question: In C Note the preprocessor definition of the DANGER_LEVEL constant. In general, when you want to use a magic number (i.e. a hard-coded literal value),

In C

Note the preprocessor definition of the DANGER_LEVEL constant. In general, when you want to use a magic number (i.e. a hard-coded literal value), you should do this instead.

Modify the code so the value of the level variable is read from the standard input (i.e. from the console) as a character. Note that whenever you expect a user to respond in the standard input, you should prompt them, asking for what you want.

Replace the if/else statement with a switch whose input is the character input by the user, converted to lowercase. The only valid inputs are e, f, or h for "empty", "full" and "half full". Print an appropriate message telling the user how full the tank is based on their input.

"Huh? What tank?" you ask. No. No context. Only C. C now. Context later.

If an invalid input is given by the user, an error message should be printed. To clarify, this means that you should print a message stating that the input was invalid; the error message should come from you, it should not be a runtime error.

You may find the following functions useful:

  • puts
  • getchar
  • tolower

You should look them up in the C manual by typing man followed by the function names in the terminal, like we did with printf and scanf earlier.

Note that to use the tolower function, you must include ctype.h (just like stdio.h is already included at the top of if-then-else.c). You can tell that this import is needed by reading the manual entry for tolower (see the first line of the synopsis).

#include  #define DANGER_LEVEL 5 /* C Preprocessor - - substitution on appearance */ /* like Java final */ int main(void) { float level = 1; /* if-then-else as in Java */ if (level <= DANGER_LEVEL) /*replaced by 5*/ printf("Low on gas! "); else printf("Good driver ! "); return 0; } 

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!