PLEASE with this code!!!!!
can someone help me fine tune this code a little more!? this code converts units and uses pass by ref (which it must) but I need help refining it! how can I improve this code!?
HTML Code:
#include <iostream>
#include <math.h>
using namespace std;
enum ConversionType {LENGTH_TO_METRIC, LENGTH_TO_US, WEIGHT_TO_METRIC, WEIGHT_TO_US};
void convert_lengths();
void convert_weights();
void length_to_metric(ConversionType conversion);
void length_to_us(ConversionType conversion);
void weight_to_metric(ConversionType conversion);
void weight_to_us(ConversionType conversion);
void prompt_user(int& large_unit, double& small_unit, ConversionType conversion);
void convert(int large_unit, double small_unit, int& new_large, double& new_small, ConversionType conversion);
void output_results(int large_before, double small_before, int large_after, double small_after, ConversionType conversion);
int main()
{
int value;
do {
do {
cout <<"Would you like to convert the weight or length:\n"
"Press 1 for Lengths\n"
"Press 2 for Weight or\n"
"Press 0 to terminate program\n";
cin >> value;
if (value != 0 && value != 1 && value != 2)
cout << "Invalid entry." << endl;
} while (value != 0 && value != 1 && value != 2);
switch (value)
{
case 0:
break;
case 1: convert_lengths();
break;
case 2: convert_weights();
break;
}
} while(value != 0);
return 0;
}
void convert_lengths()
{
cout << "We are going to convert lengths for you." << endl;
int value;
do
{
cout << "Would you like to convert:\n"
"Feet/Inches to Meters/Centimeters? If so press 1.\n"
"Meters/Centimeters to Feet/Inches? If so press 2. \n"
"Press 0 to quit.\n";
cin >> value;
if (value !=0 &&value !=1 && value !=2)
cout << "Invalad entry." << endl;
} while (value !=0 &&value !=1 && value !=2);
switch (value)
{
case 0: break;
case 1: length_to_metric(LENGTH_TO_METRIC); break;
case 2: length_to_us(LENGTH_TO_US); break;
}
}
void convert_weights()
{
cout << "We are going to convert weights for you." << endl;
int value;
do
{
cout << "Would you like to convert:\n"
"Pounds/ounces to Kilograms/grams? If so press 1.\n"
"Kilograms/Grams to pounds/ounces? If so press 2.\n"
"Press 0 to quit.\n";
cin >> value;
if (value !=0 &&value !=1 && value !=2)
cout << "Invalad entry." << endl;
} while (value !=0 &&value !=1 && value !=2);
switch (value)
{
case 0: break;
case 1: weight_to_metric(WEIGHT_TO_METRIC); break;
case 2: weight_to_us(WEIGHT_TO_US); break;
}
}
void length_to_metric(ConversionType conversion)
{
int feet, meters;
double inches, centimeters;
cout << "You have chosen to convert Feet/Inches to Meters/Centimeters.\n";
prompt_user(feet, inches, LENGTH_TO_METRIC);
convert(feet, inches, meters, centimeters, LENGTH_TO_METRIC);
output_results(feet, inches, meters, centimeters, LENGTH_TO_METRIC);
}
void length_to_us(ConversionType conversion)
{
int meters, feet;
double centimeters, inches;
cout << "You have chosen to convert Meters/Centimeters to Feet/Inches.\n";
prompt_user(meters, centimeters, LENGTH_TO_US);
convert(meters, centimeters, feet, inches, LENGTH_TO_US);
output_results(meters, centimeters, feet, inches, LENGTH_TO_US);
}
void weight_to_metric(ConversionType conversion)
{
int pounds, kilograms;
double ounces, grams;
cout << "You have chosen to convert Pounds/Ounces to Kilograms/Grams.\n";
prompt_user(pounds, ounces, WEIGHT_TO_METRIC);
convert(pounds, ounces, kilograms, grams, WEIGHT_TO_METRIC);
output_results(pounds, ounces, kilograms, grams, WEIGHT_TO_METRIC);
}
void weight_to_us(ConversionType conversion)
{
int kilograms, pounds;
double grams, ounces;
cout << "You have chosen to convert Kilograms/Grams to Pounds/Ounces.\n";
prompt_user(kilograms, grams, WEIGHT_TO_US);
convert(kilograms, grams, pounds, ounces, WEIGHT_TO_US);
output_results(kilograms, grams, pounds, ounces, WEIGHT_TO_US);
}
void prompt_user(int& bigger_unit, double& smaller_unit, ConversionType conversion)
{
switch (conversion) {
case LENGTH_TO_METRIC:
cout << "Please enter the length:\n"
"Feet(integer): ";
cin >> static_cast<int>(bigger_unit);
cin.sync();
cout << "Inches(decimal): ";
cin >> static_cast<double>(smaller_unit);
cin.sync();
break;
case LENGTH_TO_US:
cout << "Please enter the length:\n"
"Meters(integer): ";
cin >> static_cast<int>(bigger_unit);
cin.sync();
cout << "Centimeters(decimal): ";
cin >> static_cast<double>(smaller_unit);
cin.sync();
break;
case WEIGHT_TO_METRIC:
cout << "Please enter the weight:\n"
"Pounds(integer): ";
cin >> static_cast<int>(bigger_unit);
cin.sync();
cout << "Ounces(decimal): ";
cin >> static_cast<double>(smaller_unit);
cin.sync();
break;
case WEIGHT_TO_US:
cout << "Please enter the weight:\n"
"Kilograms(integer): ";
cin >> static_cast<int>(bigger_unit);
cin.sync();
cout << "Grams(decimal): ";
cin >> static_cast<double>(smaller_unit);
cin.sync();
break;
}
}
void convert(int bigger_unit, double smaller_unit, int& new_bigger, double& new_smaller, ConversionType conversion)
{
const double LENGTH_CONV = .3048, SM_L_CONV = 2.54,
WEIGHT_CONV = 2.2046, SM_W_CONV = 28.35;
switch (conversion) {
case LENGTH_TO_METRIC:
new_bigger = bigger_unit * LENGTH_CONV;
new_smaller = smaller_unit * SM_L_CONV;
new_smaller += ((bigger_unit * LENGTH_CONV) - floor(bigger_unit * LENGTH_CONV)) * 100;
while (new_smaller >= 100.0) {
new_bigger++;
new_smaller -= 100;
}
break;
case LENGTH_TO_US:
new_bigger = bigger_unit / LENGTH_CONV;
new_smaller = smaller_unit / SM_L_CONV;
new_smaller += ((bigger_unit / LENGTH_CONV) - floor(bigger_unit / LENGTH_CONV)) * 12;
while (new_smaller >= 12.0) {
new_bigger++;
new_smaller -= 12.0;
}
break;
case WEIGHT_TO_METRIC:
new_bigger = bigger_unit / WEIGHT_CONV;
new_smaller = smaller_unit * SM_W_CONV;
new_smaller += ((bigger_unit / WEIGHT_CONV) - floor(bigger_unit / WEIGHT_CONV)) * 1000;
if (new_smaller >= 1000.0) {
new_bigger++;
new_smaller -= 1000;
}
break;
case WEIGHT_TO_US:
new_bigger = bigger_unit * WEIGHT_CONV;
new_smaller = smaller_unit / SM_W_CONV;
new_smaller += ((bigger_unit * WEIGHT_CONV) - floor(bigger_unit * WEIGHT_CONV)) * 16;
if (new_smaller >= 16.0) {
new_bigger++;
new_smaller -= 16;
}
break;
}
}
void output_results(int bigger_before, double smaller_before, int bigger_after, double smaller_after, ConversionType conversion)
{
switch (conversion) {
case LENGTH_TO_METRIC:
cout << bigger_before << " feet " << smaller_before << " inches = " << bigger_after << " meters " << smaller_after << " centimeters." << endl;
break;
case LENGTH_TO_US:
cout << bigger_before << " meters " << smaller_before << " centimeters = " << bigger_after << " feet " << smaller_after << " inches." << endl;
break;
case WEIGHT_TO_METRIC:
cout << bigger_before << " pounds " << smaller_before << " ounces = " << bigger_after << " kilograms " << smaller_after << " grams." << endl;
break;
case WEIGHT_TO_US:
cout << bigger_before << " kilograms " << smaller_before << " grams = " << bigger_after << " pounds " << smaller_after << " ounces." << endl;
break;
}
}
Re: PLEASE with this code!!!!!
Quote:
Originally Posted by
bobr
can someone help me fine tune this code a little more!? this code converts units and uses pass by ref (which it must) but I need help refining it! how can I improve this code!?
HTML Code:
#include <iostream>
#include <math.h>
using namespace std;
...
}
First, please, use Code tags, not HTML ones!
Second, what is not good for you with this code so you need to improve?
Re: PLEASE with this code!!!!!
Quote:
Originally Posted by
bobr
can someone help me fine tune this code a little more!? this code converts units and uses pass by ref (which it must) but I need help refining it! how can I improve this code!?
The code looks quite good (I didn't look in detail), but there are a couple of issues.
You do not check if the user input is valid, i.e. you check the value, but not the type. See Why does my program go into an infinite loop when someone enters an invalid input character?
Code:
int value;
cin >> value;
if (value !=0 &&value !=1 && value !=2)
cout << "Invalad entry." << endl;
When you start copy-pasting code (I know you did, because you copied the typo), you should realize that you are doing something wrong. Instead of copy-pasting, try to refactor the code (e.g. into a function) such that you don't need to repeat the same code.
Code:
void prompt_user(int& bigger_unit, double& smaller_unit, ConversionType conversion)
{
switch (conversion) {
case LENGTH_TO_METRIC:
cout << "Please enter the length:\n"
"Feet(integer): ";
cin >> static_cast<int>(bigger_unit);
...
Why are you casting here? There is no need for it and I'm not sure if it actually does what you want.
Code:
new_bigger = bigger_unit * LENGTH_CONV;
new_smaller = smaller_unit * SM_L_CONV;
new_smaller += ((bigger_unit * LENGTH_CONV) - floor(bigger_unit * LENGTH_CONV)) * 100;
while (new_smaller >= 100.0) {
new_bigger++;
new_smaller -= 100;
}
You don't need a loop here.
Re: PLEASE with this code!!!!!
Thank you for your help. I incorporated a string where the static cast(s) were, but now the program wont compile and I cant figure out how to get it to do so...Can you show me to get rid of the infinite loop and the other loops? I believe this could help the program compile...
HTML Code:
#include <iostream>
#include <math.h>
using namespace std;
enum ConversionType {LENGTH_TO_METRIC, LENGTH_TO_US, WEIGHT_TO_METRIC, WEIGHT_TO_US};
void convert_lengths();
void convert_weights();
void length_to_metric(ConversionType conversion);
void length_to_us(ConversionType conversion);
void weight_to_metric(ConversionType conversion);
void weight_to_us(ConversionType conversion);
void prompt_user(int& large_unit, double& small_unit, ConversionType conversion);
void convert(int large_unit, double small_unit, int& new_large, double& new_small, ConversionType conversion);
void output_results(int large_before, double small_before, int large_after, double small_after, ConversionType conversion);
int main()
{
int value;
do {
do {
cout <<"Would you like to convert the weight or length:\n"
"Press 1 for Lengths\n"
"Press 2 for Weight or\n"
"Press 0 to terminate program\n";
cin >> value;
if (value != 0 && value != 1 && value != 2)
cout << "Invalid entry." << endl;
} while (value != 0 && value != 1 && value != 2);
switch (value)
{
case 0:
break;
case 1: convert_lengths();
break;
case 2: convert_weights();
break;
}
} while(value != 0);
return 0;
}
void convert_lengths()
{
cout << "We are going to convert lengths for you." << endl;
int value;
do
{
cout << "Would you like to convert:\n"
"Feet/Inches to Meters/Centimeters? If so press 1.\n"
"Meters/Centimeters to Feet/Inches? If so press 2. \n"
"Press 0 to quit.\n";
cin >> value;
if (value !=0 &&value !=1 && value !=2)
cout << "Invalad entry." << endl;
} while (value !=0 &&value !=1 && value !=2);
switch (value)
{
case 0: break;
case 1: length_to_metric(LENGTH_TO_METRIC); break;
case 2: length_to_us(LENGTH_TO_US); break;
}
}
void convert_weights()
{
cout << "We are going to convert weights for you." << endl;
int value;
do
{
cout << "Would you like to convert:\n"
"Pounds/ounces to Kilograms/grams? If so press 1.\n"
"Kilograms/Grams to pounds/ounces? If so press 2.\n"
"Press 0 to quit.\n";
cin >> value;
if (value !=0 &&value !=1 && value !=2)
cout << "Invalad entry." << endl;
} while (value !=0 &&value !=1 && value !=2);
switch (value)
{
case 0: break;
case 1: weight_to_metric(WEIGHT_TO_METRIC); break;
case 2: weight_to_us(WEIGHT_TO_US); break;
}
}
void length_to_metric(ConversionType conversion)
{
int feet, meters;
double inches, centimeters;
cout << "You have chosen to convert Feet/Inches to Meters/Centimeters.\n";
prompt_user(feet, inches, LENGTH_TO_METRIC);
convert(feet, inches, meters, centimeters, LENGTH_TO_METRIC);
output_results(feet, inches, meters, centimeters, LENGTH_TO_METRIC);
}
void length_to_us(ConversionType conversion)
{
int meters, feet;
double centimeters, inches;
cout << "You have chosen to convert Meters/Centimeters to Feet/Inches.\n";
prompt_user(meters, centimeters, LENGTH_TO_US);
convert(meters, centimeters, feet, inches, LENGTH_TO_US);
output_results(meters, centimeters, feet, inches, LENGTH_TO_US);
}
void weight_to_metric(ConversionType conversion)
{
int pounds, kilograms;
double ounces, grams;
cout << "You have chosen to convert Pounds/Ounces to Kilograms/Grams.\n";
prompt_user(pounds, ounces, WEIGHT_TO_METRIC);
convert(pounds, ounces, kilograms, grams, WEIGHT_TO_METRIC);
output_results(pounds, ounces, kilograms, grams, WEIGHT_TO_METRIC);
}
void weight_to_us(ConversionType conversion)
{
int kilograms, pounds;
double grams, ounces;
cout << "You have chosen to convert Kilograms/Grams to Pounds/Ounces.\n";
prompt_user(kilograms, grams, WEIGHT_TO_US);
convert(kilograms, grams, pounds, ounces, WEIGHT_TO_US);
output_results(kilograms, grams, pounds, ounces, WEIGHT_TO_US);
}
void prompt_user(int& bigger_unit, double& smaller_unit, ConversionType conversion)
{
string type_str, bigger_str, smaller_str;
switch (conversion) {
case LENGTH_TO_METRIC:
type_str = "length";
bigger_str = "feet";
smaller_str = "inches";
break;
case LENGTH_TO_US:
type_str = "length";
bigger_str = "meters";
smaller_str = "centimeters";
break;
case WEIGHT_TO_METRIC:
type_str = "weight";
bigger_str = "pounds";
smaller_str = "ounces";
break;
case WEIGHT_TO_US:
type_str = "weight";
bigger_str = "kilograms";
smaller_str = "grams";
break;
}
cout << "Please enter the " << type_str << bigger_str << "(integer): " << endl;
cin >> bigger_unit;
cout << "Please enter the " << type_str << smaller_str << "(decimal): " << endl;
cin >> smaller_unit;
}
void convert(int bigger_unit, double smaller_unit, int& new_bigger, double& new_smaller, ConversionType conversion)
{
const double LENGTH_CONV = .3048, SM_L_CONV = 2.54,
WEIGHT_CONV = 2.2046, SM_W_CONV = 28.35;
switch (conversion) {
case LENGTH_TO_METRIC:
new_bigger = bigger_unit * LENGTH_CONV;
new_smaller = smaller_unit * SM_L_CONV;
new_smaller += ((bigger_unit * LENGTH_CONV) - floor(bigger_unit * LENGTH_CONV)) * 100;
while (new_smaller >= 100.0) {
new_bigger++;
new_smaller -= 100;
}
break;
case LENGTH_TO_US:
new_bigger = bigger_unit / LENGTH_CONV;
new_smaller = smaller_unit / SM_L_CONV;
new_smaller += ((bigger_unit / LENGTH_CONV) - floor(bigger_unit / LENGTH_CONV)) * 12;
while (new_smaller >= 12.0) {
new_bigger++;
new_smaller -= 12.0;
}
break;
case WEIGHT_TO_METRIC:
new_bigger = bigger_unit / WEIGHT_CONV;
new_smaller = smaller_unit * SM_W_CONV;
new_smaller += ((bigger_unit / WEIGHT_CONV) - floor(bigger_unit / WEIGHT_CONV)) * 1000;
if (new_smaller >= 1000.0) {
new_bigger++;
new_smaller -= 1000;
}
break;
case WEIGHT_TO_US:
new_bigger = bigger_unit * WEIGHT_CONV;
new_smaller = smaller_unit / SM_W_CONV;
new_smaller += ((bigger_unit * WEIGHT_CONV) - floor(bigger_unit * WEIGHT_CONV)) * 16;
if (new_smaller >= 16.0) {
new_bigger++;
new_smaller -= 16;
}
break;
}
}
void output_results(int bigger_before, double smaller_before, int bigger_after, double smaller_after, ConversionType conversion)
{
switch (conversion) {
case LENGTH_TO_METRIC:
cout << bigger_before << " feet " << smaller_before << " inches = " << bigger_after << " meters " << smaller_after << " centimeters." << endl;
break;
case LENGTH_TO_US:
cout << bigger_before << " meters " << smaller_before << " centimeters = " << bigger_after << " feet " << smaller_after << " inches." << endl;
break;
case WEIGHT_TO_METRIC:
cout << bigger_before << " pounds " << smaller_before << " ounces = " << bigger_after << " kilograms " << smaller_after << " grams." << endl;
break;
case WEIGHT_TO_US:
cout << bigger_before << " kilograms " << smaller_before << " grams = " << bigger_after << " pounds " << smaller_after << " ounces." << endl;
break;
}
}
Re: PLEASE with this code!!!!!
Why did you use HTML tags again? Did you not read VictorN's post?
Quote:
Originally Posted by
bobr
Thank you for your help. I incorporated a string where the static cast(s) were, but now the program wont compile and I cant figure out how to get it to do so...
Which line is giving you a compiler error and what is the error?
Quote:
Originally Posted by
bobr
Can you show me to get rid of the infinite loop and the other loops?
The link in my previous post gives a full explanation of how to avoid the infinite loops.
Re: PLEASE with this code!!!!!
im sorry I thought I hit the right one. I apologize. and in the code bellow it say that I am converting from const double to type int and there is a possible loss of data which there is....
Code:
void convert(int bigger_unit, double smaller_unit, int& new_bigger, double& new_smaller, ConversionType conversion)
{
const double LENGTH_CONV = .3048, SM_L_CONV = 2.54,
WEIGHT_CONV = 2.2046, SM_W_CONV = 28.35;
switch (conversion) {
case LENGTH_TO_METRIC:
new_bigger = bigger_unit * LENGTH_CONV;
new_smaller = smaller_unit * SM_L_CONV;
new_smaller += ((bigger_unit * LENGTH_CONV) - floor(bigger_unit * LENGTH_CONV)) * 100;
while (new_smaller >= 100.0) {
new_bigger++;
new_smaller -= 100;
}
break;
case LENGTH_TO_US:
new_bigger = bigger_unit / LENGTH_CONV;
new_smaller = smaller_unit / SM_L_CONV;
new_smaller += ((bigger_unit / LENGTH_CONV) - floor(bigger_unit / LENGTH_CONV)) * 12;
while (new_smaller >= 12.0) {
new_bigger++;
new_smaller -= 12.0;
}
break;
case WEIGHT_TO_METRIC:
new_bigger = bigger_unit / WEIGHT_CONV;
new_smaller = smaller_unit * SM_W_CONV;
new_smaller += ((bigger_unit / WEIGHT_CONV) - floor(bigger_unit / WEIGHT_CONV)) * 1000;
if (new_smaller >= 1000.0) {
new_bigger++;
new_smaller -= 1000;
}
break;
case WEIGHT_TO_US:
new_bigger = bigger_unit * WEIGHT_CONV;
new_smaller = smaller_unit / SM_W_CONV;
new_smaller += ((bigger_unit * WEIGHT_CONV) - floor(bigger_unit * WEIGHT_CONV)) * 16;
if (new_smaller >= 16.0) {
new_bigger++;
new_smaller -= 16;
}
break;
}
}
Re: PLEASE with this code!!!!!
Quote:
Originally Posted by
bobr
im sorry I thought I hit the right one. I apologize. and in the code bellow it say that I am converting from const double to type int and there is a possible loss of data ....
Well, compiler is right.
But why are you converting from double to int? Why not just use double types instead of int?
Re: PLEASE with this code!!!!!
That was what I was instructed to do. the larger units must be type int while the smaller units must be of type double. for example, feet must be int while inches must be double.....Is there anyway other way to fix the problem?
Thank you,
john
Re: PLEASE with this code!!!!!
Quote:
Originally Posted by
bobr
That was what I was instructed to do.
If I instructed you to jump up in the air and to stay on the ground at the same time, would that make sense?
Code:
new_bigger = bigger_unit * LENGTH_CONV;
LENGTH_CONV is a double that contains a fractional element (it isn't really a double masquerading as an integer). So why do this multiplication? You know that the value will be truncated, since new_bigger is an int. So what's the purpose of the multiplication here, when you know data will be lost? For us to give you a fix, you need to explain what the intention of that one line is supposed to be.
Second, have you gotten to the section on arrays? The reason why is that your code would be a fraction of the size it is now if you used arrays (and created functions to reduce the redundancy). You have repeated code, with the only difference being a value here and there. What if you had 100 different conversions, and the way to convert is the same except for the multiplying / dividing factor? Write the same code 100 times, but with just different constants being used?
In short, you may be shocked at how tiny this program really could be if written using arrays and better usage of functions.
Regards,
Paul McKenzie
Re: PLEASE with this code!!!!!
I understand Paul thank you for your help...it just what my professor said that he want....Is there anyway to fix the problem? How can I manipulate my code to prevent the loss of data I am experiencing?
Re: PLEASE with this code!!!!!
Quote:
Originally Posted by
bobr
I understand Paul thank you for your help...it just what my professor said that he want....Is there anyway to fix the problem? How can I manipulate my code to prevent the loss of data I am experiencing?
Then you need to work exclusively with the smaller units. For example, if you're using feet and inches, do all your calculations with inches. If you end up with a result of 27.5" for example, you would need to convert that to 2' 3.5", but not until you're ready to output. The important thing here is not to do your feet and inches calculation separately. Just convert the result.
Re: PLEASE with this code!!!!!
Okay I see what your saying....but the how would can I do this so that the user so can still plug in the respectable number of feet and inches but still get the right output in meters and centimeters? would this even solve my const double issue? can anyone show me how the code should look? but thank you :)
Re: PLEASE with this code!!!!!
Quote:
Originally Posted by
bobr
That was what I was instructed to do. the larger units must be type int while the smaller units must be of type double. for example, feet must be int while inches must be double.....Is there anyway other way to fix the problem?
So that something like 6 feet 5 half inches can be converted (int 6 feet, double 5.5 inches) into int x metres and double y.z centimetres
Re: PLEASE with this code!!!!!
Im sorry @2kaud I do not understand what you are saying....I do not follow I apologize....I am a visual learner.....could you show me some code and explain what you mean
Re: PLEASE with this code!!!!!
Quote:
Originally Posted by
bobr
Okay I see what your saying....but the how would can I do this so that the user so can still plug in the respectable number of feet and inches but still get the right output in meters and centimeters? would this even solve my const double issue? can anyone show me how the code should look? but thank you :)
If you're going to be a programmer, you need to be able to figure these things out.
How would you convert feet and inches to inches?
How would you convert inches to millimeters?
How would you convert millimeters to meters and millimeters?
Answer those questions in English before you think about writing code.
Re: PLEASE with this code!!!!!
I know that there is 12inches for every foot and that for every inch there is 2.54 centimeters. and I know that for every foot there is .3048 meters.....
Re: PLEASE with this code!!!!!
Quote:
Originally Posted by
bobr
I know that there is 12inches for every foot and that for every inch there is 2.54 centimeters. and I know that for every foot there is .3048 meters.....
Okay, so what would be the steps involved in converting say 7'3" to meters and centimeters? Just in English, not code.
Re: PLEASE with this code!!!!!
I know that their is 12inches per foot....for every inch their is 2.54 centimeters and for every foot there is .3048 meters...
Re: PLEASE with this code!!!!!
I would multiple 7 feet by .3048 meters and multiply 3inches by 2.54cm
Re: PLEASE with this code!!!!!
Quote:
Originally Posted by
bobr
I would multiple 7 feet by .3048 meters and multiply 3inches by 2.54cm
No. Multiplying the whole number feet by a decimal number is why you're getting compiler warnings in the first place. Remember for this exercise you need to convert everything to inches first.
Again the steps are convert feet and inches to inches.
Convert inches to centimeters.
Convert centimeters to meters and centimeters.
Re: PLEASE with this code!!!!!
okay take my input number of feet and multiply by 12inches.....then take the total number of inches and multiply by 2.54 cm.....then take my total number of cm and multiply by .01 meters
Re: PLEASE with this code!!!!!
Quote:
Originally Posted by
bobr
okay take my input number of feet and multiply by 12inches.....then take the total number of inches and multiply by 2.54 cm.....then take my total number of cm and multiply by .01 meters
Not really. I'm not trying to be a jerk, but you need to understand the process yourself in order to explain it to the computer. I'm sure you know how to do it, but you need to figure out all the steps, and you're missing a few.
Multiplying the feet by 12 gives you 84.
multiply 84 by 2.54 gives you 213.36.
Multiplying that gives you 2.1336, which is not only incorrect, but I don't believe it satisfies the requirement of using an int for meters and a double for centimeters. Further, if you do follow the requirements and keep meters an int, your result will be 2, which is even further for the correct result.
I'll help with the first two steps.
Multiply the number of feet by 12.
Add that number to the number of inches.
Re: PLEASE with this code!!!!!
When programming, the first step is to design the program specifiying the input, output, algorithm, steps required etc. This includes the steps that GCDEF has been asking you re how to do a conversion in English. Once, and only once, the design has been done and checked by walkthrough etc is the design coded. Once coded, it is tested and debugged. If the program doesn't perform as expected then its either a problem coding the design or a problem with the design itself. If the program design is not right, then the coded program will not work properly. As GCDEF has said, you must understand the process and know all the steps before you start coding.
I suggest you take a step back from the code, produce a proper program design that includes all the steps etc. Then when you are happy with the design then code your program from the design.