Any tips?

So I posted before asking for help about displaying my results but never got a reply. Anyway, I figured it out, I guess that what I want to know now is if there is a way that I can make my code better? Are there things that are not necessary or if there's a way to display things better? Or is my code fine as it is?

Initially this is what I had to do:


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.


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
144
145
146
147
148
#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 << "Choose a gas: ";
}

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 valid 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;
}
void  displayResults(char theGas, double theTime, double distance)
{
 cout << "\n\n*********************************\n";
 cout << "\t. The gas: " << theGas << endl;
 cout << "\t. The time: " << theTime << endl;
 cout << "\t. The distance: " << distance << endl;
}
Last edited on
Topic archived. No new replies allowed.