Jump to content
  • Announcements

    • boen_robot

      Guidelines and Netiquette   03/28/2017

      Posting Problems:   Having problems posting your topic? Read through this: To join, you agree to our terms and conditions and fill out and submit a registration form. An activation email will be sent to your email adress, so you'll need to verify your account. After that the account has to be validated by one of the moderators. This will mean that it can take up to a day to be activated. A couple of things to remember to ensure approval: Don't use an email address in one of those $2 four character .com domains eg. xyds.com. These will be deleted and the domain added to the banned list. Don't use an email address that is within a domain with a bad reputation for spam. A Google search is run on every email address and email domain. Don't sign up with an email address that doesn't exist, doesn't work or requires the sender to answer a quiz before their email can get to you. Put your country and or state and city in the signup form. Blank forms will go to the botton of the "to do" list. And make sure that your email address and your country match, saying you're from Alabama and using a .ru email address is not going to get you activated. After a membership is activated the first few posts will be monitored. Posting spam or unapproved topics described in the agreement results in an immediate ban. The email provider and the IP addresses associated with the account will be banned and all posts will be deleted. These strict measures have been deemed necessary to hinder spam. Sorry for any inconvenience this causes, but it's not liable to change. If, after reading this, you still can't post and don't understand why, contact one of the Moderators listed here.   Topic Guidelines   Including the following information can expedite an accurate response from board members: Must be a Specific Problem or Question related to web design and development Include Code in Question (wrap with   for small blocks of code and for longer blocks   ) Include Code Author Include Extra Notes/Modifications/Attempts Include web link to page/file when possible Content Guidelines   You may not post, upload, link to, or email any Content that contains, promotes, gives instruction about, or provides prohibited Content. Prohibited Content includes any Content that breaks any local, state, county, national or international law. Prohibited Content also includes: No direct or indirect advertising or websites, forums, products, services No hijacking of posts (do not post your question in someone elses) Content that infringes upon any rights [ex. MP3s and ROMs] (including, but not limited to, copyrights and trademarks) Abusive, threatening, defamatory, racist, or obscene Viruses or any other harmful computer software False Information or libel Spam, chain letters, or Pyramid schemes Gambling or Illicit drugs Terrorism Hacking or cheating for internet/online games Warez, Roms, CD-Keys, Cracks, Passwords, or Serial Numbers Pornography, nudity, or sexual material of any kind Excessive profanity Invasive of privacy or impersonation of any person/entity Hacking materials or information Posting Tips   There are more BBcodes than there are buttons for on the reply menu. To get the full list, click "BB Code Help" underneath the clickable smilie face menu. Use   for small snippets of code Use   for lengthy snippets of code Use   if your snippet is HTML (optional) Use   if your snippet is SQL (optional) Rules of Conduct   Be nice. There's no need for calling someone stupid if they ask an 'easy' question. Keep your avatars and signatures absolutely child friendly. We have a younger audience on this forum. Keep your language appropriate for the same reason above. Do not PM moderators for help on the forum. Post on the topic, or create a new one.   Spam:   Recently, as you have all without doubt noticed, we have had lots of spam and advertisement on the forum. Therefore, we'd like to alert you as to what to do when you have found any of the aforementioned annoying messages: it. Immediately. Give a clear reason, please, if the advertising is not evident. DO NOT POST! Report, let the post stay as is, and we will get to it, meanwhile if you continue to post as normal in the other threads, it won't be on the top so long. Refrain from PMing the member. This won't help at all, as they are most likely spambots anyway. Thank you.       Images in signatures:   After thinking of users on dial-up, we have decided to enforce the following rules regarding signatures. Please pay heed to them. Respecting these rules is respecting the members on this forum with dial-up. Signature rules: No animated images AT ALL. No matter the amount of animation. Maximum image widthxheight: 300x150 Maximum image (file) size: 15kb Use calm colors. Do not use highly contrasting images in your signature, as this can get really annoying when seeing several posts from one member in the same thread. The same prohibited content goes for images as for posts. Lastly, use common sense. No lengthy signatures please. Save us some scrolling. Thank you.       Links in signatures:   Please understand that w3schools.com only exists because of voluntary work and is barely supported by the advertising littered throughout the tutorials and the forum. So, please, stop advertising other sites. DO not post links that drive traffic away from the w3schools domain - especially to a site that offers similar if not identical information. Please help support the site by keeping individuals on it. Thank you. Here are some guidelines as to what you can put in your signature: w3schools links --> allowed w3.org links --> allowed browser links --> allowed html editor links --> allowed personal sites --> allowed tutorial sites competing with w3schools --> NOT allowed sites completely irrelevant to webprogramming and this forum --> NOT allowed   Thanks for understanding, and for taking the time to read this. ~W3Schools Modstaff~
boen_robot

Error handling of keyboard input

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?

Share this post


Link to post
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?

Share this post


Link to post
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...)

Share this post


Link to post
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?

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Well, good luck. I don't notice anyone in a big hurry to join this thread. But there's stuff out there. Probably.

Share this post


Link to post
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?

Share this post


Link to post
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++...

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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 :) .

Share this post


Link to post
Share on other sites

If you want to learn C, send me your address and I'll buy the damn book. It's going to be in English though (as God intended).

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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.

Share this post


Link to post
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)?

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
I didn't take any offense, I'm all business this week.
Good man. Someone's gotta keep the economy alive, and it might as well be you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×