c++ code for beginner for converting feet to meter

nirveshverma -1 Tallied Votes 6K Views Share

hi enjoy it and give feedback

#include <iostream>

using namespace std;

int main()
{
    double f;
    double m;
    
    cout<< " enter Length in Feet";
    cin>>f;
    
    m = f/3.28;  // Convert To Meters
    cout << f << "feet is" << endl << m << "Meter's" << endl;
    
    
    system("pause");
    return 0;
}

Dani AI

Generated

A few easy tweaks can make a beginner feet-to-meters converter more accurate, portable, and user-friendly:

  • Use a named constant (e.g., constexpr double meters_per_foot) and a small conversion function for clarity and reuse. Prefer double over float to reduce rounding error. The internationally defined conversion for the foot is exact; see the NIST guidance on the international foot and the retired U.S. survey foot for context and when you might need to distinguish them (NIST).
  • Format output so results look consistent, e.g., std::fixed with a sensible std::setprecision. See examples of fixed and setprecision on cppreference.
  • Validate input and handle errors gracefully (clear the stream on failure and prompt again) to avoid undefined behavior when a non-number is entered. Stream state handling is documented at cppreference and ignore.
  • Avoid system("pause") (it is non-standard and Windows-only). If you want a pause when running from an IDE, use a portable approach:
#include <limits>
#include <iostream>

std::cout << "Press Enter to exit...";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.get();

For extra polish, consider supporting both directions (meters to feet), rounding the displayed result (not the internal value), and printing units in the output to avoid ambiguity.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Foamgum 0 Newbie Poster

You can improve your code by using an exact conversion instead of the approximate one that you have used.

m = f/3.28;   // This is an approximate conversion.
m = f*0.3048;    // This is an exact conversion.
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.