Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- // ensures no mistakes are made with constants
- const int INCH_PER_FOOT{12};
- const int INCH_PER_YARD{36};
- const int INCH_PER_MILE{63360};
- /*
- this function takes in the user's input for inches, then prints out how many miles, yards, feet, and inches total (its simplified form)
- */
- int main()
- {
- cout << "Enter the number of inches: ";
- int inches;
- cin >> inches;
- cout << inches << endl;
- {
- /*
- math should be right, here. Because int values truncate (not round), the remainder will be able to be re-used for
- the next value. We want it to print regardless of how many miles there are.
- */
- int miles{ inches / INCH_PER_MILE };
- inches = ( inches % INCH_PER_MILE );
- cout << miles << ( ( miles == 1 ) ? " mile" : " miles" ) << endl; // should print "0 miles" if 0 as well, proper grammar
- }
- {
- int yards{ inches / INCH_PER_YARD };
- inches = ( inches % INCH_PER_YARD );
- cout << yards << ( ( yards == 1 ) ? " yard" : " yards" ) << endl;
- }
- {
- int feet{ inches / INCH_PER_FOOT };
- inches = ( inches % INCH_PER_FOOT );
- cout << feet << ( ( feet == 1 ) ? " foot" : " feet" ) << endl;
- }
- // no need for extra scope here, var inches is in the same scope, after all
- cout << inches << ( ( inches > 1 ) ? " inches" : " inch" ) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement