Question
C++ DEVC++. This lab is based on Chapter 4, The Speed of Sound. 1) Write a program as described, but include a loop so the
C++
DEVC++.
This lab is based on Chapter 4, "The Speed of Sound".
1) Write a program as described, but include a loop so the user can perform as many calculations as desired. Present a menu of options; prompt for: a)ir, w)water, s)teel and q)uit. For ease of use, provide an option to quit gracefully at any time. Accept the first letter of each material as input, then ask for the distance. Display the time it takes for sound to travel that distance (in seconds). Test your loop on multiple options in a single run. Reject invalid data. From the textbook: "Input validation: Decide how the program should handle an illegal input for the menu choice or a negative value for the distance." If the user enters an invalid option, display an error message and ask again. Do not accept distances less than 0. If the user enters an invalid distance (less than 0) display an error message and ask for the distance again.
2) For processing the different material options, use a switch statement instead of if else if else if else. Use the default for invalid material option input ,the switch statement.
3) Make an enumerated type to designate the options. Example: enum material {air, water, steel};. This will set air=0; water=1; steel=2, which can be used for options. Or try: enum material {air='a', water='w', steel='s', quit='q'}; and use the type-name letter defaults as options. By using an enum type, you can avoid using "magic numbers" like 0, 1, 2.
4) Read material data from a file. Read the table from a file containing the following text: Medium Speed Air 1100 Water 4 Steel 16400 This file can be created with copy/paste: copy the 4 lines above, and paste them into a new text file called.This provides a complete, working FileIO example. If reading from your own data file, submit both the code and the data file. Because the material type names (in the file) cannot be known in advance, this option means you cannot use an enum.
5)For a bigger challenge, handle the case where more materials are added to the data file, such as: Copper 7500 Nylon 1150 Iron 8200 If reading from a file, your CANNOT assume you know the contents of the data file in advance. You may assume that each material begins with a different letter. Do not "hard code" the prompt or the material or the speed in the program code. Instead, read the data from the materials file first. Create a prompt that displays each material like above. You cannot use switch or enum as before, because the option letters are not known in advance. Handle up to 6 materials.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started