> Sorry for such a late reply!
Heh, don't worry about it. Sometimes I'll mull things over for a day or two before replying to a thread, just to make sure I give the most intelligent answer I can. When I just automatically hit the "reply" button is when I tend to get into trouble...
I understand about the " vs '. Your compiler is weird, as that's illegal Pascal. The proper way is to double the ' character.
There's a technical term for this but I can never remember it: global variables aren't evil. But they should be avoided. The reason has to do with "state complexity". Computers always do exactly what they are told. We humans have trouble remembering more than a few things at a time, though, so when we have a lot of global variables accessed by a zillion procedures, we can't keep track of what state the program is in at any given time. This is part of the problem you are having with your assignment: there's too much information to remember at once.
The point behind "modular programming" is to reduce the "state" to smaller units that don't depend upon each other. For example, your code currently requires each routine to be aware of which one of digit1, digit2, digit3 it can change at any given time. That's dangerous, because if you mix up the routines the wrong way then you clobber data that you need. You are better off keeping things separate.
For example, any routine that reads from the input can take a reference parameter to the last-read character. What that means is that it only has to worry about
one character -the one given to it. If it needs other variables for its personal use, it can do so without worrying about damaging something elsewhere.
function isDigit( c: char ): boolean;
begin
result := c in ['0'..'9']
end;
function readNumber( var f: textFile; var last_c: char ): integer;
begin
result := 0;
while isDigit( last_c ) do begin
result := (result *10) +ord( last_c ) -ord( '0' );
read( f, last_c )
end
end;
Notice how in this code the last_c reference is always the last character read from the file. If you call the function and the last-read-character is
not a digit, then things still work properly: the file hasn't been read any more, the last-read-char hasn't been changed, and you get zero as a result (since no number was read).
The other thing to notice is that the code doesn't rely upon
anything other than what was given to it. It doesn't need global variables. It doesn't care what the current state of the program is. All it cares about is the last-read-character and the file --which is all it should care about.
Now, when you
use such a procedure,
you don't have to care about anything else either! That's great! It frees your mind to worry about the big picture instead of nitty details.
// continued from above
...
// These variables are technically global, but they are limited
// to use in the main block, so they are OK. (They are treated
// as local to the main program block.)
var c: char;
begin
writeln( 'Type lots of stuff, including numbers. Type ^Z, ENTER to quit.' );
read( c );
while not eof do
begin
if isDigit( c )
then writeln( 'You typed the number ', readNumber( input, c ), '.' )
else
if c in ['A'..'Z']
then writeln( 'You typed a capital letter!' );
read( c )
end;
writeln( 'Good-bye.' )
end.
This is what I mean by good vs bad programming.
All programming is about how you think. The clarity of your own thought processes manifest themselves in the programs you write. It doesn't matter if you use Pascal or C++ or Java or Scheme or whatever. What matters is
how you think about it. Learning to program is, in the most correct sense, learning to think.
Now, don't feel to bad. You are just beginning, but you now have a knowledge that will help you learn to program well: by learning to examine your own thought processes you learn to program. So, if something seems big, or unmanageable, think about how to break it down into smaller tasks. Once done, concentrate on one task at a time. Smaller tasks may themselves be broken into even smaller tasks, and so on. (This is called the "top-down" approach, BTW.) Then, combine your smaller tasks together and build back up to a completed program.
I hope this makes sense. If I have confused you about any particular thing, ask for more.
[EDIT] I can't answer your question about loops without more info. Did you read y and forget to set legalinteger before the while statement? Things only work in order... Frankly, I'd use isDigit instead of legalinteger, since isDigit always gives the correct answer, anytime, where legalinteger might not be right if it isn't set right before you need it...
Hope this helps.