fork download
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. //forward declarations
  6. int power_of_ten(int);
  7. int input();
  8. int num_count(int&);
  9. void new_val(int&);
  10.  
  11. int main()
  12. {
  13. int string_of_numbers{0};
  14.  
  15. do
  16. {
  17. string_of_numbers=input();
  18. cout << endl; //spacing
  19. new_val(string_of_numbers);
  20. cout << endl; //spacing
  21. cout << endl; //spacing
  22. system("pause");
  23. system("cls");
  24. }while(string_of_numbers != -1); // loop until user enters -1
  25. }
  26.  
  27. int input()
  28. {
  29. int num_to_string;
  30. cout << "Insert or '-1' to quit: ";
  31. cin >> num_to_string;
  32. return num_to_string;
  33. }
  34.  
  35. int num_count(int &string_of_numbers)
  36. {
  37. int sub_string_of_numbers{string_of_numbers};
  38. int amount{0};
  39. while(sub_string_of_numbers>0)
  40. {
  41. sub_string_of_numbers=sub_string_of_numbers/10;
  42. amount++;
  43. }
  44. return amount;
  45. }
  46.  
  47. void new_val(int &string_of_numbers)
  48. {
  49. int m[100]={0};
  50. int i{0};
  51. int count_n{0};
  52.  
  53. //added if else checks for input to set count_n variable
  54. if (string_of_numbers == 0)
  55. count_n = 0;
  56. else
  57. count_n = num_count(string_of_numbers)-1;
  58.  
  59. for(i = 0; count_n > (-1); i++)
  60. {
  61. int exponents = power_of_ten(count_n); //added function call and set to exponents variable
  62.  
  63. //added if else checks to set array of integers
  64. if(string_of_numbers == 0)
  65. {
  66. m[i] = 0;
  67. }
  68. else if (count_n == 0)
  69. {
  70. m[i]=string_of_numbers;
  71. }
  72. else
  73. {
  74. m[i]=string_of_numbers / exponents;
  75. }
  76.  
  77. switch(m[i])
  78. {
  79. case 0: cout << "Zero "; break;
  80. case 1: cout << "One "; break;
  81. case 2: cout << "Two "; break;
  82. case 3: cout << "Three "; break;
  83. case 4: cout << "Four "; break;
  84. case 5: cout << "Five "; break;
  85. case 6: cout << "Six "; break;
  86. case 7: cout << "Seven "; break;
  87. case 8: cout << "Eight "; break;
  88. case 9: cout << "Nine "; break;
  89. }
  90. string_of_numbers %= exponents;
  91. count_n--;
  92. }
  93.  
  94. }
  95.  
  96. // function for power of 10s
  97. int power_of_ten(int x)
  98. {
  99. int power_of_tens = 10;
  100. for(int j = 1; j < x; ++j)
  101. {
  102. power_of_tens *= 10;
  103. }
  104. return power_of_tens;
  105. }
Success #stdin #stdout #stderr 0s 15240KB
stdin
1234
 
7894
 
0
 
-1
 
stdout
Insert or '-1' to quit: 
One Two Three Four 

Insert or '-1' to quit: 
Seven Eight Nine Four 

Insert or '-1' to quit: 
Zero 

Insert or '-1' to quit: 


stderr
sh: 1: pause: not found
sh: 1: cls: not found
sh: 1: pause: not found
sh: 1: cls: not found
sh: 1: pause: not found
sh: 1: cls: not found
sh: 1: pause: not found
sh: 1: cls: not found