im stuck on homework i need some help

closed account (ypXoE3v7)
I'm stuck on my homework. I need to write a program that when you input a number of inches it gives you the miles, yards feet and inches.

Last edited on
Here is one approach, very similar to your commented code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int constexpr in_per_mi = 63360;
int constexpr in_per_yd = 36;
int constexpr in_per_ft = 12;

#include <iostream>
int main() {
  std::cout << "Enter inches: ";
  int inches = 0; std::cin >> inches;

  int const miles = inches / in_per_mi; inches %= in_per_mi;
  int const yards = inches / in_per_yd; inches %= in_per_yd;
  int const feet  = inches / in_per_ft; inches %= in_per_ft;

  std::cout << "miles: "<< miles << "\nyards: " << yards
            << "\nfeet: " << feet << "\ninches: " << inches << "\n";
}
closed account (ypXoE3v7)
ill try to figure your code out because I'm barely a beginner and I want to learn what each of those things mean.
It helps to read the manual :+)

http://en.cppreference.com/w/cpp/language/constexpr

There is also reference, tutorials and articles at the top left of this page. You might find it easier to read, but sometimes things are harder to find. cppreference is authoritative but quite technical.
closed account (ypXoE3v7)
I'm sorry to ask this but is there any other way of writing it. I read what each thing means but I don't quite understand it. I appreciate all the help I get.
closed account (48T7M4Gy)
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
37
38
#include <iostream>

using namespace std;

int main()
{
    int inches = 0;
    int feet = 0;
    int yards = 0;
    int miles = 0;
    
    int number = 0;
    
    cout << "Enter inches: ";
    cin >> number;
    
    /*
      12 inches per foot
      36 inches per yard
     63360 inches per mile
     */
    
    // USE MODULAR ARITHMETIC - CHECK THE TUTORIALS
    
    // GET THE MILES
    miles = number / 63360;  // Whole miles
    number = number % 63360; // number is now the leftover inches
    
    // GET THE YARDS
    yards = number / 36;  // Whole yards
    number = number % 36; // number is now revised leftover inches
    
    
    // YOU CAN DO THE REST
    
    cout << miles << " mile " << yards << " yards " << number << " inches (leftover)" << endl;
    return 0;
}
closed account (48T7M4Gy)
A few tips:
1. You are best off by working out a simple example on paper before you write code. You get a clear picture of what needs to be done. That's what pseudocode is all about. Remember, all the machine ever does is what you can do manually - just more reliably (probably) and quicker (probably).

2. Make sure you understand the difference between types - double, int etc because dividing integer values can give you unexpected results. eg in integers, 1/2 is 0 not 0.5

3. The tutorials on this site are useful.
eg http://www.cplusplus.com/doc/tutorial/operators/
closed account (ypXoE3v7)
thank you all so much. after two days with struggling I managed to figure out how to do it and understand each step. again thank you all.
Topic archived. No new replies allowed.