This is what I have so far and I can't figure out how to get the converted length in my output file so that 312 inches converts to 3 yards, 1 foot, and 3 inches.

#include <iostream.h>
#include <fstream.h>


using namespace std;


int main()
{
ofstream outfile;


int centimeters;
int inches;
int yards;
int feet;


outfile.open("lengthout.txt");


cout << "Enter length in centimeters: ";
cin >> centimeters;
cout << endl;
cout << "The number entered was " << centimeters
<< " for centimeters. " << endl;
inches = centimeters * 0.3937;



outfile << "Program #3 Output by " << endl;
outfile << "Input Length in centimeters: " << centimeters << endl;
outfile << "Length in inches: " << inches << endl;
outfile << "Converted length: "


return 0;
}

Dani AI

Generated

A few concrete fixes that tie together 's and 's suggestions: keep the conversion in a floating type so the decimal part isn't lost, then convert to an integer number of inches and split that total into yards, feet and inches with integer division and the modulus operator.

Algorithm (safe and simple)

  • Read the input into a double.
  • Convert to inches using the exact relationship 1 inch = 2.54 cm (so 1 cm = 1/2.54).
  • Round the resulting inches to the nearest whole inch (or choose truncation if you prefer).
  • Compute yards = total_inches / 36; remainder = total_inches % 36; feet = remainder / 12; inches = remainder % 12.

Example implementation (writes to lengthout.txt):

#include <iostream>
#include <fstream>
#include <cmath>

int main() {
    std::ofstream outfile("lengthout.txt");
    double cm;
    if (!(std::cin >> cm)) return 1;

    double inches_d = cm * (1.0 / 2.54);
    long total_inches = static_cast<long>(inches_d + 0.5); // round to nearest inch

    long yards = total_inches / 36;
    long rem = total_inches % 36;
    long feet = rem / 12;
    long inches = rem % 12;

    outfile << "Input Length in centimeters: " << cm << '\n';
    outfile << "Converted length: " << yards << " yards, "
            << feet << " feet, " << inches << " inches\n";
    return 0;
}

Notes and troubleshooting

  • Using int for the intermediate conversion truncates the fractional inches; that was the source of the bug in 's code.
  • Choose how to handle fractions: the snippet rounds to nearest inch. To preserve fractional inches, keep a double and print with std::fixed and std::setprecision.
  • Always check outfile.is_open() in production code and handle negative inputs if relevant.

Recommended Answers

All 2 Replies

You shouldn't use integers for math if you are multiplying numbers with decimals. Use floats or doubles...

im not sure, but you may want to try using the modulus operator "%".
for example... to turn feet into yards and feet you could enter

int yards, feet;
yards = feet / 3;
feet = feet % 3;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.