Problem with converting numbers with text

Im trying to write a program that could convert numbers to text.

The main problem is that i only got error when i write a number between 100-999. I know i doing something wrong here but im not sure how to fix the problem because im very new at C++ so im a little confused right now. I'd appreciate if someone could help me with this problem.

Anyway, this is the program i have written:


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
#include <iostream>
#include <string>
using namespace std;

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

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

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

	string hundreds[] = {"one hundred", " two hundred", "three hundred", "four hundred", "five hundred", "six hundred", "seven hundred", "eight", "nine hundred"};


	cout << "Pick a number between 0-999: ";
	cin >> num;

	if(num <= 0)
	{
		cout << "Try again!" << endl;
	}

	else if (num >= 0 && num <= 19)
	{
		cout << "Your number is: " << ones[Rdight] ;
	}

	else if (num >=20 && num <=99)
	{
		Rdight = num % 10;
		Ldight = num / 10;

		cout << "Your number is: " << tens[Ldight - 2] << ones[Rdight];	
	}
        else if (num >=99 && num <=999)
	{
		Rdight = num % 10;
		Ldight = num / 10;
		Hdight = num / 100;
		num = num - Hdight * 100;

		cout << "Your number is: " << hundreds[Hdight-1] << tens[Ldight - 2] << ones[Rdight];	
	}
         return 0;
	 }
 
Ldight = num / 10;

This can evaluate to anything between 9 and 99. You're not extracting the tens correctly.

Also, if you feed 99 to your last loop you'll get a memory access error. It should be part of the second loop.
1
2
3
		Rdight = num % 10;
		Ldight = num / 10;
		Hdight = num / 100;


Given that num = 876. The desired result of this code is that:

Rdight = 6
Ldight = 7
Hdight = 8

However that is not what this code generates. Instead you get this:

Rdight = 6
Ldight = 87
Hdight = 8


As you can see, you're not calculating Ldight properly.
See what you mean, but do not quite understand how to fix it...
Disregard my second statement, I missed the "else".

What about (num%100)/10?
#include <iostream>
#include <string>
using namespace std;

int main(){
int temp=0, temp2=0;
int num, Ldigit=0, Rdigit=0, Hdigit=0;
string ones[] = {"zero","one", "two", "three", "four",
"five","six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen"};

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

string hundreds[] = {"one hundred","two hundred","three hundred","four hundred","five hundred","six hundred","seven hundred","eight hundred","nine hundred"};


cout << "Pick a number between 0-999: ";
cin >> num;

if(num < 0)
cout<<"Try Again!!!";

else{
if((num >= 0) && (num < 20))
cout<<'\n'<<ones[num];
else if ((num >= 20) && (num < 100)){
Rdigit = num % 10;
Ldigit = num / 10;
cout<<tens[Ldigit - 2]<<" "<<ones[Rdigit];
}
else if ((num >= 100) && (num < 1000)){
Hdigit = num / 100;
temp = num % 100;
Ldigit = temp /10;
temp2 = temp % 10;
Rdigit = temp2 % 10;

cout<<hundreds[Hdigit-1]<<" ";
if (Ldigit >= 2)
cout<<tens[Ldigit-2]<<" ";
else {
if(Ldigit == 1)
Rdigit += 10;
}
if (Rdigit > 0)
cout<<ones[Rdigit];
}
else
cout<<"\nOut of bounds!!!";

}
system ("PAUSE");
return 0;
}

try this..
Last edited on
Please don't give out full solutions, redlichan.

See what you mean, but do not quite understand how to fix it...


Well you know that % 10 gives you the remainder after a division by 10 (isolating the low digit).

You also know that / 10 drops the low digit.

So if you want to drop the low digit, then isolate the next low digit........
Please don't give out full solutions, redlichan.


noted... I'm a newbie.. Next time I won't... :)
Topic archived. No new replies allowed.