2
\$\begingroup\$

After reading some beginner textbooks on C++ programming, I decided to write a trivial unit conversion program. Since I am working on Windows, the console windows still use codepage 850 by default, and I wanted to output the Euro currency sign. I also wanted to get the error handling right, since that's one of the topics beginner books often ignore. And, since I'm German, I wanted to use the appropriate decimal separators.

Now my "trivial" program has become quite large. Does it have to be that large? Is there some way to make it smaller, without giving up one of its features?

#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <locale>
#include <string>

#include <Windows.h>

class SwitchToUtf8 {
private:
    UINT in;
    UINT out;
public:
    SwitchToUtf8() {
        in = GetConsoleCP();
        out = GetConsoleOutputCP();
        SetConsoleCP(65001);
        SetConsoleOutputCP(65001);
    }
    ~SwitchToUtf8() {
        SetConsoleCP(in);
        SetConsoleOutputCP(out);
    }
};

int main() {
    SwitchToUtf8 temporarily;

    std::locale::global(std::locale(""));
    std::wcin.imbue(std::locale());
    std::wcout.imbue(std::locale());
    std::wcerr.imbue(std::locale());

    const double price_per_liter = 1.31;

    std::wcout << "How much gas did you refuel? ";
    std::wcin.sync();

    double liter;
    if (std::wcin >> liter) {
        double price = liter * price_per_liter;
        std::ios::fmtflags saved_flags(std::wcout.flags());
        std::wcout << "That sums up to " << std::fixed << std::setprecision(2) << price << " €.\n" << saved_flags;
        std::wcout << price << "\n";
    }
    else {
        std::wcin.clear();

        std::wstring line;
        if (std::getline(std::wcin, line)) {
            std::wcerr << "Input error: " << line << "\n";
        }
        else {
            std::wcerr << "Other error\n";
        }
    }
}
\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

Firstly, I want to commend you on the neatness of your code. Appropriate spacing and newlines used make it very readable, even if some lines are slightly long. Also, you used the std:: namespace tag over the using namespace std; line, which is again, better practice in C++.

Now to answer your question, this code is not that large. When you compare it to the simplest possible program for a gas calculator, you must realize that your code has error-handling capabilities, it adds decimal seperators according to the German style, and it adds the ability to output the Euro sign to the screen. As you add more features, your code is naturally expected to get bigger, and once again, there is nothing that I can see in your code that is unnecessary, redundant, or unrequired (I'm not an expert on GetConsoleCP(), GetConsoleOutputCP(), etc..., so I may be wrong here).

**This is my general viewpoint of the code, and any further input for discussion is welcomed.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.