•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Pascal and Delphi section within the Software Development category of DaniWeb, a massive community of 397,589 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,996 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Pascal and Delphi advertiser:
Views: 5812 | Replies: 62
![]() |
•
•
Join Date: Nov 2007
Posts: 44
Reputation:
Rep Power: 1
Solved Threads: 0
program Assignment6(input, output);
Var
file1 : text;
c : char;
error : boolean;
num1 : integer;
function convert(c: char): integer;
begin
convert := ord(c) - ord('0');
end; { convert }
function find_digits(var c : char): integer;
Var
num : integer;
begin
num := 0;
while (ord(c) >= ord('0')) and (ord(c) <= ord('9')) do
begin
num := num * 10 + (ord(c) - ord('0'));
read(file1, c);
end;
end; { find_digits }
procedure ignorespaces;
begin
while c = ' ' do
read(file1, c);
end; { ignore_spaces }
BEGIN
assign(file1, 'input.txt');
reset(file1);
read(file1, c);
error := false;
ignorespaces;
If ((ord(c) >= ord('0')) and (ord(c) <= ord('9'))) then
begin
find_digits;
num1 := find_digits(c);
end;
end.
ok it makes more sense having it an function. Did I write the function find_digits right? Is the num suppose to be declared like that? Would I need to change anything in the main program now? Getting these errors c6.p: In function `Find_digits':
c6.p:25: warning: return value of function not assigned
c6.p: In main program:
c6.p:45: too few arguments to function `Find_digits'
c6.p:45: warning: function call as a statement - value is ignored
EDIT: Ok i got most of the errors down, last one is giving me a hard time.
warning: return value of function not assigned
Last edited by Gotovina7 : Nov 24th, 2007 at 8:25 pm.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,825
Reputation:
Rep Power: 11
Solved Threads: 187
Yes, it looks good, except that you aren't returning the value of num.
Notice in "convert" you return the value
In any BP dialect, you can also say:
The "find_digits" function goes through all the trouble of calculating "num", but it fails to say:
at the end...
You forgot to remove the "find_digits" all by itself right after the "begin". The next one is correct.
Good job!
[EDIT] Oh yeah, the "ignorespaces" needs to account for the next character the same way:
All your procedures and functions will have the same issue: they'll have to have a
Hope this helps.
Notice in "convert" you return the value
ord(c) - ord('0') by "assigning it" to the function name:convert := ord(c) - ord('0')In any BP dialect, you can also say:
result := ord(c) - ord('0')The "find_digits" function goes through all the trouble of calculating "num", but it fails to say:
find_digits := numat the end...
You forgot to remove the "find_digits" all by itself right after the "begin". The next one is correct.
Good job!
[EDIT] Oh yeah, the "ignorespaces" needs to account for the next character the same way:
Pascal Syntax (Toggle Plain Text)
procedure ignorespaces( var c: char ); begin while c = ' ' do read( file1, c ) end;
var c: char in the parameter list.Hope this helps.
Last edited by Duoas : Nov 24th, 2007 at 8:39 pm.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,825
Reputation:
Rep Power: 11
Solved Threads: 187
OK, sorry for the double post.
The reason it hasn't complained about "c" is that it is a global variable, which I am trying to get you to avoid using. The same is true of "file1". In all your procedures and functions, you could get rid of the "var c: char" parameter and your program would work fine.
What I would like to see, though, is something like this:
This makes your code more modular and more understandable. The "ignorespaces" function only cares about information given to it. You could use it on any file you want --even multiple files at the same time.
Hope this helps.
The reason it hasn't complained about "c" is that it is a global variable, which I am trying to get you to avoid using. The same is true of "file1". In all your procedures and functions, you could get rid of the "var c: char" parameter and your program would work fine.
What I would like to see, though, is something like this:
Pascal Syntax (Toggle Plain Text)
program foo; procedure ignorespaces( var f: textFile; var c: char ); begin while c = ' ' do read( f, c ) end; { The following variables are 'local' to the main program's block. } var my_file: textFile; my_char: char; begin ... ignorespaces( my_file, my_char ); ... end.
This makes your code more modular and more understandable. The "ignorespaces" function only cares about information given to it. You could use it on any file you want --even multiple files at the same time.
Pascal Syntax (Toggle Plain Text)
var my_file: textFile; my_char: char; another_file: textFile; another_char: char; begin ... ignorespaces( my_file, my_char ); ignorespaces( another_file, another_char ); ... end.
Hope this helps.
Last edited by Duoas : Nov 24th, 2007 at 8:47 pm.
•
•
Join Date: Nov 2007
Posts: 44
Reputation:
Rep Power: 1
Solved Threads: 0
AHHHHHHH i see now. Changing this stuff gives me a new error
thanks for all the help. Everything is making sense now. I get an error on the part I bolded in the code.
too few arguments to function `Ignorespaces'
program Assignment6(input, output);
Var
file1 : text;
c : char;
error : boolean;
num1 : integer;
function convert(c: char): integer;
begin
convert := ord(c) - ord('0');
end; { convert }
function find_digits(var c : char): integer;
Var
num : integer;
begin
num := 0;
while (ord(c) >= ord('0')) and (ord(c) <= ord('9')) do
begin
num := num * 10 + (ord(c) - ord('0'));
read(file1, c);
find_digits := num;
end;
end; { find_digits }
procedure ignorespaces(var c : char);
begin
while c = ' ' do
read(file1, c);
end; { ignore_spaces }
BEGIN
assign(file1, 'input.txt');
reset(file1);
read(file1, c);
error := false;
ignorespaces;
If ((ord(c) >= ord('0')) and (ord(c) <= ord('9'))) then
begin
num1 := find_digits(c);
end;
end.
thanks for all the help. Everything is making sense now. I get an error on the part I bolded in the code.
too few arguments to function `Ignorespaces'
•
•
Join Date: Nov 2007
Posts: 44
Reputation:
Rep Power: 1
Solved Threads: 0
If I understand correctly, this is what I have
It now compiles if I add a writeln(num1); after 'num1 := find_digits(my_char);" I get an output as 5 (that is the first digit in the equation)
program Assignment6(input, output);
Var
error : boolean;
num1 : integer;
my_file : text;
my_char : char;
function convert(c: char): integer;
begin
convert := ord(c) - ord('0');
end; { convert }
function find_digits(var c : char): integer;
Var
num : integer;
begin
num := 0;
while (ord(c) >= ord('0')) and (ord(c) <= ord('9')) do
begin
num := num * 10 + (ord(c) - ord('0'));
read(my_file, my_char);
find_digits := num;
end;
end; { find_digits }
procedure ignorespaces(var f : text; var c : char);
begin
while c = ' ' do
read(f, c);
end; { ignore_spaces }
BEGIN
assign(my_file, 'input.txt');
reset(my_file);
read(my_file, my_char);
error := false;
ignorespaces(my_file, my_char);
If ((ord(my_char) >= ord('0')) and (ord(my_char) <= ord('9'))) then
begin
num1 := find_digits(my_char);
end;
end.
It now compiles if I add a writeln(num1); after 'num1 := find_digits(my_char);" I get an output as 5 (that is the first digit in the equation)
Last edited by Gotovina7 : Nov 24th, 2007 at 9:14 pm.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,825
Reputation:
Rep Power: 11
Solved Threads: 187
Beautiful!
Now that you have the 5, what are you going to do with it?
[EDIT] Wait! Er... Your find_digits is using the global variables 'my_file' and 'my_char'. To help you out, I would like you to take that entire
block and move it so that it is immediately before the
Your program will then not compile. See if you can fix it so that it does (without putting the var block back at the top).
Now that you have the 5, what are you going to do with it?
[EDIT] Wait! Er... Your find_digits is using the global variables 'my_file' and 'my_char'. To help you out, I would like you to take that entire
Var error : boolean; num1 : integer; my_file : text; my_char : char;
BEGIN.Your program will then not compile. See if you can fix it so that it does (without putting the var block back at the top).
Last edited by Duoas : Nov 24th, 2007 at 9:33 pm.
•
•
Join Date: Nov 2007
Posts: 44
Reputation:
Rep Power: 1
Solved Threads: 0
program Assignment6(input, output);
function convert(c: char): integer;
begin
convert := ord(c) - ord('0');
end; { convert }
function find_digits(var my_char : char; var my_file : text): integer;
Var
num : integer;
begin
num := 0;
while (ord(my_char) >= ord('0')) and (ord(my_char) <= ord('9')) do
begin
num := num * 10 + (ord(my_char) - ord('0'));
read(my_file, my_char);
find_digits := num;
end;
end; { find_digits }
procedure ignorespaces(var f : text; var c : char);
begin
while c = ' ' do
read(f, c);
end; { ignore_spaces }
Var
error : boolean;
num1 : integer;
my_file : text;
my_char : char;
BEGIN
assign(my_file, 'input.txt');
reset(my_file);
read(my_file, my_char);
error := false;
ignorespaces(my_file, my_char);
If ((ord(my_char) >= ord('0')) and (ord(my_char) <= ord('9'))) then
begin
num1 := find_digits(my_char, my_file);
end;
end.
Is this correct? Now, would A loop allow me to get all the numbers from the equation? If I put the loop right after the num1 := find_digits(my_char, my_file);
Last edited by Gotovina7 : Nov 24th, 2007 at 9:52 pm.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,825
Reputation:
Rep Power: 11
Solved Threads: 187
> Is this correct?
Perfect.
Keep in mind, you don't have to use the same names in your function as you do for the globals. Your function could be:
(I made a couple of other changes too, just to show you how you can think of some things...)
> Now, would A loop allow me to get all the numbers from the equation? If I put the loop right after the num1 := find_digits(my_char, my_file);
You are going to have to think about this one with the construction paper and crayons a little bit. What is the next character you read going to look like? (You already know it is not a digit, otherwise find_digits would have read it. So when find_digits finishes and returns a number, my_char is hopefully going to be one of '+' or '-' or '*' or '(' or something like that. If not it is invalid and you can complain about the equation.) Here again is a list of valid equations:
And some invalid ones:
Hope this helps.
Perfect.
Keep in mind, you don't have to use the same names in your function as you do for the globals. Your function could be:
Pascal Syntax (Toggle Plain Text)
function find_digits(var c : char; var f : text): integer; Var num : integer; begin num := 0; while c in ['0'..'9'] do begin num := num * 10 + (ord(c) - ord('0')); read(f,c); end; find_digits := num; end; { find_digits }
> Now, would A loop allow me to get all the numbers from the equation? If I put the loop right after the num1 := find_digits(my_char, my_file);
You are going to have to think about this one with the construction paper and crayons a little bit. What is the next character you read going to look like? (You already know it is not a digit, otherwise find_digits would have read it. So when find_digits finishes and returns a number, my_char is hopefully going to be one of '+' or '-' or '*' or '(' or something like that. If not it is invalid and you can complain about the equation.) Here again is a list of valid equations:
•
•
•
•
1+2+3
-9+10
12*-2
5*(2+3)
(2*(1+3))-1
•
•
•
•
3,7
+
We're #1!
Hope this helps.
•
•
Join Date: Nov 2007
Posts: 44
Reputation:
Rep Power: 1
Solved Threads: 0
•
•
•
•
> Is this correct?
Perfect.
Keep in mind, you don't have to use the same names in your function as you do for the globals. Your function could be:
(I made a couple of other changes too, just to show you how you can think of some things...)Pascal Syntax (Toggle Plain Text)
function find_digits(var c : char; var f : text): integer; Var num : integer; begin num := 0; while c in ['0'..'9'] do begin num := num * 10 + (ord(c) - ord('0')); read(f,c); end; find_digits := num; end; { find_digits }
> Now, would A loop allow me to get all the numbers from the equation? If I put the loop right after the num1 := find_digits(my_char, my_file);
You are going to have to think about this one with the construction paper and crayons a little bit. What is the next character you read going to look like? (You already know it is not a digit, otherwise find_digits would have read it. So when find_digits finishes and returns a number, my_char is hopefully going to be one of '+' or '-' or '*' or '(' or something like that. If not it is invalid and you can complain about the equation.) Here again is a list of valid equations:
And some invalid ones:
Hope this helps.
Ill think about it, I also HAVE to use arrays in this assignment.
•
•
Join Date: Nov 2007
Posts: 44
Reputation:
Rep Power: 1
Solved Threads: 0
program Assignment6(input, output);
function convert(c: char): integer;
begin
convert := ord(c) - ord('0');
end; { convert }
function find_digits(var my_char : char; var my_file : text): integer;
Var
num : integer;
begin
num := 0;
while (ord(my_char) >= ord('0')) and (ord(my_char) <= ord('9')) do
begin
num := num * 10 + (ord(my_char) - ord('0'));
read(my_file, my_char);
find_digits := num;
end;
end; { find_digits }
procedure ignorespaces(var f : text; var c : char);
begin
while c = ' ' do
read(f, c);
end; { ignore_spaces }
Var
error : boolean;
num1, num2 : integer;
my_file : text;
my_char : char;
temp_result : real;
oper : char;
BEGIN
assign(my_file, 'input.txt');
reset(my_file);
read(my_file, my_char);
error := false;
ignorespaces(my_file, my_char);
num1 := find_digits(my_char, my_file);
temp_result := num1;
oper := my_char;
If oper = '+' then
begin
temp_result := temp_result + num2;
writeln(temp_result);
end;
If oper = '-' then
begin
temp_result := temp_result - num2;
writeln(temp_result);
end;
If oper = '*' then
begin
temp_result := temp_result * num2;
writeln(temp_result);
end;
If oper = '/' then
begin
temp_result := temp_result / num2;
writeln(temp_result);
end;
end.
I am pretty stumped now, that is all I could think of (look at code). Now, I am having troubles figuring out how to read the second number "num2". The writeln are just in there for testing.
Last edited by Gotovina7 : Nov 25th, 2007 at 1:10 am.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Pascal and Delphi Marketplace
- Really need help with the interactive Calculator program, or will be dead (C++)
- pascal triangle program (C++)
- starting a calculator program in C (C)
- Java Swing Calculator program not running. It has 0 errors (Java)
- Wierd error messages with calculator program (C++)
- need help with calculator program in C (C)
- C++ Calculator Program (C++)
Other Threads in the Pascal and Delphi Forum
- Previous Thread: RES file creaton under Vista
- Next Thread: Deleting components in D2007



Linear Mode