Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4095
Number of posts: 14004

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
how do you convert a written number in to a digit number??? Posted by ahinchee on 24 Oct 2003 at 9:53 AM
I am trying to write a program where you prompt a user to input a 3 digit number written in text... ie.. one hundred thirty-five & have it output 135.

I am having so much trouble with this. I am very very new to Pascal & I am not grasping the concepts at all.

For right now... I am trying to just get the 1 out... not worried about the rest.... I am entering a one in & all I can get is a one back. HELP ME!

CONST
one='1';
VAR
dignum:
integer;
wrtnum:
string;
(**********************************************************)
BEGIN
Write('Please enter a 3-digit number in text:');
Read(wrtnum);
write(wrtnum);


IF wrtnum = one
THEN dignum = 1
END.

I know this is probably nowhere being correct... but I am totally lost. My book doesn't help & I haven't found anything on any sites that references this.

what is the alcoholic statistics for Pascal Programmers???? Cause I swear its enough to drive you to drinking! :(
Report
Re: how do you convert a written number in to a digit number??? Posted by Perran on 24 Oct 2003 at 10:53 AM
: I am trying to write a program where you prompt a user to input a 3 digit number written in text... ie.. one hundred thirty-five & have it output 135.
:
: I am having so much trouble with this. I am very very new to Pascal & I am not grasping the concepts at all.
:
: For right now... I am trying to just get the 1 out... not worried about the rest.... I am entering a one in & all I can get is a one back. HELP ME!
:
: CONST
: one='1';
: VAR
: dignum:
: integer;
: wrtnum:
: string;
: (**********************************************************)
: BEGIN
: Write('Please enter a 3-digit number in text:');
: Read(wrtnum);
: write(wrtnum);
:
:
: IF wrtnum = one
: THEN dignum = 1
: END.
:
: I know this is probably nowhere being correct... but I am totally lost. My book doesn't help & I haven't found anything on any sites that references this.
:
: what is the alcoholic statistics for Pascal Programmers???? Cause I swear its enough to drive you to drinking! :(
:
Try
IF wrtnum = 'one' THEN dignum =1;

Make yourself a table of constants to hold the strings ['one', 'two']. Try some more code. This looks a lot like schoolwork. Drinking is bad for your liver...
Report
Re: how do you convert a written number in to a digit number??? Posted by ahinchee on 24 Oct 2003 at 11:30 AM
: : I am trying to write a program where you prompt a user to input a 3 digit number written in text... ie.. one hundred thirty-five & have it output 135.
: :
: : I am having so much trouble with this. I am very very new to Pascal & I am not grasping the concepts at all.
: :
: : For right now... I am trying to just get the 1 out... not worried about the rest.... I am entering a one in & all I can get is a one back. HELP ME!
: :
CONST
one='one';
two='two';
three='three';
four='four';
five='five';
six='six';
seven='seven';
eight='eight';
nine='nine';
ten='ten';
VAR
wrtnum:
string;
dignum,
outerror:
integer;
(**********************************************************)
BEGIN
Write('Please enter a 3-digit number in text:');
Read(wrtnum);

Val(wrtnum,dignum,outerror);


writeln(outerror);
END.

***Yes this is homework & I'm totally lost in this class. Trying to hit message boards to find insight since I am not picking it up.

I changed my code to the above.... it compiles... once I enter one... it outputs 2... i close down the dos window & try it again this time entering two.. it ouputs 1 still???

Yes drinking is bad for your liver... but I am starting to believe that Pascal is bad for my total health! :)

: :
: Try
:
: IF wrtnum = 'one' THEN dignum =1;
: 

: Make yourself a table of constants to hold the strings ['one', 'two']. Try some more code. This looks a lot like schoolwork. Drinking is bad for your liver...
:

Report
Re: how do you convert a written number in to a digit number??? Posted by Perran on 25 Oct 2003 at 4:47 AM
: : : I am trying to write a program where you prompt a user to input a 3 digit number written in text... ie.. one hundred thirty-five & have it output 135.
: : :
: : : I am having so much trouble with this. I am very very new to Pascal & I am not grasping the concepts at all.
: : :
: : : For right now... I am trying to just get the 1 out... not worried about the rest.... I am entering a one in & all I can get is a one back. HELP ME!
: : :
: CONST
: one='one';
: two='two';
: three='three';
: four='four';
: five='five';
: six='six';
: seven='seven';
: eight='eight';
: nine='nine';
: ten='ten';
: VAR
: wrtnum:
: string;
: dignum,
: outerror:
: integer;
: (**********************************************************)
: BEGIN
: Write('Please enter a 3-digit number in text:');
: Read(wrtnum);
:
: Val(wrtnum,dignum,outerror);
:
:
: writeln(outerror);
: END.
:
: ***Yes this is homework & I'm totally lost in this class. Trying to hit message boards to find insight since I am not picking it up.
:
: I changed my code to the above.... it compiles... once I enter one... it outputs 2... i close down the dos window & try it again this time entering two.. it ouputs 1 still???
:
: Yes drinking is bad for your liver... but I am starting to believe that Pascal is bad for my total health! :)
:
: : :
: : Try
: :
: : IF wrtnum = 'one' THEN dignum =1;
: : 

