Please help!!!! this is due at midnight!!!

Okay i am having problems with this code. for example if i wish to convert 5ft 10.5 inches to meters and cm the programs doesnt give me a value instead it just goes crazy on me....Please help me identify the problem and as well provide me witht the code necessary to fix it.....i would be very apriciative of this.....

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
#include <iostream>
using namespace std;

void convert_lengths();
void convert_weights();

int round(double x);
//rounds off decimal places
void weight_to_metric();
double input_the_lbsandoz();
double convert_the_lbsandoz();
void output_the_kgandg();
//conversion functions for us to metric weight
void weight_to_us();
double input_the_kgandg();
double convert_the_kgandg();
void output_the_lbsandoz();
//conversion functions for metric to us weight
void length_to_metric();
double input_the_ftandin();
double convert_the_ftandin();
void output_the_mandcm();
//conversion functions for us to metric length
void length_to_us();
double input_the_mandcm();
double convert_the_msandcm();
void output_the_ftandin();
//conversion functions for metric to us length


int main() {
	int ans;
	do {
	cout << "This program converts lengths and weights." << endl;
	cout << "Press 1 to convert lengths." << endl;
	cout << "Press 2 to convert weights." << endl;
	cout << "Press 0 to end the program." << endl;
	cout << "Enter: ";
	cin >> ans;
	if (ans == 1) {
		convert_lengths();
	}
	else if (ans == 2) {
		convert_weights();
	}
	else if (ans == 0)
		cout << "Program terminated." << endl;
	else 
		cout << "Please enter a vaild choice." << endl;
	} while (ans > 2 || ans < 0);
	return 0;
}

void convert_lengths() {
	int answer;
	do {
	cout << endl;
	cout << "You have chosen to convert lengths." << endl;
	cout << "If you would like to convert ft./in. to m/cm press 1." << endl;
	cout << "If you would like to convert m/cm to ft./in. press 2." << endl; 
	cout << "If you would like to stop converting lengths press 0." << endl;
	cout << "Enter: ";
	cin >> answer;
	if (answer == 1)
		length_to_metric();			//conversion function ft/in to m/cm
	else if (answer == 2)
		length_to_us();				//conversion function m/cm to ft/in
	else if (answer == 0)
		main();
	else 
		cout << "Please enter a valid choice." << endl;
	} while (answer < 0 || answer > 2);
}

void convert_weights() {
	int ans;
	do {
	cout << endl;
	cout << "If you would like to convert lbs./oz. to kg/g press 1." << endl;
	cout << "If you would like to convert kg/g to lbs./oz. press 2." << endl;
	cout << "If you would like to stop converting weights press 0." << endl;
	cout << "Enter: ";
	cin >> ans;
	if (ans == 1)
		weight_to_metric();			//weight to metric function
	else if (ans == 2) 
		weight_to_us();				//weight to us function
	else if (ans == 0)
		main();
	else 
		cout << "Please enter a valid choice." << endl;
	} while (ans < 0 || ans > 2); 
}

void length_to_metric() {
	cout << "You have chosen to convert from us to metric lengths." << endl;
	output_the_mandcm();
}

void length_to_us() {
	cout << "You have chosen to convert from metric to us." << endl;
	output_the_ftandin();
}

void weight_to_metric() {
	cout << "You have chosen to convert from us to metric weights." << endl;
	output_the_kgandg();
}

void weight_to_us() {
	cout << "You have chosen to convert from metric to us." << endl;
	output_the_lbsandoz();
}

double input_the_ftandin() {
	int feet, inch;
	double totalin;
	cout << "Please enter the number of feet: ";
	cin >> feet;
	cout << "Please enter the number of inches: ";
	cin >> inch;
	totalin = inch + (feet * 12);
	return totalin;
}

double convert_the_ftandin() {
	double totalcm, totalmeters, totalin = input_the_ftandin();
	totalcm = totalin * 2.54;
	totalmeters = totalcm / 100;
	return totalmeters;
}

void output_the_mandcm() {
	double totalmeters = convert_the_ftandin(), totalcm;

	totalcm = round(totalmeters * 100) % 100;
	totalmeters = floor(totalmeters);
	cout << "The total meters are: " << totalmeters << endl;
	cout << "The total centimeters are: " << totalcm << endl;
	main();
}

double input_the_mandcm() {
	double meters, cm;
	cout << "Please enter the number of meters: ";
	cin >> meters;
	cout << "Please enter the number of cm: ";
	cin >> cm;
	meters = meters + (cm / 100);
	return meters;
}

double convert_the_mandcm() {
	double totalfeet, totalmeters = input_the_mandcm();
	totalfeet = totalmeters / .3048; 
	return totalfeet;
}

void output_the_ftandin() {
	double totalinches, totalfeet = convert_the_mandcm();
	totalinches = totalfeet * 12;
	totalinches = round(totalinches) % 12; 
	cout << "The total feet are: " << floor(totalfeet) << endl;
	cout << "The total inches are: " << totalinches << endl;
	main();
}

