Numbers to Words

I'm not sure on how to do this where you put in number and your suppose to get a word example: 2 -> two ,100->hundered using arrays

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
  #include <iostream>
#include <string.h>
#include <string>

using namespace std;



char Ones[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"
, "ten", "eleven", "twelve", "thirteen", "fouthteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };

char Tens[] = { "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" };




int main()
{

	cout << "Please enter a number: " << endl;
	





	system("pause");
	return 0;
}
Last edited on
I think you want to be usingstring and notchar. char only takes one character not a word
Last edited on
I think this is easier, but is uncomplete you have to complete it.
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
#include <iostream>
using namespace std;

int main()
{
    int a;
    label:
	cout << "Please enter a number: " << endl;
	cin >> a;
	switch (a)
	{
		case 0:
			cout << "zero" << endl;
			break;
		
		case 1:
		   	cout << "one" << endl;
			break;    
		   	// Here do the same with 2, 3, 4, etc.
		default:
		    cout << "Unknown character. Try again." << endl;//Or whatever you want to put 
			goto label;   	
	}
	
    system("pause");
	return 0;
}
Well you are going to need to start with checking if it is 100 if so print 100.
if it is less than 100 and greater than 19 you are going to want to get the first digit eg and output Tens[x%10 - 2] since twenty is at index 0 so 2 - 2 = 0. Then you are going to want to output 1-19 if there is any remaining after checking the left digit [edit] or it is less than 20 to start with[/edit]
Last edited on
i got it but in a differnet way but i need to add point to the code an example would be like 1000.25 -> one thousand point twenty five .Trying to use a function but im lost here.

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "stdafx.h"
#include <iostream>
#include <string.h>


using namespace std;


void saySingleDigit(char x)
{
	switch (x)
	{
	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;
	}
}

void sayTwoDigits(char a, char b)
{
	if (a == '1' &&  b == '1') { cout << "eleven"; return; }
	if (a == '1' &&  b == '2') { cout << "twelve"; return; }
	if (a == '1' &&  b == '3') { cout << "thirteen"; return; }
	if (a == '1' &&  b == '4') { cout << "fourteen"; return; }
	if (a == '1' &&  b == '5') { cout << "fifteen"; return; }
	if (a == '1' &&  b == '6') { cout << "sixteen"; return; }
	if (a == '1' &&  b == '7') { cout << "seventeen"; return; }
	if (a == '1' &&  b == '8') { cout << "eighteen"; return; }
	if (a == '1' &&  b == '9') { cout << "nineteen"; return; }


	switch (a)
	{
	case '2': cout << "twenty "; break;
	case '3': cout << "thirty "; break;
	case '4': cout << "fourty "; break;
	case '5': cout << "fifty "; break;
	case '6': cout << "sixty "; break;
	case '7': cout << "seventy "; break;
	case '8': cout << "eighty "; break;
	case '9': cout << "ninety "; break;
	}
	saySingleDigit(b);
}

void sayThreeDigits(char a, char b, char c)
{
	saySingleDigit(a);
	cout << " hundred ";
	sayTwoDigits(b, c);
}

void sayFourDigits(char a, char b, char c, char d)
{
	saySingleDigit(a);
	cout << " thousand ";
	saySingleDigit(b);
	cout << " hundred ";
	sayTwoDigits(c, d);
	
}

int main()
{
	char num[7];
	int n, shift = 0;
	cout << "Please enter a number between -9,999.99 to 9,999.99: ";
	cin >> num;
	n = strlen(num);

	if (num[0] == '-') 
	{
		cout << "minus ";
		shift = 1;
		n--;

	}
	


	if (n == 1 && num[0] == '0') cout << "zero";
	else
	
		switch (n)
	{
		case 1: saySingleDigit(num[0 + shift]); break;
		case 2: sayTwoDigits(num[0 + shift], num[1 + shift]); break;
		case 3: sayThreeDigits(num[0 + shift], num[1 + shift], num[2 + shift]); break;
		case 4: sayFourDigits(num[0 + shift], num[1 + shift], num[2 + shift],num[3+shift]); break;
				
	}
	cout << endl;
	system("pause");
Last edited on
#include <string>
#include <sstream>
#include <iostream>
using namespace std ;

int main( )
{
string term = "100" ; // String to convert to int.
int number = 100 ; // Int to convert to string.
string text ; // String variable for converted int.
int num ; // Int variable for converted string
stringstream stream ; // Intermediary stream object.

// Convert string to int...
stream << term ; // Load string into stream.
stream >> num ; // Extract stream to int.
num /= 4 ; // Manipulate the int.
cout << "Integer value: " << num << endl ;

// Reset the stream object back to new...
stream.str("") ; // Reset the stream to an empty string.
stream.clear() ; // Reset the stream bit flags (good,bad,eof,fail).

// Convert int to string...
stream << number ; // Load int into stream.
stream >> text ; // Extract stream to string.
text += " Per Cent" ; // Manipulate the string.
cout << "String value: " << text << endl ;

return 0 ;
}
Topic archived. No new replies allowed.