This is our first assignment in my Intro Comp Sci class and my first real experience writing any kind of code other than "Hello World". Our teacher wants us to create a program that will take two measurements expressed in feet and inches, and return and output of their sum. He has a few other requests (using iomanip and certain formats like setw, setprecision, etc.) which I have been able to implement.
The only problem that I seem to be having is getting the program to display the output as a fraction of a foot.
I know there is something easy that I am forgetting to add but I can't seem to locate it in my book. If you have any suggestions or can point me in the right direction, that would be great. Thank you!
P.S. This is the code BEFORE I added comments. I wasn't at the same computer that I saved my comments on. My apologies.
#include <cstdlib>
#include <iostream>
#include <iomanip>
usingnamespace std;
int main(int argc, char *argv[])
{
int feet, inches, ft_1, in_1, ft_2, in_2, temp_in;
cout <<"Welcome to the World's Most Comprehensive 2-Dimensional Shape Analyzer"<<endl;
cout <<"\nThis program will take two dimensions expressed in feet and inches, \n calculate the sum, and produce a final measurement."<<endl;
cout <<"\nPlease enter the units of the first measurement."<<endl;
cout <<"\nFeet:"<<endl;
cin>>ft_1;
cout <<"\nInches"<<endl;
cin>>in_1;
cout <<"Please enter the units of the second measurement measurement."<<endl;
cout <<"\nFeet:"<<endl;
cin>>ft_2;
cout <<"\nInches"<<endl;
cin>>in_2;
feet = ft_1+ft_2;
inches = in_1 + in_2;
temp_in = inches % 12;
feet += (inches - temp_in) / 12;
cout << "The total sum of the two measurements expressed in feet and inches is:"<<endl;
cout << setw(4) << fixed << setprecision(2) << showpoint << feet <<"ft.";
cout << setw(4) << fixed << setprecision(2) << showpoint << temp_in <<"in."<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Use one temporary variable to get all the information, and one variable that holds inches. That will quickly simplify your code. This is how I would do it (quick fix).