c++: numbers to text not working

Hi Guys,

I cant get my numbers to text to work, i tried num/100, (num/10)%10 and i cant get the first digit from the left in a 3 digit number: ex: 123.

If anyone can assist me with this would be great.

ty

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

 
using namespace std;


/////////Implement the source code that turns numbers into English text.


int main()
{
	int num, Ldight, Rdight, Tdight;

	string ones[] = { "zero ", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "ninteen"};
	string tens[] = { "twenty ", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninghty"};
	string hundreds[] = { "one hundred","two hundred","three hundred","four hundred","five hundred","six hundred","seven hundred", "seven hundred","eight hundred","nine hundred"};
	string thousand[] = { "thousand"};

	cout << "Enter a number from 1-1000: " << endl;
	cin >> num;

	if(num <= 10 && num >= 1)
	{
		cout << "The number you entered is: " << ones[num] << endl;
	}else if(num >= 20 && num <= 99)
	{
		Rdight = num %10;
		Ldight = num/10; 
		cout << "The number you selected is: " << tens[Ldight-2] << ones[Rdight] << endl;
	}else if( num >=100 && num <= 999)
	{
		Rdight = num %10;
		Ldight = num/10;
		Tdight = num/100;
		cout << "The number you selects is: " << hundreds[Tdight-2] << tens[Ldight-2] << ones[Rdight] <<  endl;
	}


	



}
you can get the rightmost digit simply by using mod 10. You were close but you never removed the right digit from the numbers when you tried to find the rest of the digits.

say we have a number 23:

23 % 10 = 3

so if we want to remove that we could either subtract 3 and divide by 10 or even just divide by 10 since they are integer values and when a double gets casted to an integer it is automatically floored ( removed right most digit )

so here are a few fixes:

1
2
3
Rdight = num %10;
//Ldight = num/10;
Ldight = num / 10;


and:

1
2
3
4
5
6
7
8
Rdight = num %10;
//Ldight = num/10;
//Tdight = num/100;

//this one was very close you just had the left and middle swapped

Ldight = num/100;
Tdight = num/10;


Also why do you spell it as "dight" why not "digit?"

dight
dīt/Submit
adjectivearchaic
1.
clothed or equipped.
verbliterary
1.
make ready for a use or purpose; prepare.
"let the meal be dighted"


I see another slight problem. hundreds[Tdight-2] should be -1 not - 2.

One last thing to mention you don't have an else for the 1000 case.
I was following a dumb youtube video lol, thought it was a unique code or something

You changed the tdight to the same value of ldight im so confused on this.

I want to get the value of lets say 123

value for 1 is num /100 ??
value for 2 is num /10
value for 3 is num %10

this is mainly what im stuck at, but you switched them and now im more confused lol, as for the 1000 I just couldnt figure out how to get the (1) in 1000 , num/1000????

ty
Sorry earlier I meant / 10 % 10 and /100 % 10

Lets look at the math:

num = 123

Then we have left , middle , right


We can do this a few different ways. For me the easiest way without caring for the old copy would be to use modulo 10 then divide by 10 ex:

1
2
3
4
5
6
7
int num = 123;

while( num ) //while num is anything but 0
{
    cout << num % 10 << endl;
    num /= 10;
}


Here is what that does
we have 123 as starting value

we output each digit from right to left.

123 % 10 = 3
123 / 10 = 12.3 ( casted as int/flooring ) = 12
12 % 10 = 2
12 / 10 = 1.2 ( casted as int/flooring) = 1
1 % 10 = 1
1 / 10 = .1 ( casted as int/flooring ) = 0


Now the easiest method for me if you care about the original value:

1
2
3
4
5
6
7
8
9
const int num = 123 , digits = 3; //you could do math to find the number of digits but that's another topic
const int ten = 10;
int power = 1;

for( int i = 0; i < digits; ++i )
{
    cout << ( num / power ) % ten << endl;
    power *= 10;
}


Here is what happens:

num starts at 123, power starts at 1

first output: ( 123 / 1 ) % 10 = ( 123 ) % 10 = 3
second output: ( 123 / 10 ) % 10 = ( 12.3 ) % 10 = ( floored ) 12 % 10 = 2
third output: ( 123 / 100 ) % 10 = ( 1.23 ) % 10 = (floored) 1 % 10 = 1

Basically if you do integer division you get an integer and not a floating-point so if you have 1.9999 it is 1 as an integer it floors it down it does not round up or down.
Got it to work guys ty

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

 
using namespace std;


/////////Implement the source code that turns numbers into English text.


int main()
{
	int num, Ldight, Rdight, Tdight, Fdigit;

	
	string ones[] = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "ninteen"};
	string tens[] = { "", "", "twenty ", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninghty"};
	string hundreds[] = { "", "one hundred","two hundred","three hundred","four hundred","five hundred","six hundred","seven hundred","eight hundred","nine hundred"};
	string thousand[] = { "thousand"}; ///incomplete

	cout << "Enter a number from 1-1000: " << endl;
	cin >> num;

	if(num <= 19 && num >= 1)
	{
		Rdight = num %10;
		Ldight = num/10;
		cout << "The number you entered is: " << ones[num] << endl;
	}else if(num >= 20 && num <= 99)
	{
		Rdight = num %10;
		Ldight = num/10;
		cout << "The number you selected is: " << tens[Ldight] << ones[Rdight] << endl;
	}else if( num >=100 && num <= 119)
	{
		Rdight = num%50;
		Tdight = num/100;
		cout << "The number you selects is: " << hundreds[Tdight] <<  ones[Rdight] <<  endl;
	}else if( num >=120 && num <= 999)
	{
		if( num%50 == 19)//////219,319,419,519,619
		{
			Rdight = num%50;
			Tdight = num/100;
			cout << "The number you selects is: " << hundreds[Tdight] <<  ones[Rdight] <<  endl;
		}else
		{
		Rdight = num %10;
		Ldight = (num%100)/10;
		Tdight = num/100;
		cout << "The number you selects is: " << hundreds[Tdight] << tens[Ldight] << ones[Rdight] <<  endl;
		}
	}

system("pause");
}
Last edited on
Topic archived. No new replies allowed.