Getting a weird output, help?

I've included the entirity of the code here, I'm getting an odd output here, I keep getting it asking me for input twice before it accepts it, and then it doesn't seem to do the computations correctly. I'm really not sure where I went wrong "Shape.h" contains constants used in the formulas:

Shape.h
1
2
3
4
5
6
7
8
9
10
#include <iostream>

	//Constants
	const double PI = 3.141593;
 	const char SPHERE = '1';
	const char CYLINDER = '2';
	const char CONE = '3';
	const char QUIT = 'q';
	const char VOLUME = '1';
	const char SURFACE_AREA = '2';


main
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
#include <iostream>
#include <iomanip>
#include <cmath>
#include "Shape.h"
using namespace std;

//Declare Functions
char inputShape();
void inputDimension (double radius);
void inputDimension (double radius, double height);  
char inputCompType();
void performComputation (char charType, double radius);
void performComputation (char shape, char charType, double radius, double height);
double sphere_volume (double radius);
double sphere_surface_area (double radius);
double cylinder_volume (double radius, double height);
double cylinder_surface_area (double radius, double height);
double cone_volume (double radius, double height);
double cone_surface_area (double radius, double height);

int main()
{
	char shape = '0';
	char compType = '0';
    double radius = 0.0;
    double height = 0.0;
   
	// Call inputShape function to request the first shape
    // type or ‘q’ and return result to the shape variable 
	inputShape();
	shape = inputShape();

	// Loop until the user specifies QUIT for the shape type
	while (shape != 'q')
	{	
		// For Spheres:
		if (shape == SPHERE)
		{
			// Call the appropriate inputDimension function
			// to request the radius
			inputDimension(radius);

			// Call the inputCompType function to request 
			// the computation type and return result to 
			// the compType variable
			inputCompType();
			compType = inputCompType();

			// Call the appropriate performComputation
			// function to compute and display the result
			performComputation (compType, radius);
		}
		// For Cylinders and Cones:
		else if (shape == CYLINDER || shape == CONE)
		{
			// Call the appropriate inputDimension function
			// to request the radius and height
			inputDimension(radius, height);

			// Call the inputCompType function to request 
			// the computation type and return result to 
			// the compType variable
			inputCompType();
			compType = inputCompType();

			// Call the appropriate performComputation
			// function to compute and display result
			performComputation (shape, compType, radius, height);
		}

		// Call inputShape function to request the next shape 
		// type or ‘q’ and return result to the shape variable  
		inputShape();
		shape = inputShape();
	}
    // end loop
	
	if (shape == QUIT)
	{
		cout << "Good bye!" << endl;
	}

    system("pause");
    return 0;
}

//Input Functions
char inputShape()
{
	char input = '0';

	cout << "Select a Shape (1) sphere (2) cylinder (3) cone (q) quit:";
	cin >> input;

	return input;
}

void inputDimension (double)
{
	double sphereRadius = 0.0;
	
	cout << "Enter radius: ";
	cin >> sphereRadius;
}

void inputDimension (double, double)
{
	double r = 0.0;
	double h = 0.0;

	cout << "Enter radius: ";
	cin >> r;
	cout << "Enter height: ";
	cin >> h;
}

char inputCompType()
{
	char type = '0';

	cout << "Select a Computation (1) volume (2) surface area: ";
	cin >> type;

	return type;
}

//Computation Functions
void performComputation (char compType, double radius)
{
	if (compType = VOLUME)
	{
		sphere_volume(radius);
		cout << "Volume of sphere is " << setprecision(3) << sphere_volume(radius) << endl;
	}
	else if (compType = SURFACE_AREA)
	{
		sphere_surface_area(radius);
		cout << "Surface area of sphere is " << setprecision(3) << sphere_surface_area(radius) << endl;
	}
}

void performComputation (char shape, char compType, double radius, double height)
{
	if (shape = CYLINDER)
	{
			if (compType = VOLUME)
			{
				cylinder_volume(radius, height);
				cout << "Volume of cylinder is " << setprecision(3) << cylinder_volume(radius, height) << endl;
			}
			else if (compType = SURFACE_AREA)
			{
				cylinder_surface_area(radius, height);
				cout << "Surface area of cylinder is " << setprecision(3) << cylinder_surface_area(radius, height) << endl;
			}
	}
	else if (shape = CONE)
	{
			if (compType = VOLUME)
			{
				cone_volume(radius, height);
				cout << "Volume of cone is " << setprecision(3) << cone_volume(radius, height) << endl;
			}
			else if (compType = SURFACE_AREA)
			{
				cone_surface_area(radius, height);
				cout << "Surface area of cone is " << setprecision(3) << cone_surface_area(radius, height) << endl;
			}	
	}
}

//Sphere Functions
double sphere_volume (double radius)
{
	double sphere_volume = 0.0;

	sphere_volume = (4/3) * PI * pow(radius,3);

	return sphere_volume;
}

