Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. // ensures no mistakes are made with constants
  6. const int INCH_PER_FOOT{12};
  7. const int INCH_PER_YARD{36};
  8. const int INCH_PER_MILE{63360};
  9.  
  10. /*
  11. this function takes in the user's input for inches, then prints out how many miles, yards, feet, and inches total (its simplified form)
  12. */
  13.  
  14. int main()
  15. {
  16. cout << "Enter the number of inches: ";
  17. int inches;
  18. cin >> inches;
  19. cout << inches << endl;
  20. {
  21. /*
  22. math should be right, here. Because int values truncate (not round), the remainder will be able to be re-used for
  23. the next value. We want it to print regardless of how many miles there are.
  24. */
  25. int miles{ inches / INCH_PER_MILE };
  26. inches = ( inches % INCH_PER_MILE );
  27. cout << miles << ( ( miles == 1 ) ? " mile" : " miles" ) << endl; // should print "0 miles" if 0 as well, proper grammar
  28. }
  29.  
  30. {
  31. int yards{ inches / INCH_PER_YARD };
  32. inches = ( inches % INCH_PER_YARD );
  33. cout << yards << ( ( yards == 1 ) ? " yard" : " yards" ) << endl;
  34. }
  35.  
  36. {
  37. int feet{ inches / INCH_PER_FOOT };
  38. inches = ( inches % INCH_PER_FOOT );
  39. cout << feet << ( ( feet == 1 ) ? " foot" : " feet" ) << endl;
  40. }
  41.  
  42. // no need for extra scope here, var inches is in the same scope, after all
  43. cout << inches << ( ( inches > 1 ) ? " inches" : " inch" ) << endl;
  44.  
  45. return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement