Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Solaris 11.2 System Administration (oracle Press)

Authors: Harry Foxwell

1st Edition

007184421X, 9780071844215

More Books

Students also viewed these Databases questions