#include <cmath>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

// Read one whole line so a failed conversion cannot trap the input stream.
bool readNumber(const std::string& prompt, double& value, double low, double high) {
    std::string line;
    while (std::cout << prompt && std::getline(std::cin, line)) {
        std::istringstream input(line);
        char extra = '\0';
        if (input >> value && !(input >> extra) && std::isfinite(value)
            && value >= low && value <= high) {
            return true;
        }
        std::cout << "Enter a finite number in [" << low << ", " << high << "].\n";
    }
    std::cerr << "Input ended before a valid number was supplied.\n";
    return false;
}

std::string colorFor(double wavelengthNm) {
    if (wavelengthNm < 450.0) return "violet";
    if (wavelengthNm < 495.0) return "blue";
    if (wavelengthNm < 570.0) return "green";
    if (wavelengthNm < 590.0) return "yellow";
    if (wavelengthNm < 620.0) return "orange";
    return "red";
}

int main() {
    double wavelengthNm = 0.0;
    if (!readNumber("Wavelength in nm (380 to 750): ", wavelengthNm, 380.0, 750.0)) return 1;
    constexpr double speedOfLight = 299792458.0; // m/s, in vacuum
    const double wavelengthM = wavelengthNm * 1e-9;
    const double frequencyHz = speedOfLight / wavelengthM;
    std::cout << "Approximate color: " << colorFor(wavelengthNm) << '\n'
              << "Frequency in vacuum (Hz): " << std::scientific << std::setprecision(6)
              << frequencyHz << '\n';
}