: : Make yourself a table of constants to hold the strings ['one', 'two']. Try some more code. This looks a lot like schoolwork. Drinking is bad for your liver...
: :
:
:
OK. I don't want to turn this into one of those threads that won't die, but you don't want to use val. That is used to do a type conversion from string to number. You will probably want to prompt the user to use spaces or dashes to separate the words. You will have to parse their input into individual words. If you make your constants an array, then you can quickly scan thru the array and find a match. Since the user can enter a mixture of cases, you might want to convert their input into all caps. If you think thru the way you make your arrays, the *index* into the array can be used to determine the numeric equivalent of the string. I suppose you could also use a giant case statement. BTW, your "writeln(outerror)" is printing the error code for your conversion which is non-zero indicating that the function is not returning a valid number. KEEP TRYING. Pascal is a good way to learn to program. That's why your university is having you take it.....good luck.
Report
Re: how do you convert a written number in to a digit number??? Posted by Raadya on 3 Nov 2003 at 4:03 AM
This message was edited by Raadya at 2003-11-3 4:8:33

[pre]
Hi, I don't know good english, but I try help to you

here is a program for conversion any word-number to a digital form.

uses CRT;
var
cr,x,y,expnt:byte;
c:char;
delka,cislo:word;
slovo:string;
vysledek:word;
numbers:array[1..4] of string;


const
digits: array[0..20] of string=
'zero','one','two','three','four','five','six','seven','eight','nine', 'ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen','twenty';
digits2:[3..9] of string=
'thirty','forty','fifty','sixty','seventy','eighty','ninety';

begin
ClrScr;
Write ('What is the number? ');
readLN (slovo);

delka:=length(slovo);
cr:=1;
y:=1;
for x:=1 to 4 do numbers[x]:='';

{ this makes from word e.g. 'thirty-five' two words "thirty" and "five" '}
for x:=1 to delka do
begin
if (slovo[x]<>' ' OR (slovo[x]<>'-')) then
numbers[cr]:=numbers[cr]+slovo[x] else cr:=cr+1;
{ one didit ends with the mark <SPACE> or - }
end;


for x:=cr downto 1 do
begin
for y:=0 to 20 do
begin
if numbers[x]=digits[y] then
begin
vysledek:=vysledek+y*expnt;
expnt:=1;
end;

for y:=3 to 9 do
if numbers[x]=digits2[y] then vysledek:=vysledek+y*10;

if numbers[x]='hundert' then exp:=100;
end;


Writeln(' and in digitals is it: ', vysledek);

Writeln;
Writeln(' Press ENTER to end of this program');
ReadLN;
end.



I hope, it will go...
I write it in the school and I havent any shance to try it...

When you will have any questions, write to me at: cz609361@tiscali.cz

Bye


Radim as raadya here

[/pre]







 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.