Hi everyone,
I have been working on the same problem as mp252 from an earlier thread and 4 days later, some hair pulling, a bit of lost sleep, and now, a big sigh of relief, I appear to have it working. Mine only goes from 0-9999 though as I must move on!
Code:
#include <iostream>
#include <string>
int getThousands(int number);
int getHundreds(int number);
int getTens(int number);
int getUnits(int number);
void printNumber(int number);
std::string tens[10] = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
std::string units[10] = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
std::string specialCase[10] = {"", "Eleven",
"Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
int main()
{
int number;
do
{
std::cout << "Please enter a number between 1 - 9999: ";
std::cin >> number;
if (number > 0 && number < 10000)
{
printNumber(number);
}
else
{
std::cout << "Number too big! Try again." << std::endl;
}
} while (number > 9999);
}
int getThousands(int number)
{
int noOfDivides = number/1000;
return noOfDivides;
}
int getHundreds(int number)
{
int noOfDivides = (number % 1000)/100;
return noOfDivides;
}
int getTens(int number)
{
int noOfDivides = (number % 100)/10;
return noOfDivides;
}
int getUnits(int number)
{
int remainder = number % 10;
return remainder;
}
void printNumber(int number)
{
if (!(getThousands(number) == 0)) /*if getThousands returns something
other than 1, it's a 4 digit number*/
{
std::cout << units[getThousands(number)] << " Thousand ";
}
if (!(getHundreds(number) == 0)) /*if getHundreds returns something
other than 1, it's a 3 digit number*/
{
std::cout << units[getHundreds(number)] << " Hundred ";
}
if (!(getTens(number) < 2)) /* (419%100)/10 = 1.9 = 1. (420%100)/10 = 2. Only
want this block to run if last 2 digits are 20 or above*/
{
std::cout << tens[getTens(number)] << " ";
}
if (number < 20) /* i.e. number is between 0 - 19, just take value and use it
straight in the array to get text version*/
{
std::cout << units[number];
}
else if (getTens(number) == 1) /*if final 2 digits are between 11-19 i.e. 119, 417, use the specialCase array for 'teens'*/
{
std::cout << specialCase[getUnits(number)]; /*for when 3 and 4 digit numbers
end in 11-19*/
}
else
{
std::cout << units[getUnits(number)];
}
}
I didn't want to look at the other solution because that wouldn't help me.
I had a scrap of paper like a mad scientist trying to find relationships between numbers and the values that they would return through my functions. This is how I arrived at the conditions of my if statements in 'void printNumber'.
I have seen other code that allows a greater range but I can't quite follow it (yet):
C++ code by fun2code - 67 lines - codepad
I'm sure this could be achieved in another way though, any suggestions for improvement to my version are appreciated. This is my first time with arrays and the starting at [0] bit was slightly confusing. My functions could be better named, and I'm sure there are many other things!
I wont keep posting my programs all the time unless I have a specific problem, but I'm not that confident at the moment and your evaluations are very useful to me. Thanks.