C++: Write / improve function to convert numbers 0 to 1.000.000.000 to German words

Job ID: 35965935

Budget: €8 – €30 EUR

Hello!

I need to convert numbers between 0 and 1.000.000.000 to German words.

I have asked chat.openai, and it has tried something, but it's buggy.ö
I need somebody to fix it.
I think it would be easiest if you are German.

Here is what I have so far:

#include <map>
#include <vector>
#include <cmath>
#include <sstream>
#include <iostream>
#include <locale>
#include <codecvt>

using namespace std;

wstring NumberToWords(wstring u)
{
map<int, wstring> numberWords =
{
{0, L"null"},
{1, L"eins"},
{2, L"zwei"},
{3, L"drei"},
{4, L"vier"},
{5, L"fünf"},
{6, L"sechs"},
{7, L"sieben"},
{8, L"acht"},
{9, L"neun"},
{10, L"zehn"},
{11, L"elf"},
{12, L"zwölf"},
{13, L"dreizehn"},
{14, L"vierzehn"},
{15, L"fünfzehn"},
{16, L"sechzehn"},
{17, L"siebzehn"},
{18, L"achtzehn"},
{19, L"neunzehn"},
{20, L"zwanzig"},
{30, L"dreißig"},
{40, L"vierzig"},
{50, L"fünfzig"},
{60, L"sechzig"},
{70, L"siebzig"},
{80, L"achtzig"},
{90, L"neunzig"},
{100, L"hundert"},
{1000, L"tausend"},
{1000000, L"million"},
{1000000000, L"milliarde"}
};

std::wstring result = L"";
std::wstring number = u;
if (number.empty() || number == L"0")
{
return numberWords[0];
}

std::vector<int> numberParts;
int size = number.size();
int start = size - 3;
int end = size;
while (start >= 0)
{
if (start == 0 && size % 3 != 0)
{
numberParts.push_back(stoi(number.substr(0, end - start)));
break;
}
numberParts.push_back(stoi(number.substr(start, end - start)));
end = start;
start -= 3;
}

for (int i = numberParts.size() - 1; i >= 0; i--)
{
int part = numberParts[i];
if (part >= 100) {
result += numberWords[part / 100];
result += L" ";
result += numberWords[100];
part = part % 100;
if (part > 0) {
result += L" ";
}
}
if (part >= 20)
{
result += numberWords[(part / 10) * 10];
part = part % 10;
if (part > 0) {
result += L" ";
result += numberWords[part];
}
}
else if (part > 0)
{
result += numberWords[part];
}
if (i > 0)
{
result += L" ";
if (part > 0)
{
result += numberWords[1000 * (int)pow(10, i)];
result += L"en";
}
}
}
return result;
}
Related categories: Software Architecture C++ Programming