Jump to content

Error handling of keyboard input


boen_robot

Recommended Posts

I'm doing some practicing in Pascal, as this is, unfortunately, what IT universities accept students with.One issue I'm coming across is input validation. Languages like C++ have a try...catch syntax which would be one way to deal with such a situation in them, but as far as I'm aware, Pascal doesn't have this syntax. So if not like that, how exactly can one validate input to read() or readln()?A particular use case (well... not really... more of a "sample test problem") is ensuring an input filling array items are always integers. I'd like to present an error message when anything else is inputted, or do something else with whatever array items were already filled out.Example code I'm working on:

program TEST;TYPE		intArray = array[1..5] of integer;VAR		i : integer;		inputArray : intArray;BEGIN		for i := 1 to 5 do readln(inputArray[i]);		write('OK!');		readlnEND.

I'd like to somehow skip to the "OK!" message if the latest input to the inputArray is not an integer.One way I can think of is to first store the input in a temporary variable, and then try to cast it into an integer one I'd then add to the array. But I don't know how/if Pascal can do type casting either. And if/how validation can be performed as to whether the casting was successful.So... any ideas?

Link to comment
Share on other sites

Egad! Well, what do you expect with a 37-year-old language?I'm no expert on Pascal, but I used to code for the Mac, and all it's internal code used to be in Pascal, so I got a snootful. But forgive me if I get some usage wrong. You're right, there is no try . . . catch model in elderly languages.Typecasting, when it works, looks like a function.var c : character;var i : Integerc := 'C';i := Integer( c );It only works I believe if the types/vars are the same size. So what I just wrote might not even work!I don't know how to type check. I think in the old days we just compiled stuff till it worked. And some compilers were more snooty than others. And you relied on the operator not to input something stupid. Can't do that today.Aren't there any OLD people on this forum?

Link to comment
Share on other sites

Well then, I guess I'd be hoping no teacher would ever try to feed my Pascal solution something wrong (gasp).Universities actually also allow us to use C or C++ instead, but if the requirements to Pascal programmers are lesser, I may actually chose that one. I mean, if I was a C++ teacher, I'd be sure to have higher requirements to newbies, especially if the interest is high. But if their language of choise is the limit, there isn't much I'd be able to demand.(besides, I only recently learned a little C++, and Pascal is just a piece of cake. Now if I only knew what complier would the comitee be using...)

Link to comment
Share on other sites

Have you tried straight C? After javascript and PHP it will be VERY easy. Until you get to memory allocation, but you should be able to find plenty of snippets on the net to help you through that. I mean, NOBODY uses Pascal anymore. Why not brush up on something useful?

Link to comment
Share on other sites

Have you tried straight C? After javascript and PHP it will be VERY easy. Until you get to memory allocation, but you should be able to find plenty of snippets on the net to help you through that. I mean, NOBODY uses Pascal anymore. Why not brush up on something useful?
They don't teach it in the school, and I couldn't find any tutorials on it. I just found (thanks to aspnetguy once upon a time) a C++ tutorial on cplusplus.com. A lot of the things they say also apply to C, but without having a good C only reference and an IDE, I can't be certain of my own programs.
Link to comment
Share on other sites

So, you have a pascal IDE but no manual?
Um... yes. My closest thing to a manual are all the tutorials I can find. The same goes for C++, but Pascal is just simpler.
Link to comment
Share on other sites

Good god man, there are tons of manuals out there. There's one now, even translated into Bulgarian.

Have you tried straight C? After javascript and PHP it will be VERY easy. Until you get to memory allocation
And pointers, and shared memory, heck, even strings. C doesn't have strings, it has arrays of characters that end with a null character. But using C gives you an appreciation for everything that something like PHP does.
Aren't there any OLD people on this forum?
Isn't that why you're here?
Link to comment
Share on other sites

Think he means old as in 40 years old + :)Isn't there a String object in C though? Or is that C++...

Link to comment
Share on other sites

Okay, I'm past 40. But the Pascal gurus should be older than that.Pascal DOES have pointers, BTW.It sort of has strings, but only 255 bytes long! The last byte tells how long the thing is, because apparently a 1971 microprocessor couldn't be trusted to stop at a null byte, I guess. And anyway, who would need more than 255 characters in a string?laugh.gif

Link to comment
Share on other sites

Isn't there a String object in C though? Or is that C++...
C++ included a string class, that was one of the benefits of C++. C strings are null-terminated arrays of characters.char test_str[] = "string";sizeof(test_str) will return 7, one byte for each character plus the extra null byte at the end.
Link to comment
Share on other sites

