Program
It takes a numerical input in range [1, 99] and outputs its word equivalent.
Concerns
Procedural approach
General code style
Code
#include <iostream>
void print1to20(int num)
{
switch(num)
{
case 1: std::cout << "one"; break;
case 2: std::cout << "two"; break;
case 3: std::cout << "three"; break;
case 4: std::cout << "four"; break;
case 5: std::cout << "five"; break;
case 6: std::cout << "six"; break;
case 7: std::cout << "seven"; break;
case 8: std::cout << "eight"; break;
case 9: std::cout << "nine"; break;
case 10: std::cout << "ten"; break;
case 11: std::cout << "eleven"; break;
case 12: std::cout << "twelve"; break;
case 13: std::cout << "thirteen"; break;
case 14: std::cout << "fourteen"; break;
case 15: std::cout << "fifteen"; break;
case 16: std::cout << "sixteen"; break;
case 17: std::cout << "seventeen"; break;
case 18: std::cout << "eighteen"; break;
case 19: std::cout << "nineteen"; break;
case 20: std::cout << "twenty"; break;
default: std::cout << "1 to 20 switch error";
}
}
void printTens(int num)
{
switch(num)
{
case 20: std::cout << "twenty"; break;
case 30: std::cout << "thirty"; break;
case 40: std::cout << "fourty"; break;
case 50: std::cout << "fifty"; break;
case 60: std::cout << "sixty"; break;
case 70: std::cout << "seventy"; break;
case 80: std::cout << "eighty"; break;
case 90: std::cout << "ninety"; break;
default: std::cout << "tens switch error";
}
}
int main()
{
int num;
do
{
std::cout << "Insert a number 1-99: ";
std::cin >> num;
} while(num < 1 || num > 99);
int num_a, num_b; // num = (a)(b);
num_a = num - num%10;
num_b = num%10;
std::cout << "Your number is ";
if(num <= 20)
print1to20(num);
else
{
printTens(num_a);
std::cout << ' ';
print1to20(num_b);
}
return 0;
}