Ok, I'm doing the exercises at the end of a chapter. One of the questions is this:
Assuming there are 7.481 gallons in a cubic foot, write a program that asks the user to enter a number of gallons, and then displays the equivalent in cubic feet.
This was my solution to the problem (and it works) is:
CODE
//gallon.cpp
//tests my knowlege, converts gallons to cubic feet
#include <iostream>
#include <conio.h>
using namespace std;
int main() {
double gallon;
cout << "Enter the number of gallons: ";
cin >> gallon;
gallon /= 7.481;
cout << "Volume in cubic feet is: " << gallon;
getch();
return 0;
}
My book did it a little different. It used 2 separate variables. Here it is:
CODE
#include <iostream>
#include <conio.h>
using namespace std;
int main() {
float gallons, cufeet;
cout << "\nEnter quantity in gallons: ";
cin >> gallons;
cufeet = gallons / 7.481;
cout << "Equivalent in cubic feet is " << cufeet << endl;
getch();
return 0;
}
(I didn't compile this version, so sorry for any errors.) I was wondering, is there any inherent advantage to one over the other? Just wanted to ask and make sure my way wasn't a 'bad' way to do it, and I was also curious if the way I did it might be superior in some situations. Thanks!
Edit: Oh, I forgot to ask. I was also wondering if there's a way to use the /= arithmetic operator to divide the other way if you catch my drift. Switch from x/y to y/x.
This post has been edited by Guydin: 7 Jun, 2007 - 09:26 PM