Quick Question About Simple Measurement Program

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.

His example:
Input 1: 5 (feet) 7 (inches)
Input 2: 6 (feet) 8 (inches)

Output 1: 12 ft. 3 in.
Output 2: 12.25 ft.

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.

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
 #include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace 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).

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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	int temp;
	int inches = 0;

	cout << "Enter the feet of the first measurement:  ";
	cin >> temp;
	inches += 12 * temp;

	cout << "Enter the inches of the first measurement:  ";
	cin >> temp;
	inches += temp;

	cout << "Enter the feet of the second measurement:  ";
	cin >> temp;
	inches += temp * 12;
	
	cout << "Enter the feet of the second measurement:  ";
	cin >> temp;
	inches += temp;

	cout << "Sum of two measurements:  " << inches / 12 << "ft. " << inches % 12 << "in.\n";
	cout << "Sum of two measurements:  " << setiosflags(ios::fixed) << setprecision(2) << (double)inches / 12.0 << "ft.\n";
}


*Note: This follows almost none of your requirements, but it's quick and efficient.
Topic archived. No new replies allowed.