Hi everyone, I am working on program to convert numbers to words, however, this is not the traditional conversion where by a number 150 is converted to one hundred and fifty; rather a number 150 is converted to one five zero. This program is an assignment I am working on and I have made some progress, but I am still confused, since I am not allowed to use strings, arrays, characters, recursion or any math functions. I can only use integer variables. My program so far is as follows:-
int main(void)
{
int Divisor = 10;
using namespace std;
//Request input from the user
cout << "Enter an integer: \n";
int inputInteger, individualDigit, CurrentDigit;
cin >> inputInteger;
//count the number of digits in inputInteger
int count = 0;
inputInteger /= Divisor;
while (inputInteger > 0)
{
inputInteger /= Divisor;
count++;
switch (CurrentDigit)
{
case 0:cout << "Zero";break;
case 1:cout << "One";break;
case 2:cout << "Two";break;
case 3:cout << "Three";break;
case 4:cout << "Four";break;
case 5:cout << "Five";break;
case 6:cout << "Six";break;
case 7:cout << "Seven";break;
case 8:cout << "Eight";break;
case 9:cout << "Nine";break;
}
cout << "\n";
cout << "Enter another integer:\n";
cin >> inputInteger;
}
The program gives the text for the last digit, but am not sure how to get and hold every digit in the integer. I am also confused about the modulus operator and how it works, so an explanation would be very helpful.
In your code, you are losing the first digit by dividing by the divisor BEFORE entering the loop. That line is unnecessary because inputInteger has already been initialized by the end user by means of typing a number.
But even after that correction, your code will produce the output backwards: The last digit will be shown first and the first will be shown last. In order to correct, I would have calculated the base-10 logarithm of the number (as it will tell me the number of decimal digits in the number) and then I would have used a simple for loop. BUT, you cannot. You must then figure out the number of digits in the input number differently. There is a simple way of doing this. I bet that you can find it.
Why do you need to use logarithm? You can just modulus & divide in a loop until it reaches 0.
1 2 3 4
for(cin >> num; num != 0; num /= 10)
{
int digit = num % 10; //do stuff with digit
}
You may want to reconstruct the number backwards with this method because this method gets the digits in forward order (numbers are read right-to-left). You want to print the digits in backwards order because that's how numbers are written, left to right. Then once you have the new 'backwards' number, you can do the exact same loop with it except printing out the digits as text like you wanted.
not allowed to use strings, arrays, characters, recursion or any math functions
That's quite a lot of restrictions there..
nth digit of number X is X/10n mod 10. Here n starts from 0 and 0th digit of 123 is 3. You can write a simple loop to find all digits and print them, but since this method finds them from the wrong end, that won't work. What you need is to find how long X is before printing it's digits.
To do this, you'll need a counter variable, p=1. The idea is to find a power of 10 which is greater than X. On every iteration multiply p by 10 until you find it.
Now that you have found the smallest power of 10 greater than X, you can find all q = powers of 10 between p and 1. Then to find the digit you want just calculate X/q%10.
The code you posted is not far from the reverse version ( if you do inputInteger /= Divisor; before finding CurrentDigit, you loose one digit. if you do inputInteger /= inputInteger; you get inputInteger = 1, of course )
Try thinking about what are the values of your variables on every line and every cycle in your loop.
@ L B: Please read more carefully: The logarithm was to reverse the result. I thought I clearly stated that. But even log() is forbidded by the assignment so that's out of the question. hamsterman have given away the way to replace it. :-S hehe. Leave the guy something! LOL
Thank you guys for your responses, I have actually solved the problem :) and here's the code for it.
#include<iostream>
const EMPTY '\0';
int main(void)
{
using namespace std;
//Request input from the user
cout << "Enter an integer: \n";
int inputInteger, currentDigit, divisor = 10, initialDivisor = 1;
cin >> inputInteger;
//count the number of digits in inputInteger
int count = 0, tempVariable;
tempVariable = inputInteger;
while (tempVariable != EMPTY)
{
initialDivisor *= divisor;
tempVariable /= divisor;
count++;
}
initialDivisor /= divisor;
//Read inputInteger and switch each digit
for (int i; i<count; i++)
{
if (inputInteger > 0)
{
//Find most significant digit
currentDigit = inputInteger / initialDivisor;
//Display digit in english
switch (currentDigit)
{
case 0:cout << "Zero\t";break;
case 1:cout << "One\t";break;
case 2:cout << "Two\t";break;
case 3:cout << "Three\t";break;
case 4:cout << "Four\t";break;
case 5:cout << "Five\t";break;
case 6:cout << "Six\t";break;
case 7:cout << "Seven\t";break;
case 8:cout << "Eight\t";break;
case 9:cout << "Nine\t";break;
}
inputInteger %= initialDivisor;
initialDivisor /= divisor;
}
else if (inputInteger < 0)
{
inputInteger = -(inputInteger);
cout << "Minus\t";
//Find most significant digit
currentDigit = inputInteger / initialDivisor;
//Display digit in english
switch (currentDigit)
{
case 0:cout << "Zero\t";break;
case 1:cout << "One\t";break;
case 2:cout << "Two\t";break;
case 3:cout << "Three\t";break;
case 4:cout << "Four\t";break;
case 5:cout << "Five\t";break;
case 6:cout << "Six\t";break;
case 7:cout << "Seven\t";break;
case 8:cout << "Eight\t";break;
case 9:cout << "Nine\t";break;
}
inputInteger %= initialDivisor;
initialDivisor /= divisor;
}
else if (inputInteger == 0)
cout << "Zero\t";
}
return 0;
}
I must note that I am still trying to get the zero case resolved i.e. if the user inputs a 0, then a zero should be displayed. The problem is in the while loop used to count the number of digits entered to control the following for loop. I have in the while loop the condition tempVariable != 0; I would like to have something like != NULL, but that works only with strings. I tried a const EMPTY '\0' definition but can't get it to work.
You don't initialize your i int the for loop to 0, so it could have any random value.
Also, your const EMPTY '\0'; is wrong, it should be constint EMPTY = '\0'; though I don't see why you would want this as you can just write 0.
Also, you don't need the same code twice. It could just be
1 2 3 4 5
if(inputInteger < 0){
inputInteger = -inputInteger;
cout << "Minus\t";
}
//here goes your for loop without checking whether inputInteger is positive
And lastly, when you post code here, use [code][/code] tags so that your code is more readable.