Struggling with a beginners homework assignment if anyone can help!

I am working on homework and have spent a few hours but can't figure out what is missing from this code. I would appreciate any help to point in the right direction to resolve this.

I need to write a code that converts inches into the equivalent yards, feet, and inches. When I run the program it says I haven't defined yards. I thought I did but clearly am missing something but after hours of online searching while looking through my textbook I just don't understand what's wrong.
Each Google search takes me to more difficult ways to code this answer but I need the basics as I'm in an intro class to C++.
Thank you to anyone that can help!

#include <iostream>
using namespace std;

int main( )
{
int userInches;
int feet, yards, inches;

cout << " Enter distance in inches:\n";
cin >> userInches;

feet = inches / 12;
inches = userInches % 12;
yards = feet / 3;
feet = inches % 3;

// converts the number of inches to yards, feet, and inches

cout << "The distance of " << inches << " is the same as the distance of " << yards << " yards " << "," << feet << " feet " << " , and " << inches << " inches." << endl;

return 0;
}

When i run the code i get:

Enter the distance in inches:
140
The distance of 8 is the same as the distance of 0 yards ,2 feet , and 8 inches.

Process returned 0 (0x0) execution time : 24.600 s
Press any key to continue.

but it needs to read as:
Enter the distance in inches:

140

The distance of 140 inches is the same as the distance of 3 yards, 2 feet and 8 inches

Last edited on
On line 13 of your code inches has an undefined value, perhaps it should be userInches instead of inches?

Please learn to use code tags when posting code.


Thank you for that clarification. I'm on week 1 of coding and this site so I don't know what code tags are but ill figure it out I guess.
When you reply, on your right there are two <>. Click that and paste your code in between.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main() {
int userInches{};
int feet, yards, inches;

cout << "Enter distance in inches:\n";
cin >> userInches;

feet = userInches / 12;
inches = userInches % 12;
yards = feet / 3;
feet = userInches % 3;

// converts the number of inches to yards, feet, and inches

cout << "The distance of " << userInches 
<< " is the same as the distance of " 
<< yards << " yards" << ", " << feet 
<< " feet" << " and " << inches 
<< " inches." << endl;
  return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main() {
	int userInches {};

	std::cout << "Enter distance in inches: ";
	std::cin >> userInches;

	const auto feet {userInches / 12};
	const auto inches {userInches % 12};
	const auto yards {feet / 3};

	std:: cout << "The distance of " << userInches << " inches is the same as the distance of "
		<< yards << " yards, " << feet % 3 << " feet, "
		<< inches << " inches.\n";
}

Another way is to write the program only after considering the problem on paper first, perhaps with some pseudocode:

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
#include <iostream>
using namespace std;

int main( )
{
    int feet{0}, yards{0}, inches{0};
    
    int left_over_inches{0};
    
    cout << "Enter distance in inches: ";
    cin >> inches;
    
    yards = inches/36;
    left_over_inches = inches % 36;
    
    feet = left_over_inches/12;
    left_over_inches = left_over_inches%12;
    
    cout
    << "The distance of " << inches
    << " inches is the same as the distance of " << yards << " yards, "
    << feet << " feet and " << left_over_inches << " inches." << endl;
    
    return 0;
}


Enter distance in inches: 140
The distance of 140 inches is the same as the distance of 3 yards, 2 feet and 8 inches.
Program ended with exit code: 0
Topic archived. No new replies allowed.