Integer & string problem in c++

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
Reaction score
13
I need to open a file, read from it and write to another file

For every alphabetical value, I need to write it to the new file as is. If there is a numeric/integer occurance, I need to convert it into alphabetical order

For example:

I have 1 apple and 16 bananas

need to be output as:

I have one apple and sixteen bananas


Any advice?
 
You could repeatedly mod the number with 10000 down to 1 (depends on int size) and use the remainder. I think you'll need some sort of lookup table to make your life easier for some numbers (like 11-19, 20,30,40,50 etc). For 100 you'd need an and as well. Something like:

647 % 100 = Six hundred and
47 % 10 = forty
7 % 1 = seven

312 % 100 = Three hundred and
12 % 10 = 1 ==> special case for 11-19 ==> twelve. Finish here

200 % 100 = Two hundred and
0 % 10 =
0 =
==> use sub string to remove the word and from the result for all calculations.

EDIT: the google answers algorithm is a lot better than minne
 
Last edited:
Code:
     public string NumberToWords(long Number, // ERROR: Optional parameters aren't supported in C# bool BlankIfZero)
     {
         string functionReturnValue = null;
         switch (Number) {
             case 0:
                 functionReturnValue = (BlankIfZero ? "" : "Zero");
                 break;
             case 1: // TODO: to 19
                 functionReturnValue = Interaction.Choose(Number, "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
                 "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"
                 );
                 break;
             case 20: // TODO: to 99
                 functionReturnValue = Interaction.Choose(Number / 10 - 1, "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety") + NumberToWords(Number % 10, true);
                 break;
             case 100: // TODO: to 999
                 functionReturnValue = NumberToWords(Number / 100) + "Hundred" + (Number >= 200 ? "s" : "") + NumberToWords(Number % 100, true);
                 break;
             case 1000: // TODO: to 999999
                 functionReturnValue = NumberToWords(Number / 1000) + "Thousand" + (Number >= 2000 ? "s" : "") + NumberToWords(Number % 1000, true);
                 break;
             case 1000000: // TODO: to 999999999
                 functionReturnValue = NumberToWords(Number / 1000000) + "Million" + (Number >= 2000000 ? "s" : "") + NumberToWords(Number % 1000000, true);
                 break;
             case // ERROR: Case labels with binary operators are unsupported : GreaterThanOrEqual 1000000000:
                 functionReturnValue = NumberToWords(Number / 1000000000) + "Billion" + (Number >= 2000000000 ? "s" : "") + NumberToWords(Number % 1000000000, true);
                 break;
         }
         return functionReturnValue;
     }

courtesy of http://www.devx.com/vb2themax/Tip/19053
 
Code:
     public string NumberToWords(long Number, // ERROR: Optional parameters aren't supported in C# bool BlankIfZero)
     {
         string functionReturnValue = null;
         switch (Number) {
             case 0:
                 functionReturnValue = (BlankIfZero ? "" : "Zero");
                 break;
             case 1: // TODO: to 19
                 functionReturnValue = Interaction.Choose(Number, "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
                 "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"
                 );
                 break;
             case 20: // TODO: to 99
                 functionReturnValue = Interaction.Choose(Number / 10 - 1, "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety") + NumberToWords(Number % 10, true);
                 break;
             case 100: // TODO: to 999
                 functionReturnValue = NumberToWords(Number / 100) + "Hundred" + (Number >= 200 ? "s" : "") + NumberToWords(Number % 100, true);
                 break;
             case 1000: // TODO: to 999999
                 functionReturnValue = NumberToWords(Number / 1000) + "Thousand" + (Number >= 2000 ? "s" : "") + NumberToWords(Number % 1000, true);
                 break;
             case 1000000: // TODO: to 999999999
                 functionReturnValue = NumberToWords(Number / 1000000) + "Million" + (Number >= 2000000 ? "s" : "") + NumberToWords(Number % 1000000, true);
                 break;
             case // ERROR: Case labels with binary operators are unsupported : GreaterThanOrEqual 1000000000:
                 functionReturnValue = NumberToWords(Number / 1000000000) + "Billion" + (Number >= 2000000000 ? "s" : "") + NumberToWords(Number % 1000000000, true);
                 break;
         }
         return functionReturnValue;
     }

courtesy of http://www.devx.com/vb2themax/Tip/19053


That doesnt look c++ish to me:)
 
Thanks for the response guys. And yea, that piece of code is c# if I'm not mistaken

c++ looks a bit different :p
 
I need to open a file, read from it and write to another file

For every alphabetical value, I need to write it to the new file as is. If there is a numeric/integer occurance, I need to convert it into alphabetical order

For example:

I have 1 apple and 16 bananas

need to be output as:

I have one apple and sixteen bananas


Any advice?

I had to do this type of thing in the beginning of SFD1 for my degree, teh book sams teach yourself c in 21 days was exceptionally helpfull....
 
Top
Sign up to the MyBroadband newsletter
X