User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Nov 2007
Posts: 44
Reputation: Gotovina7 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Gotovina7 Gotovina7 is offline Offline
Light Poster

Re: Pascal calculator program

  #31  
Nov 24th, 2007
 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.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,825
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 187
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Pascal calculator program

  #32  
Nov 24th, 2007
Yes, it looks good, except that you aren't returning the value of num.

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 := num
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:
  1. procedure ignorespaces( var c: char );
  2. begin
  3. while c = ' ' do
  4. read( file1, c )
  5. end;
All your procedures and functions will have the same issue: they'll have to have a var c: char in the parameter list.

Hope this helps.
Last edited by Duoas : Nov 24th, 2007 at 8:39 pm.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,825
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 187
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Pascal calculator program

  #33  
Nov 24th, 2007
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:
  1. program foo;
  2.  
  3. procedure ignorespaces( var f: textFile; var c: char );
  4. begin
  5. while c = ' ' do read( f, c )
  6. end;
  7.  
  8. { The following variables are 'local' to the main program's block. }
  9. var
  10. my_file: textFile;
  11. my_char: char;
  12.  
  13. begin
  14. ...
  15. ignorespaces( my_file, my_char );
  16. ...
  17. 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.
  1. var
  2. my_file: textFile;
  3. my_char: char;
  4. another_file: textFile;
  5. another_char: char;
  6.  
  7. begin
  8. ...
  9. ignorespaces( my_file, my_char );
  10. ignorespaces( another_file, another_char );
  11. ...
  12. end.

Hope this helps.
Last edited by Duoas : Nov 24th, 2007 at 8:47 pm.
Reply With Quote  
Join Date: Nov 2007
Posts: 44
Reputation: Gotovina7 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Gotovina7 Gotovina7 is offline Offline
Light Poster

Re: Pascal calculator program

  #34  
Nov 24th, 2007
AHHHHHHH i see now. Changing this stuff gives me a new error

 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'
Reply With Quote  
Join Date: Nov 2007
Posts: 44
Reputation: Gotovina7 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Gotovina7 Gotovina7 is offline Offline
Light Poster

Re: Pascal calculator program

  #35  
Nov 24th, 2007
If I understand correctly, this is what I have

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.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,825
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 187
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Pascal calculator program

  #36  
Nov 24th, 2007
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
Var
   error   : boolean;
   num1    : integer;
   my_file : text;
   my_char : char;
block and move it so that it is immediately before the 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.
Reply With Quote  
Join Date: Nov 2007
Posts: 44
Reputation: Gotovina7 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Gotovina7 Gotovina7 is offline Offline
Light Poster

Re: Pascal calculator program

  #37  
Nov 24th, 2007
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.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,825
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 187
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Pascal calculator program

  #38  
Nov 24th, 2007
> 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:
  1. function find_digits(var c : char; var f : text): integer;
  2. Var
  3. num : integer;
  4. begin
  5. num := 0;
  6. while c in ['0'..'9'] do
  7. begin
  8. num := num * 10 + (ord(c) - ord('0'));
  9. read(f,c);
  10. end;
  11. find_digits := num;
  12. end; { find_digits }
(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:
1+2+3
-9+10
12*-2
5*(2+3)
(2*(1+3))-1
And some invalid ones:
3,7
+
We're #1!

Hope this helps.
Reply With Quote  
Join Date: Nov 2007
Posts: 44
Reputation: Gotovina7 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Gotovina7 Gotovina7 is offline Offline
Light Poster

Re: Pascal calculator program

  #39  
Nov 25th, 2007
Originally Posted by Duoas View Post
> 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:
  1. function find_digits(var c : char; var f : text): integer;
  2. Var
  3. num : integer;
  4. begin
  5. num := 0;
  6. while c in ['0'..'9'] do
  7. begin
  8. num := num * 10 + (ord(c) - ord('0'));
  9. read(f,c);
  10. end;
  11. find_digits := num;
  12. end; { find_digits }
(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.


Ill think about it, I also HAVE to use arrays in this assignment.
Reply With Quote  
Join Date: Nov 2007
Posts: 44
Reputation: Gotovina7 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Gotovina7 Gotovina7 is offline Offline
Light Poster

Re: Pascal calculator program

  #40  
Nov 25th, 2007
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Pascal and Delphi Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Pascal and Delphi Forum

All times are GMT -4. The time now is 5:51 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC