Does this look right?

The program runs but what I want to know is if I did things that were unnecessary? or was there an easier way to write it? this was what I had to do:

Write a program that displays a menu allowing the user to select one of these four gases (Gases are listed in the program). 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 one of the available menu choices.
Do not accept times less than 0 seconds or more than 30 seconds.



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
#include <iostream>
#include <vector>
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;
//More declarations
void displayMenu();
char acceptGas();
double convertGas(char gasType);
double acceptTime();
double computeDistance(double gasSpeed, double gasTime);
void  displayResults(double distance);

int main()
{
	do
	{
		char theGas = acceptGas();
		double theSpeed = convertGas(theGas);
		double theTime = acceptTime();

		double distance = computeDistance(theSpeed, theTime);
		displayResults(distance);

	} while (displayMenu);

	//End of do/while loop

	cout << "Please press ENTER to end program.";
	cin.sync();
	getchar();
	return 0;
}

//Displaying the gas menu for user to select.
void displayMenu()
{
	cout << "\n\n*********************************\n";
	cout << "\nPlease select a gas: \n";
	cout << "\n\ta. Carbon Dioxide\n";
	cout << "\tb. Air \n";
	cout << "\tc. Helium\n";
	cout << "\td. Hydrogen\n";
	cout << "\nChoose a gas: ";
}

//Accepting user input (the gas) and validating user choice.
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;
}

//Assigning the character to gases speeds.
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;
}

//Accepting user input (the time traveled in seconds) and validating user input.
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;
}

//Calculating the total distance.
double computeDistance(double gasSpeed, double gasTime)
{
	return gasSpeed * gasTime;
}

// Displaying the gas, time, and total distance.
void  displayResults(double distance)
{

	cout << "\n\t. The distance: " << distance << " meters" << endl;

}
Hello Newie. Overall, your code is pretty nice. I find it funny seeing someone use do-while loops, but if it works, it works. As for any corrections in the more proper sense, line 30 is a bit weird. The line is the while loop check, and what you have is:
} while (displayMenu);
But the thing is, you mine as well have while(true) instead. Why? It isn't actually calling the function displayMenu, for your code would look like while (displayMenu()), having the brackets, but instead it is simple going "does displayMenu exist?", but it does (obviously), so every time it goes through, it always says true. Another thing, thou again not really important, is the value of some functions that only get called once, namely computeDistance and displayResults. computeDistance is kinda a complex series of calls just to have the line gasSpeed * gasTime, which would have been simpler just having at line 24. displayResults also isn't too use full since you could just have line 134 at line 28 instead. Lastly, the program doesn't have a clean exit. You get stuck in an infinite loop and never really give the user a chance to leave the program except through closing the window. You might want to add another option in the "Please select a gas" to leave the program. Thanks, Spike :D
Thanks for your time and advice. Also, originally the program I wrote was this (listed below) which is called 'mission 5' and is based on mission 3 & 4. The funny thing is that I never wrote 3 & 4 so I went straight to 5. After writing mission 5 I started to remove certain things to make it seem as mission 3 hahah. I just wasn't sure about things because I didn't want to have anything in there that wasn't necessary as it would have resulted in points off.

Mission 5

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
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include <vector>
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;
//More declarations
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());
	//End of do/while loop

	cout << "Please press ENTER to end program.";
	cin.sync();
	getchar();
	return 0;
}

//Assing the user if they wish to continue and enter another gas.
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;
} 

//Displaying the gas menu for user to select.
void displayMenu()
{
	cout << "\n\n*********************************\n";
	cout << "\nPlease select a gas: \n";
	cout << "\n\ta. Carbon Dioxide\n";
	cout << "\tb. Air \n";
	cout << "\tc. Helium\n";
	cout << "\td. Hydrogen\n";
	cout << "\nChoose a gas: ";
}

//Accepting user input (the gas) and validating user choice.
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;
}

//Assigning the character to gases speeds.
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;
}

//Accepting user input (the time traveled in seconds) and validating user input.
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;
}

//Calculating the total distance.
double computeDistance(double gasSpeed, double gasTime)
{
	return gasSpeed * gasTime;
}

// Displaying the gas, time, and total distance.
void  displayResults(char theGas, double theTime, double distance)
{
	
			cout << "\n\n*********************************\n";
			cout << "\n\t. The gas: " << theGas << endl;
			cout << "\t. The time: " << theTime << " seconds" << endl;
			cout << "\t. The distance: " << distance << " meters" << endl;
		
}


Quick question, how would I set this line }While (displayMenu);to true? using a boolean?
Last edited on
Sorry for the delayed response. Simple answer, you just need to write while(true);. Long answer, while loops, if statements and the middle part of for loops
for(not here;here;not here either)
use statements to see if the condition is met to continue using it. A statement can be true or false. You can use a comparison to get the true or false (ie, X == 5), but you can also just simple type the answer (ie, just but if(true), then it will always be true). More into it, false only is false if the statement equals 0, while true is true if the statement does not equal 0 (apposed to equaling 1). Here is some examples:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// assume iostream with namespace std
int x = 10;
if (x < 11)
     cout << "This would display, for the statement is true\n";

if (x > 5 && x != 10)
     cout << "This won't display, for the statement is false\n";

if (true)
     cout << "This will display, for true == 1\n";

if (false)
     cout << "This won't display, for false == 0\n";

if (7)
     cout << "This will display, for anything but 0 is true\n";

if (((x/2)-5))
     cout << "This will not display for the answer will be 0, which is false\n";


Hope this helps a bit. I'm not the best at explaining things.
I get it now, thanks for your advice!
Topic archived. No new replies allowed.