Missing something

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:

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.


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;
}
Last edited on
But what's your (detailed) question, please?
When I run the program I get an error and then asks me if i want to run the last successful build. I haven't been programming using C++ for long so I'm still learning.
Last edited on
If this is the entire code you've shown us, then it won't even link due to a missing function.

But please be somewhat more specific. I can't guess the error type, message and location.
So after removing these lines of code i found that it runs. I guess that the problem is when displaying the results which hasn't been implemented yet.

1
2
3
void  displayResults(char theGas, double theTime, double distance);

displayResults(theGas, theTime, distance);
Topic archived. No new replies allowed.