Search code examples
c++vectorinputintegeroutput

C++: Outputting the original input rather than the modified input


//units

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>

int main()
{
    double cm;
    double m = 100.0;
    double in = 2.54;
    double ft = 30;

    std::vector<double> numbers;

    std::cout << "Please enter two doubles with their units (cm, m, in, ft): ";
    double input = 0;
    std::string unit = "";
    while(std::cin >> input >> unit)
    {
        if (unit == "m")
        {
            double inputInCm = input*m;
            numbers.push_back(inputInCm);
        }
        else if (unit == "ft")
        {
            double inputInFt = input*m;
            numbers.push_back(inputInFt);
        }
        else if (unit == "in")
        {
            input *= in;
            numbers.push_back(input);
        }
        else if (unit == "cm")
        {
            numbers.push_back(input);
        }
        else
            std::cout << "This is not a valid unit/you did not enter a unit!\n";
    }

    sort(numbers.begin(), numbers.end());

    if(numbers[0] == numbers[1])
        std::cout << "These numbers are equal.\n";
    else if(numbers[1] - numbers[0] < 1.0/100)
        std::cout << "Thesee numbers are almost equal.\n";
    else
        std::cout << "The smaller value is: " << numbers[0] << ".\n"
            << "The bigger value is: " << numbers[1] << ".\n";
}

This program collects two numbers then returns which value is bigger. However, with regards to the units, when I collect them into a vector it changes their value into the "cm" so I can compare their size. At the end, when I am printing "The smallest value is: ", it returns the modified value. How do I print the original value instead? Thanks


Solution

  • I would recommend creating a custom structure and implementing operators on that structure. Check out example bellow:

    //units
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <cmath>
    
    struct measurement_t
    {
        double value;
        std::string unit;
        
        double to_cm()
        {
            if (unit == "m")
                return 100.0 * value;
            else if (unit == "ft")
                return 30.0 * value;
            else if (unit == "in")
                return 2.54 * value;
            else
                return value;
        }
    };
    
    bool operator<(measurement_t meas1, measurement_t meas2)
    {
        return meas1.to_cm() < meas2.to_cm();
    }
    
    bool operator==(measurement_t meas1, measurement_t meas2)
    {
        return meas1.to_cm() == meas2.to_cm();
    }
    
    std::ostream& operator<< (std::ostream& os, measurement_t meas)
    {
        os << meas.value << " " << meas.unit;
        return os;
    }
    
    int main()
    {
        std::vector<measurement_t> numbers;
    
        std::cout << "Please enter two doubles with their units (cm, m, in, ft): ";
        double input = 0;
        std::string unit = "";
        while(std::cin >> input >> unit)
        {
            if (unit == "m" || unit == "ft" || unit == "in" || unit == "cm")
            {
                measurement_t meas {input, unit};
                numbers.push_back(meas);
            }
            else
            {
                std::cout << "This is not a valid unit/you did not enter a unit!\n";
            }
        }
    
        sort(numbers.begin(), numbers.end());
    
        if(numbers[0] == numbers[1])
            std::cout << "These numbers are equal.\n";
        else if(numbers[1].to_cm() - numbers[0].to_cm() < 1.0/100)
            std::cout << "Thesee numbers are almost equal.\n";
        else
            std::cout << "The smaller value is: " << numbers[0] << ".\n"
                << "The bigger value is: " << numbers[1] << ".\n";
    }
    

    This way you solve your problem because you always have a value with the unit stored together and you implement printing and sorting capabilities so you can sort measurements as any other floating value.

    Also code is much more easily reused in later development and its easy to expand functionalities as needed.