double sphere_surface_area (double radius)
{
	double sphere_surface_area = 0.0;

	sphere_surface_area = 4 * PI * pow(radius,2);

	return sphere_surface_area;
}

//Cylinder Functions
double cylinder_volume (double radius, double height)
{
	double cylinder_volume = 0.0;

	cylinder_volume = PI * pow(radius,2) * height;

	return cylinder_volume;
}

double cylinder_surface_area (double radius, double height)
{
	double cylinder_surface_area = 0.0;

	cylinder_surface_area = 2 * PI * pow(radius,2) + 2 * PI * radius * height;

	return cylinder_surface_area;
}

//Cone Functions
double cone_volume (double radius, double height)
{
	double cone_volume = 0.0;

	cone_volume = (1/3) * PI * pow(radius,2) * height;

	return cone_volume;
}

double cone_surface_area (double radius, double height)
{
	double cone_surface_area = 0.0;

	cone_surface_area = PI * pow(radius,2) + PI * radius * sqrt(pow(radius,2) + pow(height,2));

	return cone_surface_area;
}
delete line 30, 46, 63, 73 to get rid of the double input. I would double check your if statements for example if (compType = VOLUME) should be if (compType == VOLUME)

your inputdimention function on line 106 needs to return a value.

line 177 sphere_volume = (4/3) * PI * pow(radius,3); this will give the wrong answer you have to be carefull of integer division try this sphere_volume = (4.0/3.0) * PI * pow(radius,3);
Last edited on
I keep getting it asking me for input twice before it accepts it
:
That is because you called the function inputShape() twice:
1
2
inputShape(); // delete this, you don't need it ! ( and also line 73 )
shape = inputShape(); // you only need this 


When calling functions, we don't have to call them twice, instead when they have a something to return,
we should declare a variable similar to the return type of the function, just like what you did:
shape = inputShape();

An exception is void, void means that the funtion will return nothing and thus we will not need a variable to store the return value, ( since void has no return value ):
like: printOpeningScreen()

-----------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
double radius = 0.0;

/* bla bla bla */

inputDimension(radius); // you just called the function but nothing happens inside , no processing etc..

/* ... ... ... */

void inputDimension (double)
{
	double sphereRadius = 0.0;
	
	cout << "Enter radius: ";
	cin >> sphereRadius;     // what do you want to do here, i assume you want something here ?
}
Last edited on
Ohhhhhh thank you, I've made the edits now it's asking me the correct amount of times and the loop is working just fine, however I'm getting garbage back as the answer though? Like 0A000789 or something to that affect... I'm assuming it's something wrong with my calculation functions?? But what would give me answers like that??
ok... played with it some more and now I'm getting 0.000 as my answer...consistently...which is better than utter nonsense but still incorrect lol
I'm just getting 0.000 as my answer correct precision, but no matter what I get 0.000, here's my updated code:

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
#include <iostream>
#include <iomanip>
#include <cmath>
#include "Shape.h"
using namespace std;

//Declare Functions
char inputShape();
void inputDimension (double radius);
void inputDimension (double radius, double height);  
char inputCompType();
void performComputation (char charType, double radius);
void performComputation (char shape, char charType, double radius, double height);
double sphere_volume (double radius);
double sphere_surface_area (double radius);
double cylinder_volume (double radius, double height);
double cylinder_surface_area (double radius, double height);
double cone_volume (double radius, double height);
double cone_surface_area (double radius, double height);

int main()
{
	char shape = '0';
	char compType = '0';
    double radius = 0.0;
    double height = 0.0;
   
	// Call inputShape function to request the first shape
    // type or ‘q’ and return result to the shape variable 
	shape = inputShape();

	// Loop until the user specifies QUIT for the shape type
	while (shape != QUIT)
	{	
		// For Spheres:
		if (shape == SPHERE)
		{
			// Call the appropriate inputDimension function
			// to request the radius
			inputDimension(radius);

			// Call the inputCompType function to request 
			// the computation type and return result to 
			// the compType variable
			compType = inputCompType();

			// Call the appropriate performComputation
			// function to compute and display the result
			performComputation (compType, radius);
		}
		// For Cylinders and Cones:
		else if (shape == CYLINDER || shape == CONE)
		{
			// Call the appropriate inputDimension function
			// to request the radius and height
			inputDimension(radius, height);

			// Call the inputCompType function to request 
			// the computation type and return result to 
			// the compType variable
			compType = inputCompType();

			// Call the appropriate performComputation
			// function to compute and display result
			performComputation (shape, compType, radius, height);
		}

		// Call inputShape function to request the next shape 
		// type or ‘q’ and return result to the shape variable  
		shape = inputShape();
	}
    // end loop
	
	if (shape == QUIT)
	{
		cout << "Good bye!" << endl;
	}

    system("pause");
    return 0;
}

