1,105,447 Community Members

Convert any 1,2, or 3 digit number into words using string/array in C++

Member Avatar
7h3.doctorat3
Newbie Poster
2 posts since Jun 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello, i need help to convert any 1, 2, or 3 digit number into words using string/array method in C++. For example, 111 should read one hundred and eleven, 011 should read eleven, 001 should read one. I would much appreciate it if you could help me solve this problem. I have done a little of it so far and it is correct up to this point and no modifications should be necessary. It should also be short (30 lines max.)

#include<iostream>
using namespace std;
int main()
{
    int A,B,C,number;
    string a[]={"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
    string b[]={"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
    string c[]={"hundred"};
    cout<<"Please enter a three digit number";
Member Avatar
Radical Edward
Posting Pro
526 posts since May 2008
Reputation Points: 301 [?]
Q&As Helped to Solve: 98 [?]
Skill Endorsements: 2 [?]
 
0
 

> I have done a little of it so far and it is correct up to this point
This is true. It is very hard to get array declarations and a prompt for input wrong. :D Ed is inclined to believe that you accidentally pasted only a portion of your code rather than all of it.

Member Avatar
finito
Nearly a Posting Virtuoso
1,264 posts since May 2010
Reputation Points: 46 [?]
Q&As Helped to Solve: 140 [?]
Skill Endorsements: 0 [?]
 
0
 

It looks like this is a homework assignment.

Member Avatar
dansnyderECE
Junior Poster
104 posts since Jun 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Looks like you're on the right track, just keep in mind that if you have to account for all 999 cases of 3 digit numbers then it would be advantageous for you to look for word aliasing. For instance, eleven occurs 11 times within that range. hundred occurs 900 times etc. Looks like you're headed that way.

Member Avatar
rhoan
Newbie Poster
2 posts since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

guyz can convert this... this the problem.. conversion of numbers into words maximum 999,999... sample output:
enter: 192,453
figure: one hundred ninety two thousand four fifty three

Member Avatar
rhoan
Newbie Poster
2 posts since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

help me guyz....

Member Avatar
prvnkmr449
Junior Poster
113 posts since Sep 2010
Reputation Points: -8 [?]
Q&As Helped to Solve: 16 [?]
Skill Endorsements: 0 [?]
 
0
 

No one is here to give you a copy paste code, you have to do work for that.
You work quite fine until now..........

Further what you have do thats I will explain bellow

Take user input in a integer type variable for example num
Use a string variable to store converted input in word and initialize it with NULL
Then you have to check your last two digit number is less then 20 or not

IF LASTTWODIGIT <20 THEN
INWORD=a[LASTTWODIGIT]+" "+INWORD
OTHERWISE
INWORD=b[DIGIT_AT_TENTH_PLACE]+" "+a[DIGIT_AT_ONES_PLACE]+" "+INWORD

INWORD=a[DIGIT_AT_HUNDREDS_PLACE]+" "+INWORD

FOR EXAMPLE: num=456

LASTTWODIGIT=56
DIGIT_AT_HUNDRED_PLACE=4
DIGIT_AT_TENS_PLACE=5
DIGIT_AT_ONCES_PLACE=6

Best Of Luck

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: