Re-entered value not returning to function

Hi I'm VERY new to C++ and am struggling to figure out what's wrong with my code. I'm doing a project to calculate the air density of a material which contain sphereical bubbles and cylindrical shapes pores. When I run the program and enter a negative value, I am prompted to enter a new value (as it should). However, when the calculations are performed, it uses the original negative value. So my code is only useful if I enter everything correctly.

Also if the number of pores or bubbles are zero, it should not ask me the radius of the bubbles or the radius/height of the pores. I'm not sure how to change it correctly.

Thanks for the help.

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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<math.h>

float height, width, length;
float s_radius;
int bubbles;
int pores;
float c_radius, c_height;
float per;

//Namespace Declaration
using namespace std;


//Functions Prototypes
float check_inputs_of_Parallelepiped(float height, float width, float length);
float check_inputs_sphere(int bubbles, float s_radius);
void check_inputs_pores(int pores);
void check_inputs_cylinder(float c_radius, float c_height);
float Volume_of_sphere(float s_radius);
float Volume_of_cylinder(float c_radius, float c_height);
float percentage(float width, float length, float height, float c_height, float c_radius, float s_radius, int bubbles, int pores);

//Starting of main function

int main()

{

	//Asking for user inputs

	cout << "Enter the height, length, and width of the sample in centimeters: ";
	cin >> height >> width >> length;

	//call to function

	check_inputs_of_Parallelepiped(height, width, length);

	cout << endl;

	cout << "How many spherical bubbles are present?: ";
	cin >> bubbles;

	//call to function

	check_inputs_sphere(bubbles, s_radius);

	cout << "What is the radius of the spherical bubbles in centimeters?: ";
	cin >> s_radius;

	//call to function

	check_inputs_sphere(bubbles, s_radius);

	cout << endl;

	cout << "How many cylindrical pores are present? ";
	cin >> pores;

	//call to functions

	check_inputs_pores(pores);

	cout << "What are the radius and height of the cylindrical pores in centimeters? ";
	cin >> c_radius >> c_height;

	//call to functions

	check_inputs_cylinder(c_radius, c_height);

	cout << endl;

	//call to function

	per = percentage(width, length, height, c_height, c_radius, s_radius, bubbles, pores);

	//Display the percent air present in the meterial

	cout << "The material contains " << fixed << setprecision(3) << per << "% " << "air" << endl;

	cout << endl;

	return 0;

}

//function defination

float check_inputs_of_Parallelepiped(float height, float width, float length)
{

	float Volume;

	do

	{

		if (height<0)

		{

			cout << "The height of the sample must be greater then zero.\n";

			cout << "Please re-enter the height of the sample: ";

			cin >> height;

		}
	} while (height<0);


	do
	{
		if (length<0)

		{

			cout << "The length of the sample must be greater then zero.\n";

			cout << "Please re-enter the length of the sample: ";

			cin >> length;

		}
	} while (length<0);


	do

	{

		if (width<0)

		{

			cout << "The width of the sample must be greater then zero.\n";

			cout << "Please re-enter the width of the sample: ";

			cin >> width;

		}
	} while (width<0);

	//calculate volume

	Volume = width * height * length;

	return Volume;

}

//function defination

float check_inputs_sphere(int bubbles, float s_radius)

{
	do
	{
		if (bubbles<0)
		{
			cout << "The number of spherical bubbles must be greater than or equal to zero.\n";
			cout << "Please re-enter the number of spherical bubbles: ";
			cin >> bubbles;
		}
	} while (bubbles<0);


	do
	{
		if (s_radius<0)
		{
			cout << "The radius of the bubbles must be greater than or equal to zero.\n";
			cout << "Please re-enter the radius of the bubbles: ";
			cin >> s_radius;
		}
	} while (s_radius<0);

	return s_radius;

}

void check_inputs_pores(int pores)
{
	do
	{
		if (pores<0)
		{
			cout << "The number of cylindrical pores must be greater than or equal to zero.\n";
			cout << "Please re-enter number of cylindrical pores: ";
			cin >> pores;
		}
	} while (pores<0);
	
}


//function defination

void check_inputs_cylinder(float c_radius, float c_height)
{
	do
	{
		if (c_radius<0)
		{
			cout << "The radius of cylindrical pores must be greater than zero.\n";
			cout << "Please re-enter the radius of the cylindrical pores.";
			cin >> c_radius;
		}

	} while (c_radius<0);


	do
	{
		if (c_height<0)
		{
			cout << "The height of cylindrical pores must be greater than zero.\n";
			cout << "Please re-enter the height of the cylindrical pores.";
			cin >> c_height;
		}

	} while (c_height<0);

}

//function defination

float percentage(float width, float length, float height, float c_height, float c_radius, float s_radius, int bubbles, int pores)

{

	float VoS, VoC, per;

	VoS = Volume_of_sphere(s_radius);
	VoC = Volume_of_cylinder(c_radius, c_height);
	per = (VoS*bubbles + VoC*pores) * 100 / (height*width*length);

	return per;

}

//function defination

float Volume_of_sphere(float s_radius)

{

	float PI = 3.14159;
	float Volume;

	//calculating volume of sphere

	Volume = (4 * PI * pow(s_radius, 3)) / 3;

	return Volume;

}

float Volume_of_cylinder(float c_radius, float c_height)

{

	float PI = 3.14159;
	float Volume;

	//calculating Volume of Cylinder

	Volume = PI * pow(c_radius, 2) * c_height;

	return Volume;

}
 
The problem with the check_inputs_... functions is that you pass by value. I.e. you pass a copy of the value. Changing that copy will not affect the original value.

So pass the values by reference:

float check_inputs_of_Parallelepiped(float &height, float &width, float &length) // Note the reference operator &

If you want to check for invalid input (like "abc") which raises an error you need to use ignore like so:

http://en.cppreference.com/w/cpp/io/basic_istream/ignore

See the Example
Topic archived. No new replies allowed.