I'm currently stuck with my code and I need assistance please. This is the background of what I am working on:
Mission 5 Background:
This mission is based of Primary Missions 03 & 04, which are copied below.
Repeat the assignment from Primary Mission 04 with the following changes:
Create a function that displays the menu of gases and returns a character (or integer) that represents the choice made by the user. The function should validate the user's input to ensure it is a valid selection.
Create a function the accepts from the user the time traveled in seconds and validates the value before returning it.
Create a function that has two parameters - the gas and the time. The function should compute the distance and return it to the caller.
What to submit:
Primary Mission 04 Description:
Repeat the assignment from Primary Mission 03 with the following changes:
Add a menu item to Exit or Quit the Program
When validating the user entered the correct menu choice, loop and give the user another chance to enter the values. Continue to loop until the user make a valid menu choice.
When validating the time, loop until the user enters a valid time.
The program should repeated ask the user for a gas, travel time of the sound and print the results. The repetition should end when the user selects Exit or Quit.
Primary Mission 03 Description:
When the two large asteroids impacted on the nearby planet, that planet broke into many small pieces that have been falling through the atmosphere of the planet where our command base is located. As part a previous mission, we had placed gas sensors with light and sound detectors around the planet in various locations near openings in the planet's surface. At each of these openings, either carbon dioxide, helium or hydrogen emerges. Command Central has asked us to use these sensors to determine how far from the gas vents the meteorites impact. We must create an application that determines the distance to a meteorite impact based on the time it takes for sound to reach the detector after the impact.
Mission Details:
When sound travels through a gas, its speed depends primarily on the density of the gas. The less dense the gas, the faster the speed will be. The following table shows the approximate speed of sound at 0 degrees centigrade when traveling through carbon dioxide, air, helium, and hydrogen.
Gas and its Speed (meters per second)
Carbon Dioxide 258.0
Air 331.5
Helium 972.0
Hydrogen 1270.0
Write a program that displays a menu allowing the user to select one of these four gases. After a selection has been made, the user should enter the number of seconds it took for the sound to travel in this medium from its source to the receiver where it was detected. The program should then report how far away (in meters) the source of the sound was from the detected location.
Input validation:
Check that the user has entered on of the available menu choices.
Do not accept times less than 0 seconds or more than 30 seconds.
This is what I have so far.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
#include <iostream>
using namespace std;
// Constant declarations
// Speeds are in meters per second
const double CARBON_DIOX_SPEED = 258.0;
const double AIR_SPEED = 331.5;
const double HELIUM_SPEED = 972.0;
const double HYDROGEN_SPEED = 1270.0;
bool userWantsToContinue();
void displayMenu();
char acceptGas();
double convertGas(char gasType);
double acceptTime();
double computeDistance(double gasSpeed, double gasTime);
void displayResults(char theGas, double theTime, double distance);
int main()
{
do
{
char theGas = acceptGas();
double theSpeed = convertGas(theGas);
double theTime = acceptTime();
double distance = computeDistance(theSpeed, theTime);
displayResults(theGas, theTime, distance);
} while (userWantsToContinue());
cout << "Please press ENTER to end program.";
cin.sync();
getchar();
return 0;
}
bool userWantsToContinue()
{
bool userChoice = false;
bool validInput = false;
char userInput = ' ';
do
{
cout << "\n\nDo you want to continue? (y/n): ";
cin >> userInput;
validInput = (userInput == 'y') || (userInput == 'n');
} while (!validInput);
userChoice = (userInput == 'y');
return userChoice;
}
void displayMenu()
{
cout << "\n\n*********************************\n";
cout << "Please select a gas: \n";
cout << "\ta. Carbon Dioxide\n";
cout << "\tb. Air \n";
cout << "\tc. Helium\n";
cout << "\td. Hydrogen\n";
cout << "Your choice human: ";
}
char acceptGas()
{
char userGas = ' ';
bool validInput = false;
do
{
displayMenu();
cin >> userGas;
validInput = (userGas == 'a') || (userGas == 'b')
|| (userGas == 'c') || (userGas == 'd');
if (!validInput)
{
cout << "\nPlease enter a choice from the menu!\n";
}
} while(!validInput);
return userGas;
}
double convertGas(char gasType)
{
double gasSpeed = 0.0;
if (gasType == 'a')
{
gasSpeed = CARBON_DIOX_SPEED;
}
else if (gasType == 'b')
{
gasSpeed = AIR_SPEED;
}
else if (gasType == 'c')
{
gasSpeed = HELIUM_SPEED;
}
else
{
gasSpeed = HYDROGEN_SPEED;
}
// cout << endl << "Gas Speed: " << gasSpeed << endl;
return gasSpeed;
}
double acceptTime()
{
double userTime = 0.0;
bool validInput = false;
do
{
cout << "\n\nEnter the time for the sound ( 0 - 30 seconds): ";
cin >> userTime;
validInput = (userTime >= 0.0) && (userTime <= 30.0);
if (!validInput)
{
cout << "\nPlease enter a valid time!\n";
}
} while(!validInput);
return userTime;
}
double computeDistance(double gasSpeed, double gasTime)
{
return gasSpeed * gasTime;
}
|