1) Write a program as described, but include a loop so the user can perform as m
ID: 3752522 • Letter: 1
Question
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. (max 16 points).
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. For the switch statement, see pages 210 -215 (7ed & 8ed), pages 213-220 (9ed). (max 18 points)
3) Make an enumerated type to designate the options. See page 219 (7ed & 8ed), pages 222-223 (9ed). 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. (max 20 points)
Extra credit: Read material data from a file. Read the table from a file containing the following text:
Medium Speed
Air 1100
Water 4900
Steel 16400
This file can be created with copy/paste: copy the 4 lines above, and paste them into a new text file called: DDHH_L4_Lastname_In.txt. Modify DDHH as needed. DDHH is the course DayDayHourHour designator, for example: TT10, MW16, TT17, DL5, etc. To see some working sample code which does almost exactly what you need to read data from a file, see: "Program Samples", Simple FileIO. 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. (max 22 points).
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. This is easier to do with arrays (Chapter 8) but can be done without arrays. (max 24 points).
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
// For maximum record
#define MAX 10
// Defines enumeration for medium type for array index
// air is 0, water is 1, steel is 2, copper is 3, nylon is 4, iron is 5, and quit is 6
enum type {air, water, steel, copper, nylon, iron, quit};
// Defines enumeration for medium type for menu choice
// Air is a, Water is w, Steel is s, Copper is c, Nylon is n, Iron is i, and quit is 6
enum choice {Air = 'a', Water = 'w', Steel = 's', Copper = 'c', Nylon = 'n', Iron = 'i', Quit = 'q'};
// Function to read medium and speed information from file and stores it in respective array
void readFile(string medium[], int speed[])
{
// Counter for records
int counter = 0;
// ifstream object declared
ifstream fRead;
// Opens the file for reading
fRead.open("MaterialSpeed.txt");
// Checks if the file unable to open for reading display's error message with file name
if(!fRead)
{
cout<<" ERROR: Unable to open the file MaterialSpeed.txt for reading.";
exit(0);
}// End of if condition
// Reads the heading
getline(fRead, medium[0]);
// Loops till end of the file
while(!fRead.eof())
{
// Extracts medium from the file and stores it at counter index position of medium array
fRead>>medium[counter];
// Extracts speed from the file and stores it at counter index position of speed array
fRead>>speed[counter];
// Increase the book counter by one
counter++;
}// End of while loop
// Closes the file
fRead.close();
}// End of function
// Function to display menu, accept user choice and return user choice
char menu()
{
// To store user choice
char ch;
// Displays menu
cout<<" A)ri W)ater S)teel C)opper N)ylon I)ron Q)uit";
// Accepts user choice
cout<<" Enter your choice: ";
cin>>ch;
// Returns user choice
return ch;
}// End of function
// main function definition
int main()
{
// Declares an string array to store medium of size MAX
string medium[MAX];
// Declares an integer array to store speed of size MAX
int speed[MAX];
// To store distance entered by the user
int distance;
// Calls the function to read file contents and stores it in arrays
readFile(medium, speed);
// Loops till valid distance entered by the user
do
{
// Accepts distance
cout<<" Enter distance: ";
cin>>distance;
// Checks if distance is zero or negative then display error message
if(distance <= 0)
cout<<" ERROR: Invalid distance. Cannot be 0 or negative.";
// Otherwise stop the loop
else
break;
}while(1);// End of do - while loop
// Loops till user choice is not 'Q'
do
{
// Calls the function menu() to accept user choice
// Converts the return character to enumeration type choice
// Calls the function based on the user choice
switch((choice)menu())
{
case Air:
// (type)air - Converts the air value to enumeration type
// for medium array index position
cout<<" Time taken by the material - "<<medium[(type)air];
// Converts the distance and speed to meter per second
cout<<" is: "<<(double)(distance * 1000) / (speed[(type)air] * 1000);
break;
case Water:
cout<<" Time taken by the material - "<<medium[(type)water];
cout<<" is: "<<(double)(distance * 1000) / (speed[(type)water] * 1000);
break;
case Steel:
cout<<" Time taken by the material - "<<medium[(type)steel];
cout<<" is: "<<(double)(distance * 1000) / (speed[(type)steel] * 1000);
break;
case Copper:
cout<<" Time taken by the material - "<<medium[(type)copper];
cout<<" is: "<<(double)(distance * 1000) / (speed[(type)copper] * 1000);
break;
case Nylon:
cout<<" Time taken by the material - "<<medium[(type)nylon];
cout<<" is: "<<(double)(distance * 1000) / (speed[(type)nylon] * 1000);
break;
case Iron:
cout<<" Time taken by the material - "<<medium[(type)iron];
cout<<" is: "<<(double)(distance * 1000) / (speed[(type)iron] * 1000);
break;
case Quit:
exit(0);
default:
cout<<" Invalid material type selected.";
}// End of switch case
}while(1);// End of do - while loop
}// End of main function
Sample Output:
Enter distance: 0
ERROR: Invalid distance.
Cannot be 0 or negative.
Enter distance: -5
ERROR: Invalid distance.
Cannot be 0 or negative.
Enter distance: 25
A)ri
W)ater
S)teel
C)opper
N)ylon
I)ron
Q)uit
Enter your choice: a
Time taken by the material - Air is: 0.0227273
A)ri
W)ater
S)teel
C)opper
N)ylon
I)ron
Q)uit
Enter your choice: w
Time taken by the material - Water is: 0.00510204
A)ri
W)ater
S)teel
C)opper
N)ylon
I)ron
Q)uit
Enter your choice: s
Time taken by the material - Steel is: 0.00152439
A)ri
W)ater
S)teel
C)opper
N)ylon
I)ron
Q)uit
Enter your choice: c
Time taken by the material - Copper is: 0.00333333
A)ri
W)ater
S)teel
C)opper
N)ylon
I)ron
Q)uit
Enter your choice: n
Time taken by the material - Nylon is: 0.0217391
A)ri
W)ater
S)teel
C)opper
N)ylon
I)ron
Q)uit
Enter your choice: i
Time taken by the material - Iron is: 0.00304878
A)ri
W)ater
S)teel
C)opper
N)ylon
I)ron
Q)uit
Enter your choice: m
Invalid material type selected.
A)ri
W)ater
S)teel
C)opper
N)ylon
I)ron
Q)uit
Enter your choice: q
MaterialSpeed.txt file contents
Medium Speed
Air 1100
Water 4900
Steel 16400
Copper 7500
Nylon 1150
Iron 8200
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.