Displaying results
Dec 10, 2014 at 6:35am UTC
I'm having some issues getting the answer to display (gasSpeed * gasTime). The loop just repeats but the total is not printed. Anyone can help me figure out what I'm doing wrong?
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 143
#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);
int main()
{
do
{
char theGas = acceptGas();
double theSpeed = convertGas(theGas);
double theTime = acceptTime();
double distance = computeDistance(theSpeed, theTime);
} 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;
}
Last edited on Dec 10, 2014 at 6:36am UTC
Topic archived. No new replies allowed.