enter any integer and convert it to words.

So I have been writing a code and I am trying to get it to spell out word by word using the numbers I entered. say I enter '-000000000000026450' I would like to display "-26450" is "minus two six four five zero"' I can only get it to print single numbers at the moment(-2 writes out minus 2) NOT using nested loops.
any idea?

[code]
int main()
{

int number, i = 0, absolutenumber, ones;
//char * word[1000];

cout << "Enter any integer\n ";
cin >> number;

if (number < 0)
{
cout << "minus ";
}

absolutenumber = abs(number);

{
// tens = (absolutenumber / 10) % 10;

ones = absolutenumber % 10;

switch (ones) {


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<< number << " spelled out is"<<...<< endl;

return 0;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <stdlib.h>

using std::string;
using std::cout;
using std::cin;

string DigitToString(char ch);

void main(void)
{
  int input;

  cout << "Enter a number: ";
  cin >> input;

  char buffer[128];

  itoa(input, buffer, 10);

  cout << input << " spelled out is" << "\n\n";

  for (size_t i = 0, len = strlen(buffer); i < len; ++i)
  {
    cout << DigitToString(buffer[i]) << ' ';
  }

  system("pause");
}

string DigitToString(char ch)
{
  switch (ch) 
  {
  case '0': return "zero";
  case '1': return "one";
  case '2': return "two";
  case '3': return "three";
  case '4': return "four";
  case '5': return "five";
  case '6': return "six";
  case '7': return "seven";
  case '8': return "eight";
  case '9': return "nine";
  case '-': return "minus";
  default:
    return "illegal";
  }
}
thanks, I atempted to run it and it generated an error for itoa
Severity Code Description Project File Line Suppression State
Error C4996 'itoa': The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: _itoa. See online help for details. ConsoleApplication5 c:\users\owner\documents\visual studio 2015\projects\console1\lesson 2\consoleapplication5\consoleapplication5\source.cpp 20
Error C4996 'itoa': The POSIX name for this item is deprecated


One of the nuisances of Visual Studio.
Just use _itoa or snprintf.
http://www.cplusplus.com/reference/cstdio/snprintf/
okay it ran!! would you have any idea how to get it to print all on the same line?
'-123' is minus one two three.
not '-123' is
minus one two three do you understand what i mean?
Just change line 22 to cout << input << " spelled out is ";
do you have any idea how to write the code using if and while loops? no strings basic basic coding. I am trying to use modulo(%)
this is my new code so far i can get it to print -21 say but i cant print large numbers or stop the loop.


#include <iostream>
#include<conio.h>
using namespace std;

int main()
{

int number, absolutenumber, dividend, x, divisor=1, remainder, nevar;
//char * word[1000];

cout << "Enter any integer\n ";
cin >> number;

if (number < 0)
{
cout << "minus ";
}

absolutenumber = abs(number);

{

while (absolutenumber >= 0)
{

dividend = absolutenumber / divisor;

if (dividend < 9)
{
nevar = divisor * 1;
dividend = absolutenumber / (divisor * 1);
remainder = (absolutenumber % nevar);
}
else if (dividend > 9)
{
nevar = divisor * 10;
dividend = absolutenumber / (divisor * 10);
remainder = (absolutenumber % nevar);

}
else if (dividend > 90)
{
nevar = divisor * 100;
dividend = absolutenumber / (divisor * 100);
remainder = (absolutenumber % nevar);
}
else if (dividend > 9000)
{
nevar = divisor * 1000;
dividend = absolutenumber / (divisor * 1000);
remainder = (absolutenumber % nevar);

}



switch (dividend) {


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;
}
absolutenumber = remainder;
}
}
// cout << number << "is";// << << endl;
return 0;

}
Topic archived. No new replies allowed.