Recent

Author Topic: [SOLVED] Amount in words  (Read 1904 times)

Pe3s

  • Hero Member
  • *****
  • Posts: 533
[SOLVED] Amount in words
« on: March 16, 2023, 07:48:51 pm »
Hello, I wanted to write a program that converts the amount into words. The program compiles, but when I try to convert, there is an error. How can I improve it?

Regards everyone :)
« Last Edit: March 21, 2023, 06:10:09 pm by Pe3s »

domasz

  • Sr. Member
  • ****
  • Posts: 423

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: Amount in words
« Reply #2 on: March 17, 2023, 09:06:12 am »
I remember a really old discussion about this subject on this forum.
I will edit this comment when I find it.
[edit]
https://forum.lazarus.freepascal.org/index.php/topic,20904.msg121671.html#msg121671
« Last Edit: March 17, 2023, 09:09:24 am by Thaddy »
Specialize a type, not a var.

Roland57

  • Sr. Member
  • ****
  • Posts: 421
    • msegui.net
Re: Amount in words
« Reply #3 on: March 17, 2023, 10:01:22 am »
@Pe3s

Hello! It's hard to help without knowing the target language, but I see a mistake here:

Code: Pascal  [Select][+][-]
  1.     if (i > 4) and (StrToInt(digits[5]) > 1) then
  2.         str := str + dziesiatki[ StrToInt(digits[5]) ] + ' '
  3.     else if (i > 3) and (digits[5]<>'') and (StrToInt(digits[5]) = 1) then // <---

What happens, for example, if i = 4;D
My projects are on Gitlab and on Codeberg.

Pe3s

  • Hero Member
  • *****
  • Posts: 533
Re: Amount in words
« Reply #4 on: March 17, 2023, 05:39:17 pm »
What could be the cause of the error

BobDog

  • Sr. Member
  • ****
  • Posts: 394
Re: Amount in words
« Reply #5 on: March 17, 2023, 06:10:53 pm »

For fun, Win.
Code: Pascal  [Select][+][-]
  1. uses
  2. process;
  3.  
  4. procedure speak(text:ansistring);
  5. var
  6. x:ansistring;
  7. s:ansistring='';
  8. begin
  9. writeln(text);
  10.     x:='mshta vbscript:Execute("CreateObject(""SAPI.SpVoice"").Speak(""'+text+'"")(window.close)")';
  11.       runcommand(x,s);
  12. end;
  13.  
  14. begin
  15. speak('123,401.90');
  16. speak('Please press return to finish');
  17. readln;
  18. end.
  19.  
  20.  

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: Amount in words
« Reply #6 on: March 17, 2023, 06:17:36 pm »
And then select Polisch
Specialize a type, not a var.

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Amount in words
« Reply #7 on: March 19, 2023, 09:21:41 pm »
Seriously, are we talking about currency to String notation in verbose or are we talking about about actually spitting out to the speaker of the PC?

 I have some code that uses the SAPI and a simple implementation of it, it can even select from Male or Female voice.
The only true wisdom is knowing you know nothing

domasz

  • Sr. Member
  • ****
  • Posts: 423
Re: Amount in words
« Reply #8 on: March 19, 2023, 09:42:15 pm »
We are talking about converting numbers to text, 23 => twenty three, in Polish. Which is solved in the first reply in this thread.

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Amount in words
« Reply #9 on: March 19, 2023, 11:05:17 pm »
Ok, Well I was kind of thinking on those lines..


 In any case, I have attached a simple test app that Plays a little with the SAPI.

 It announces money strings very nicely. :D

 If not for that, its a good primer on very basic OLE.
The only true wisdom is knowing you know nothing

Pe3s

  • Hero Member
  • *****
  • Posts: 533
Re: Amount in words
« Reply #10 on: March 20, 2023, 04:22:52 pm »
What could be the cause of the error?

bytebites

  • Hero Member
  • *****
  • Posts: 633
Re: Amount in words
« Reply #11 on: March 20, 2023, 04:36:40 pm »
Proggamming error.

Pe3s

  • Hero Member
  • *****
  • Posts: 533
Re: Amount in words
« Reply #12 on: March 20, 2023, 05:23:53 pm »
Please help as I cannot find the error

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Amount in words
« Reply #13 on: March 20, 2023, 06:26:14 pm »
Please help as I cannot find the error
This (and maybe other lines) in your source are causing trouble.
Code: Pascal  [Select][+][-]
  1.     else if (i > 0) and (digits[2]<>'') and (StrToInt(digits[2]) = 1) then
Why?
Because you check "if i bigger than zero" (i holds the length of a inttostr conversation) straight after you check for an element at a specific position without checking if that position is available at all.

(if app would be in a language that i understand it would be more easy to fix)

quick and dirty fix:
Code: Pascal  [Select][+][-]
  1.     else if (i >= 2) and (digits[2]<>'') and (StrToInt(digits[2]) = 1) then
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

alpine

  • Hero Member
  • *****
  • Posts: 1038
Re: Amount in words
« Reply #14 on: March 20, 2023, 06:43:52 pm »
What KodeZwerg spotted was the immediate error. Even with that corrected, another one will raise with numbers with n*3+1 digits, e.g. 1, 1001.

I'm afraid the approach is wrong - you actually must consider the digits in the given number in a groups of three putting appropriate words for thousands, millions, etc. after them as in the
https://4programmers.net/Delphi/Gotowce/Zapis_s%C5%82owny_liczby

Edit: Same error with the line
Code: Pascal  [Select][+][-]
  1.     else if (i > 3) and (digits[5]<>'') and (StrToInt(digits[5]) = 1) then
  2.         str := str + nastki[StrToInt(digits[4])] + ' ';
  3.  
And Roland57 found that 3 days ago
« Last Edit: March 20, 2023, 06:51:19 pm by alpine »
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

 

TinyPortal © 2005-2018