1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include <map>
#include <string>
#include <iostream>
#include <sstream>
int main()
{
const std::map<std::string, unsigned> digits {{"zero", 0}, {"one", 1}, {"two", 2}, {"three", 3}, {"four", 4}, {"five", 5}, {"six", 6}, {"seven", 7}, {"eight", 8}, {"nine", 9}};
std::string num;
std::cout << "Enter number in words: ";
std::getline(std::cin, num);
std::istringstream iss(num);
std::ostringstream oss;
for (std::string n; iss >> n; )
if (const auto f {digits.find(n)}; f != digits.end())
oss << f->second;
else
oss << n << " - unknown digit word\n";
std::cout << "The number you entered was " << oss.str() << '\n';
}
| |