double input_the_lbsandoz() {
	double lbs, oz;
	cout << "Please enter the number of lbs: ";
	cin >> lbs;
	cout << "Please enter the number of oz.: ";
	cin >> oz;
	lbs = lbs + (oz / 16);
	return lbs;
}

double convert_the_lbsandoz() {
	double totallbs = input_the_lbsandoz(), totalkg, totalgrams;
	totalkg = totallbs / 2.2046;
	totalgrams = totalkg * 1000;
	return totalgrams;
}

void output_the_kgandg() {
	double totalkg, totalgrams = convert_the_lbsandoz();
	totalkg = floor(totalgrams / 1000);
	totalgrams = round(totalgrams) % 1000;
	cout << "The total kilograms are: " << totalkg << endl;
	cout << "The total grams are: " << totalgrams << endl;
	main();
}

double input_the_kgandg() {
	double kg, g;
	cout << "Please enter the number of Kg: ";
	cin >> kg;
	cout << " Please enter the number of grams: ";
	cin >> g;
	kg = kg + (g / 1000);
	return kg;
}

double convert_the_kgandg() {
	double totallbs, totalkg = input_the_kgandg();
	totallbs = 2.2046 * totalkg;
	return totallbs;
}

void output_the_lbsandoz() {
	double totallbs = convert_the_kgandg(), totaloz;
	totaloz = totallbs * 16;
	totaloz = round(totaloz) % 16;
	totallbs = floor(totallbs);
	cout << "The total pounds are: " << totallbs << endl;
	cout << "The total ounces are: " << totaloz << endl;
	main();
}

int round(double x) {
	if(x - floor(x) < 0.5)
		return floor(x);
	else
		return ceil(x);
}
Actually this program doesn't even compile for me. Maybe you should make sure your compiler is generating all possible warnings. And if you are getting warnings, Fix Them never ignore them.

main.cpp||In function ‘void convert_lengths()’:|
main.cpp|69|error: ISO C++ forbids taking address of function ‘::main’ [-pedantic]|
main.cpp||In function ‘void convert_weights()’:|
main.cpp|89|error: ISO C++ forbids taking address of function ‘::main’ [-pedantic]|
main.cpp||In function ‘void output_the_mandcm()’:|
main.cpp|137|error: ‘floor’ was not declared in this scope|
main.cpp|140|error: ISO C++ forbids taking address of function ‘::main’ [-pedantic]|
main.cpp||In function ‘double convert_the_mandcm()’:|
main.cpp|153|warning: no previous declaration for ‘double convert_the_mandcm()’ [-Wmissing-declarations]|
main.cpp||In function ‘void output_the_ftandin()’:|
main.cpp|163|error: ‘floor’ was not declared in this scope|
main.cpp|165|error: ISO C++ forbids taking address of function ‘::main’ [-pedantic]|
main.cpp||In function ‘void output_the_kgandg()’:|
main.cpp|187|error: ‘floor’ was not declared in this scope|
main.cpp|191|error: ISO C++ forbids taking address of function ‘::main’ [-pedantic]|
main.cpp||In function ‘void output_the_lbsandoz()’:|
main.cpp|214|error: ‘floor’ was not declared in this scope|
main.cpp|217|error: ISO C++ forbids taking address of function ‘::main’ [-pedantic]|
main.cpp||In function ‘int round(double)’:|
main.cpp|221|error: ‘floor’ was not declared in this scope|
main.cpp|224|error: ‘ceil’ was not declared in this scope|
main.cpp|225|warning: control reaches end of non-void function [-Wreturn-type]|


You can't recursively call main(). This function (main) can't be called by any function including main() in a C++ program.

okay i fixed that.....i re-entered the 5ft and 10.5 inches.....my output is 1 meter and 78cm........but i cant get my program to put out decimal places the value should be around 79.07cm. and i am getting an error i.e. -->

1>c:\comp141\program4c\program4c\program4c.cpp(229): warning C4244: 'return' : conversion from 'double' to 'int', possible loss of data
1>c:\comp141\program4c\program4c\program4c.cpp(231): warning C4244: 'return' : conversion from 'double' to 'int', possible loss of data
and this is refering to this section of code

1
2
3
4
5
6
int round(double x) {
	if(x - floor(x) < 0.5)
		return floor(x);
	else
		return ceil(x);
}
closed account (Dy7SLyTq)
its not an error its a warning so it compiled, but what its saying is that you said at the beginning of the function it will return an int and then you try to return a double. its warning that you might not get the answer you expected
i see.....how can i fix this problem? wat code do i need to add or change?
closed account (Dy7SLyTq)
return (int) yourDoubleVariable; there are better ways but i think that should work
Topic archived. No new replies allowed.