Question: Your program will prompt the user to enter a value for the cost of a meal. Retrieve the user input using scanf() and save the

Your program will prompt the user to enter a value for the cost of a meal. Retrieve the user input using scanf() and save the input value in a variable. The value should be read as type double. The user may or may not enter cents as part of the input. In other words, expect the user to enter values such as 5, 5.00 and 5.10. The program will then prompt the user to enter the percentage tip that should be added to the cost of the meal. This value should be read as type int and saved in a second variable. After the values have been entered, your program will calculate the amount of the tip and save the result in a third variable (consider the various ways to do this). The program will then add the tip amount to the cost of the meal and save this result in a fourth variable. Finally, it will print all four variables (meal cost, tip percentage, tip amount and total cost) to the screen in an informative format. Ensure that the floating point output always displays exactly two decimal places. Also, values of a magnitude less than 1 should display a leading 0 (ex. 0.75). Your program should check to ensure that negative values are not input. If an improper value is entered, an error message should be printed and the program will exit (do not re-prompt the user for a correct value). For input, you must use the fgets()/sscanf() combination of Standard I/O functions. Do not use the scanf() or fscanf() functions. Although using scanf() for retrieving numerical data is convenient, it is problematic when used in conjunction with character string input (as you may discover if you use it in later labs). Therefore, it is best to get into the habit of avoiding scanf() completely, and using the fgets()/sscanf() combination exclusively. Although you will learn more about this in later lectures and labs, this is what you should typically do to read a number using the fgets()/sscanf() combination:

// in main() or some function char inBuf[20]; // A buffer to hold the input from the keyboard int someInt = 0; // A variable to hold the number from the keyboard fgets(inBuf, 20, stdin); // Read the input from the keyboard and store it in inBuf sscanf(inBuf, "%d", &someInt); // Extract the numerical value from inBuf and store it in someInt

Note from the scanf() manpage that when reading a floating point value, if the data type of the variable that will receive the value is of type double, you must use "%lf" (long float) instead of "%f" (float) for your format string.

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!