I am trying to write a program that accepts numbers and prints them out word in words. Input: 1234
Output: one-two-three-four
Here is my idea of working around it.
First using a loop and dividing continuously by 10, I work out the number of digits in the number. Then I extract the numbers each one one by one, pass them over to a do while loop that has switch case. Now I have a problem. My program seems to work with some numbers, e.g. 1234 but doesn't work with others because it prints out a false number.
Test:
Input: 1234
Output: one-two-three-four-
which is correct
BUT this
Input: 65767
Output: six-five-four-six-seven
is wrong.
Code:#include <iostream> #include <cmath> using namespace std; int main() { double counter =0; int x; int i = 1; int num; cout<<"Please enter a number"<<endl; cin>>num; x = num; for (num;num>0; num = num/10) { counter = counter + i; } do{ int y = (x/(int(pow(10,(counter-1)))))%10;//Here is probably where //the problem is but I can't figure it out as it looks fine to me. switch(y){ 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; default: cout<<"Not a number!"<<endl;} counter --; } while (counter); return 0; }