//Input Functions
char inputShape()
{
	char input = '0';

	cout << "Select a Shape (1) sphere (2) cylinder (3) cone (q) quit: ";
	cin >> input;

	return input;
}

void inputDimension (double radius)
{
	cout << "Enter radius: ";
	cin >> radius;
}

void inputDimension (double radius, double height)
{
	cout << "Enter radius: ";
	cin >> radius;
	cout << "Enter height: ";
	cin >> height;
}

char inputCompType()
{
	char type = '0';

	cout << "Select a Computation (1) volume (2) surface area: ";
	cin >> type;

	return type;
}

//Computation Functions
void performComputation (char compType, double radius)
{
	if (compType == VOLUME)
	{
		cout << "Volume of sphere is " << setprecision(3) << fixed << sphere_volume(radius) << endl << endl;
	}
	else if (compType == SURFACE_AREA)
	{
		cout << "Surface area of sphere is " << setprecision(3) << fixed << sphere_surface_area(radius) << endl << endl;
	}
}

void performComputation (char shape, char compType, double radius, double height)
{
	if (shape == CYLINDER)
	{
			if (compType == VOLUME)
			{
				cout << "Volume of cylinder is " << setprecision(3) << fixed << cylinder_volume(radius, height) << endl << endl;
			}
			else if (compType == SURFACE_AREA)
			{
				cout << "Surface area of cylinder is " << setprecision(3) << fixed << cylinder_surface_area(radius, height) << endl << endl;
			}
	}
	else if (shape = CONE)
	{
			if (compType == VOLUME)
			{
				cout << "Volume of cone is " << setprecision(3) << fixed << cone_volume(radius, height) << endl << endl;
			}
			else if (compType == SURFACE_AREA)
			{
				cout << "Surface area of cone is " << setprecision(3) << fixed << cone_surface_area(radius, height) << endl << endl;
			}	
	}
}

//Sphere Functions
double sphere_volume(double radius)
{
	double sphere_volume = 0.0;

	sphere_volume = (4.0/3.0) * PI * (pow(radius,3));

	return sphere_volume;
}

double sphere_surface_area(double radius)
{
	double sphere_surface_area = 0.0;

	sphere_surface_area = 4.0 * PI * (pow(radius,2));

	return sphere_surface_area;
}

//Cylinder Functions
double cylinder_volume(double radius, double height)
{
	double cylinder_volume = 0.0;

	cylinder_volume = PI * (pow(radius,2)) * height;

	return cylinder_volume;
}

double cylinder_surface_area(double radius, double height)
{
	double cylinder_surface_area = 0.0;

	cylinder_surface_area = 2.0 * PI * (pow(radius,2)) + 2.0 * PI * radius * height;

	return cylinder_surface_area;
}

//Cone Functions
double cone_volume(double radius, double height)
{
	double cone_volume = 0.0;

	cone_volume = (1.0/3.0) * PI * (pow(radius,2)) * height;

	return cone_volume;
}

double cone_surface_area(double radius, double height)
{
	double cone_surface_area = 0.0;

	cone_surface_area = PI * (pow(radius,2)) + PI * radius * sqrt((pow(radius,2)) + (pow(height,2)));

	return cone_surface_area;
}
my guess is you want to modify radius variable when you pass it as parameter for inputDimension
which won't work because you only pass a copy of the value of it.

You should pass that as a reference instead, in which you pass the actual variable ?, and so any operations inside the function in which it was pass as parameter will modify it.
1
2
3
4
void inputDimension ( double& other_variable_name )
{
   /* your code */
}


and call the function just like an ordinary call

for more info:
http://www.cplusplus.com/doc/tutorial/functions2/


A simple example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

void changeValue ( int& bla )
{
    bla = 10;
}

int main ()
{
    int var = 5;
    std::cout << var << std::endl; // will output 5

    changeValue ( var ); // if you did not pass var as reference the output on the 
                                    // 2nd cout will be 5

    std::cout << var << std::endl; // will output 10

    return 0;
}
Last edited on
also line 144 else if (shape = CONE) should be else if (shape == CONE)
looks to me as though shape is actually a char not a string. shape can equal '1' '2' '3' or 'q' but not the strings you have it checking for.

I am very confused... how is you code working at all? All those strings should be undefined.

ok now I see at the top your header file... my mistake.
Last edited on
Alright sorry, now I am up to speed. Your void function take arguments but can't change the value of the arguments, like radius, for example unless you pass them by reference.

An example... line 94 should be...
void inputDimension (double &radius)

Hope that helps.

Also you will need to change your declarations at the top to match. So line 9 needs to be...
void inputDimension (double &radius);
Last edited on
Ohh wow, I see, so I should use the & when I want to be able to change them. That makes a lot more sense, I for some reason just wasn't getting it the other day, probably due to lack of caffeine... I'll make the edits when I get home tonight, thank you all so much, you've been an enormous help I think I understand where I was going wrong now.
Topic archived. No new replies allowed.