Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ exercise: In this exercise, you start creating your own data types. Then create variables of these types, and set these variables to valid values

C++ exercise:

In this exercise, you start creating your own data types. Then create variables of these types, and set these variables to valid values of that type. Then use those variables. An easy way to do this is with C++ enumerated types. You will also use input validation loops, to help the user enter valid data. Some background:

How would you add fourteen and twenty seven? You would probably convert the fourteen to 14; twenty seven to 27; align them and add: 14 + 27 ---- 41 This required two type conversions from string to number. You perform one more conversion: 41 to forty one. Three type conversions: string to number, string to number, number to string.

How would you add blue and yellow? (To get green.) Would you want strings: blue and yellow then add the strings to get: blueyellow? No. Or numbers: blue=1, yellow=2; blue+yellow=3; green=3? No, because blue+blue=yellow (1+1=2) and two blue isnt yellow. Assigning numbers to non-numeric objects creates magic numbers which are error prone. Colors do not work like strings or numbers. Colors are their own special type. To operate on colors, you need a color wheel and customized operations.

How to represent colors in C++? A quick and easy way is to define a new type: Color. And list, or enumerate valid values of that type. A color can be: red, green, blue, yellow, cyan, magenta, etc. Or, in C++: enum Color {red, green, blue, yellow, cyan, magenta}; // in Color, capital C indicates: Type.

To create a variables of type color, you could write: Color sky=blue, sea=green, sun=yellow, mars=red, lake=cyan; It would not be valid to write: sky= "blue "; sky=3; sky=7.00134; because these are not valid color values.

Magic numbers like 1, 2, 3, 4, ... do not appear. Colors are not numbers, they are colors. Colors use their own natural names. Only valid color names can be used.

1) Practice creating and using a type of your own. Make an enumerated type. Do not use color. Provide a name for the enumerated type and at least 3 valid values. Then create at least 5 variables of that type. Set the variables to valid values. The names should represent some real world objects and make sense. Put this code in main at the top. Dont perform any input or output. Just write code that makes sense and compiles.

2) For doing real work, you will enhance the program to compute the speed of sound in various materials. For the materials, use an enumerated type for materials: air, water, steel. enum Material {air, water, steel} . internally uses air=0; water=1; steel=2. Or use: enum material {air='a', water='w', steel='s'}; Internally, C++ must use machine-supported types. By default C++ uses int in sequence: 0, 1, 2, 3, etc. You can override this by using numbers or chars of your own. This is useful. When entering an option, the user can enter numbers or letters. This user input can be cast into a material later, using static_cast(input). For example:

enum Material {air='a', water='w', steel='s'}; cout<<"Enter material a)ir w)ater s)teel or q)uit: "; char option{}; cin>>option; if (option != 'q') { Material material = static_cast(option); cout<<"Internally, your material is stored as: "<

3) In a loop, get the material, then get how many feet the sound travels in the material. Compute and display how many seconds it takes for the sound to travel that distance. 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, 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. Use these speeds of sound: air: 1100 fps; water: 4900 fps; steel: 16,400 fps. (fps = feet per second).

4) When entering the material option or the feet to travel, the user may enter an invalid value, such as an invalid material option letter or a negative distance. 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.

5) To handle different options, use a switch statement instead of if else if else if else. Use a default for invalid input. Switch on the Material type, not int or char.

Sample output: Calculate the time (in seconds) it takes for sound to travel through a selected medium. Select a medium from the menu; then enter the distance the sound travels in feet.

Choose medium (or quit) A)ir, W)ater, S)teel, Q)uit: a Enter the distance (feet) sound travels in air: 1000 Sound takes 0.9091 seconds to travel 1000.00 feet in air.

Choose medium (or quit) A)ir, W)ater, S)teel, Q)uit: a Enter the distance (feet) sound travels in air: 1100 Sound takes 1.0000 seconds to travel 1100.00 feet in air.

Choose medium (or quit) A)ir, W)ater, S)teel, Q)uit: t Invalid option, please try again.

Choose medium (or quit) A)ir, W)ater, S)teel, Q)uit: w Enter the distance (feet) sound travels in water: 5000 Sound takes 1.0204 seconds to travel 5000.00 feet in water.

Choose medium (or quit) A)ir, W)ater, S)teel, Q)uit: w Enter the distance (feet) sound travels in water: 4900 Sound takes 1.0000 seconds to travel 4900.00 feet in water.

Choose medium (or quit) A)ir, W)ater, S)teel, Q)uit: s Enter the distance (feet) sound travels in steel: 30000 Sound takes 1.8293 seconds to travel 30000.00 feet in steel.

Choose medium (or quit) A)ir, W)ater, S)teel, Q)uit: s Enter the distance (feet) sound travels in steel: -2000 Invalid distance, please enter a distance of 0 or more feet. Enter the distance (feet) sound travels in steel: -1 Invalid distance, please enter a distance of 0 or more feet. Enter the distance (feet) sound travels in steel: 0 Sound takes 0.0000 seconds to travel 0.00 feet in steel.

Choose medium (or quit) A)ir, W)ater, S)teel, Q)uit: q Good-bye. Thank you for using the program.

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

Medical Image Databases

Authors: Stephen T.C. Wong

1st Edition

1461375398, 978-1461375395

More Books

Students also viewed these Databases questions