Hm... That turned out as a more interesting discussion than I initially planned it for :) .

Good god man, there are tons of manuals out there. There's one now, even translated into Bulgarian.
I'm not exactly keen on buying books, thank you. I'd much rather read online, ideally for free (yeah, I'm much of a skinflint).
And pointers, and shared memory, heck, even strings. C doesn't have strings, it has arrays of characters that end with a null character. But using C gives you an appreciation for everything that something like PHP does.
Sounds like a torture... though I see your point. Non the less, if I won't be using Pascal at the exam, I'd much rather use C++ instead of C.I feel comfortable enough with try..catch (since the PHP equivalent), dynamic memory allocations and file handling, as described in the mentioned C++ tutorial above. I still need to really mix them in something useful (i.e. practice them), but at least I grok them. Trying to grok even more things that I won't be using much anyway seems like an overkill for the exam. I may learn those things sometime, but not in the near future.
Isn't that why you're here?
Ha. Funny :) .
Link to comment
Share on other sites

  • 4 weeks later...

OK. I waited until it happened before I post this...MANY MANY thanks for the book to justsomeguy. I just received it yesterday and I already read the first chapter (well, introduction, and the basic getting started really), and judging by the table of contents, it's a great book I'll be sure to read from the inside out... I sure owe you one.Also, for my original question, I got a PM from a forum member called Bob Kellock, who doesn't have any posts, but seems to know this Pascal stuff. I had to adjust his code a little bit (there were compilation errors), but here's what I finally got:

program test;TypeIntArray = Array[1..5] of Integer;VarInputArray : IntArray;N : Word;I : Integer;S : String;OK : Boolean;begin		N := 0;		OK := True;		While OK AND (N < 5) do		begin				inc(N);				ReadLn(S);				Val(S, InputArray[N], I); {Returns I as non zero when the converted string is not of a type compatible with parameter 2}				OK := I = 0;		end;		if OK then writeln('OK') else writeln('NOT OK');		readln;end.

The really "new" and neat thing is the "val" function. I honestly never even suspected its existsnace. This enspired me to try and find a little more detailed reference than the ones on the Free Pascal page, and I found GNU Pascal's reference as such.

Link to comment
Share on other sites

JSG actually sent you a book? Apparently ###### froze over and I missed the memo. What is it, K&R? A true classic. The simple elegance of strcpy will bring tears to your eyes. High-level languages don't get much lower than that.

Link to comment
Share on other sites

JSG actually sent you a book? Apparently ###### froze over and I missed the memo. What is it, K&R? A true classic. The simple elegance of strcpy will bring tears to your eyes. High-level languages don't get much lower than that.
If by K&R you mean Kernighan&Ritchie, then yes. It's the exact same book justsomeguy referred to previously in this topic.The strcpy comes little surprising btw. I would expect I could do something similar to
string a = "Some string consntant";string b = a;

or is there something else I'm missing (like if the above would also copy any "\0" characters like strcpy)?

Link to comment
Share on other sites

string a = "Some string consntant"; string b = a;

or is there something else I'm missing (like if the above would also copy any "\0" characters like strcpy)?

Yeah, you'd think, but that only works in a language that does the real work for you. Ever wonder what the chips are actually doing when you assign a value to variable? Operators operate. Some little demon that lives in the machine has to physically go the place where the "S" is stored, figure out what it is, and then go to an available memory location and say, "You're going to hold an 'S' now." Then it does the same for all the other characters. It stops after it copies the null byte. strcpy is a library function that does all that. K&R show you exactly how it works. You're in the belly of the beast, my friend. And that is why operating systems and php interpreters are written in C in the first place.
Link to comment
Share on other sites

Yeah, enjoy the book. You seem to have an interest in programming so hopefully you'll have fun with it. C really is a fun language to use. My favorite programmer, John Carmack, revolutionized the video game industry by creating the first 3D games, Wolfenstein 3D, Doom, Quake, etc, by writing his own graphics engine using C. He solved a lot of problems (textured surfaces, early dynamic lighting) that other people were having a lot of problems with. I think he finally switched to C++ for Doom 3. Anyway, I've started reading through the book again myself (the classics never get old), I'm about to go out and get myself an Asus Eee PC so that I can have a small computer with Linux and a C compiler, then I'm going back through the K&R tutorial to do the different exercises. Even after all these years it's still fun to write a C program to print a vertical graph or strip comments from source code.

Link to comment
Share on other sites

No rise to my bait, jsg? I certainly didn't mean to offend . . .
Maybe he took it as a compliment? :) I know I would.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...