Computing.Net > Forums > Programming > Beginners help Pascal

Beginners help Pascal

Reply to Message Icon

Original Message
Name: goosh
Date: March 27, 2003 at 11:59:30 Pacific
Subject: Beginners help Pascal
OS: XP pro
CPU/Ram: 700/512
Comment:

Hi can anyone tell me what's wrong with this "if" statement please?

uses crt;
var
age, name, sex, occupation:string;

procedure change_colors;
begin
textcolor (lightblue);
end; {change_colors}


procedure get_details;
{ask for personal details}
begin
write ('What is your names? ');
readln (name);
write ('What age are you? ');
readln (age);
if age = 0 then
gotoxy (35,10);
write ('Grow up quick!')
else
write ('What sex are you? ');
readln (sex);
write ('What is your occupation? ');
readln (occupation);
end; {get_details}

procedure print_details;
{prints out the details entered}
begin
repeat
begin

end;
gotoxy (20,10)
write ('Name: ');
gotoxy (20,11);
write ('Age: ');
gotoxy (20,12);
write ('Sex: ');
gotoxy (20,13);
write ('Occupation: ');
gotoxy (35,10)
write (name);
gotoxy (35, 11);
write (age);
gotoxy (35,12);
write (sex);
gotoxy (35,13);
write (occupation);
end; {print_details}

begin {main program}
get_details;
print_details;
end. {main program}

I am getting the 'Error 26 Type mismatch' message with the cursor flashing under the 'then'.
Lol no flaming please I'm a newbie!
Thanks for any replies.



Report Offensive Message For Removal

Response Number 1
Name: BelAnWel
Date: March 27, 2003 at 13:31:33 Pacific
Subject: Beginners help Pascal
Reply: (edit)

i havent used pascal in ages and its a little rusty but i think that when u use an if... statement the syntax is something like "if age == 0 then". try that.


Report Offensive Follow Up For Removal

Response Number 2
Name: BelAnWel
Date: March 27, 2003 at 13:36:49 Pacific
Subject: Beginners help Pascal
Reply: (edit)

sorry but i search for the if statement for pascal and the reply i gave u was wrong. u just use 1 "=" for the if statement.

i notice that u dont have a ";" at the end of "write ('Grow up quick!')" might be just a typo.


Report Offensive Follow Up For Removal

Response Number 3
Name: Skvat
Date: March 27, 2003 at 15:58:43 Pacific
Subject: Beginners help Pascal
Reply: (edit)

It has been a few years for me too, but i guess its the same as in Java... If there are more than one statement to be called you need if (bla == bla) { bla};

in Delphi it whould be:

if bla = 0 then BEGIN bal bla END;

hope this will help! :)


Report Offensive Follow Up For Removal

Response Number 4
Name: George
Date: March 27, 2003 at 20:37:21 Pacific
Subject: Beginners help Pascal
Reply: (edit)

Hi Goosh:
It's been a long time, but I played with the code. Bear in mind, this is not what you started with, but what I modified to make it work. Go from there. I have not cleaned up the screen or the like.

Program Test;
uses crt;
var
age, name, sex, occupation:string;

{goosh you declared age as a string, above}
procedure change_colors;
begin
textcolor (lightblue);
end; {change_colors}


procedure get_details;
{ask for personal details}
begin
write ('What is your names? ');
readln (name);
write ('What age are you? ');
readln (age);
if (age = '0') then {but here your were trying to read as an integer! I changed to read as a string. Need to keep the right typecast.}
Begin
gotoxy (35,10);
write ('Grow up quick!')
end
else
write ('What sex are you? ');
readln (sex);
write ('What is your occupation? ');
readln (occupation);
end; {get_details}

procedure print_details;
{prints out the details entered}
begin {here you had a repeat statement but no condition to stop it, I played with the code in the main to get around this, take a look at the sample and play with it a little.}
clrscr; {needed to clear the screen}
gotoxy (20,10); {add semicolon}
write ('Name: ');
gotoxy (20,11);
write ('Age: ');
gotoxy (20,12);
write ('Sex: ');
gotoxy (20,13);
write ('Occupation: ');
gotoxy (35,10); {add semicolon}
write (name);
gotoxy (35, 11);
write (age);
gotoxy (35,12);
write (sex);
gotoxy (35,13);
write (occupation);
end; {print_details}{1}

begin {main program}
clrscr;
repeat
get_details;
print_details;
until name='sammy';
end. {main program}


Report Offensive Follow Up For Removal

Response Number 5
Name: George
Date: March 27, 2003 at 20:40:54 Pacific
Subject: Beginners help Pascal
Reply: (edit)

By the way goosh, I used clrscr, this is a command in Borland Pascal 7.0. You will have to convert this to the version of Pascal you are using.

George


Report Offensive Follow Up For Removal


Response Number 6
Name: goosh
Date: March 28, 2003 at 06:30:36 Pacific
Subject: Beginners help Pascal
Reply: (edit)

Hey guys thanks for replying. Thanks George for putting a lot of time into it for me. What you did worked. I didn't even think about age being a string lol. Anyway I changed it to integer and used your program and it was fine. I accidently left the repeat statement at the end of the program. The reason I was messing with repeat is this:

uses crt;
var
age, next:integer;
name, sex, occupation:string;

procedure change_colors;
begin
textcolor (lightblue);
end; {change_colors}


procedure get_details;
{ask for personal details}
begin

write ('What is your name? ');
readln (name);
repeat
write ('What age are you? ');
readln (age);
next := 10;
next := next + 1;
if (age 0;
write ('What sex are you? ');
readln (sex);
write ('What is your occupation? ');
readln (occupation);
end; {get_details}

What I'm trying to do is to get it to keep asking the age after printing "Grow up quick" and it's doing this in a way but what it's doing is leaving the line "Grow up quick in the same area and also the "What age are you?" in the same bit and if I press 0 then nothing happens until I press a number > 0 then it carries on with the rest of the program. How would I get it to keep saying "Grow up quick" after = 0 but on a different line as to the previous one and also to ask "what age are you?" on a line below? As you can see I tried to do it with the variable 'next' hoping 'next = 10' 'next = next + 1' would bring it down a line each time but it doesn't.
Thanks again for helping me out!


Report Offensive Follow Up For Removal

Response Number 7
Name: elric
Date: March 28, 2003 at 07:14:27 Pacific
Subject: Beginners help Pascal
Reply: (edit)

G'day,

Not quite sure what you are trying to achieve, but you are aware that:
next := 10;
next := next+1;
Is exactly the same as saying:
next := 11 !!
Are you trying to trap an error??
In that case, something like this would work:

write('What age are you??');
readln(age);
if (age [= 0) THEN BEGIN (* Define it as an integer*)
REPEAT
writeln ('This is invalid:try again');
(* Use the same gotoxy value as the original question *)
readln(age);
UNTIL age ]= 0
END;

Just use the same gotoxy value, so that you overwrite the original message (pad with spaces if necessary)
I haven't had time to run this through the compiler so there might be the odd bug lurking (not to mention obvious syntax errors)..
regards,
Elric


Report Offensive Follow Up For Removal

Response Number 8
Name: elric
Date: March 28, 2003 at 07:28:28 Pacific
Subject: Beginners help Pascal
Reply: (edit)

G'day,

Just had another look at your post. I think you have typed the dreaded greater than and less than signs in your post. I assume that there was an UNTIL statement between the next :=10 and next := next +1 lines...
Bit of a limitation for a programming forum, but as long as we are aware of it..
regards,
Elric


Report Offensive Follow Up For Removal

Response Number 9
Name: goosh
Date: March 28, 2003 at 09:01:54 Pacific
Subject: Beginners help Pascal
Reply: (edit)

Hi Elric my Guru lol just a quick post before I try your code out. What I posted up there is not what I've written.

procedure get_details;
{ask for personal details}
begin

write ('What is your name? ');
readln (name);
repeat
write ('What age are you? ');
readln (age);

if (age 0;
write ('What sex are you? ');
readln (sex);
write ('What is your occupation? ');
readln (occupation);
end; {get_details}

That is what I meant to post. I will have a look at yours now thanks.


Report Offensive Follow Up For Removal

Response Number 10
Name: goosh
Date: March 28, 2003 at 09:19:02 Pacific
Subject: Beginners help Pascal
Reply: (edit)

Hi Elric this is what I pout in (I think it's the same as what you said):

procedure get_details;
{ask for personal details}
begin

write ('What is your name? ');
readln (name);

write ('What age are you? ');
readln (age);

if (age 0;
end;
write ('What sex are you? ');
readln (sex);
write ('What is your occupation? ');
readln (occupation);
end; {get_details}


What happens now is when I enter 0 I get the message "Grow up quick" but the program just stops and no matter how many times I input a value 0 then the program proceeds. What I'm trying to do is get this output when a value 0 is input. Do you see what I mean?
Thanks for helping me! Write back soon remember you're being made CEO of goosh.com (formerly known as microsoft.com) hehe.


Report Offensive Follow Up For Removal

Response Number 11
Name: goosh
Date: March 28, 2003 at 09:22:51 Pacific
Subject: Beginners help Pascal
Reply: (edit)

Oh I forgot to mention. The reason I was writing 'next := 10' 'next := next + 1' is I was trying to get the phrase "grow up quick" to jump down the screen one line at a time. Although i didn't put that code in my last post.


Report Offensive Follow Up For Removal

Response Number 12
Name: George
Date: March 28, 2003 at 19:52:06 Pacific
Subject: Beginners help Pascal
Reply: (edit)

Hi Goosh, Try this. George

Program Test;
uses crt;
var
name, sex, occupation:string;
age : integer;

procedure change_colors;
begin
textcolor (lightblue);
end; {change_colors}


procedure get_details;
{ask for personal details}
begin
gotoxy(10,3);
write ('What is your name? ');
readln (name);
Repeat
begin
gotoxy(10,4);
write ('What age are you? ');
readln (age);
if (age 0);
gotoxy(10,5);
write ('What sex are you? ');
readln (sex);
gotoxy(10,6);
write ('What is your occupation? ');
readln (occupation);
end; {get_details}

procedure print_details;
{prints out the details entered}
begin {1}
clrscr;
gotoxy (20,10); {add semicolon}
write ('Name: ');
gotoxy (20,11);
write ('Age: ');
gotoxy (20,12);
write ('Sex: ');
gotoxy (20,13);
write ('Occupation: ');
gotoxy (35,10); {add semicolon}
write (name);
gotoxy (35, 11);
write (age);
gotoxy (35,12);
write (sex);
gotoxy (35,13);
write (occupation);
end; {print_details}{1}

begin {main program}
clrscr;
repeat
get_details;
print_details;
until name='sammy';
end. {main program}


Report Offensive Follow Up For Removal

Response Number 13
Name: George
Date: March 28, 2003 at 19:55:09 Pacific
Subject: Beginners help Pascal
Reply: (edit)


Try this goosh. George

Program Test;
uses crt;
var
name, sex, occupation:string;
age : integer;

procedure change_colors;
begin
textcolor (lightblue);
end; {change_colors}


procedure get_details;
{ask for personal details}
begin
gotoxy(10,3);
write ('What is your name? ');
readln (name);
Repeat
begin
gotoxy(10,4);
write ('What age are you? ');
readln (age);
if (age 0);
gotoxy(10,5);
write ('What sex are you? ');
readln (sex);
gotoxy(10,6);
write ('What is your occupation? ');
readln (occupation);
end; {get_details}

procedure print_details;
{prints out the details entered}
begin {1}
clrscr;
gotoxy (20,10); {add semicolon}
write ('Name: ');
gotoxy (20,11);
write ('Age: ');
gotoxy (20,12);
write ('Sex: ');
gotoxy (20,13);
write ('Occupation: ');
gotoxy (35,10); {add semicolon}
write (name);
gotoxy (35, 11);
write (age);
gotoxy (35,12);
write (sex);
gotoxy (35,13);
write (occupation);
end; {print_details}{1}

begin {main program}
clrscr;
repeat
get_details;
print_details;
until name='sammy';
end. {main program}


Report Offensive Follow Up For Removal

Response Number 14
Name: geolew
Date: March 28, 2003 at 19:56:38 Pacific
Subject: Beginners help Pascal
Reply: (edit)

Program Test;
uses crt;
var
name, sex, occupation:string;
age : integer;

procedure change_colors;
begin
textcolor (lightblue);
end; {change_colors}


procedure get_details;
{ask for personal details}
begin
gotoxy(10,3);
write ('What is your name? ');
readln (name);
Repeat
begin
gotoxy(10,4);
write ('What age are you? ');
readln (age);
if (age 0);
gotoxy(10,5);
write ('What sex are you? ');
readln (sex);
gotoxy(10,6);
write ('What is your occupation? ');
readln (occupation);
end; {get_details}

procedure print_details;
{prints out the details entered}
begin {1}
clrscr;
gotoxy (20,10); {add semicolon}
write ('Name: ');
gotoxy (20,11);
write ('Age: ');
gotoxy (20,12);
write ('Sex: ');
gotoxy (20,13);
write ('Occupation: ');
gotoxy (35,10); {add semicolon}
write (name);
gotoxy (35, 11);
write (age);
gotoxy (35,12);
write (sex);
gotoxy (35,13);
write (occupation);
end; {print_details}{1}

begin {main program}
clrscr;
repeat
get_details;
print_details;
until name='sammy';
end. {main program}


Report Offensive Follow Up For Removal

Response Number 15
Name: elric
Date: March 29, 2003 at 04:02:29 Pacific
Subject: Beginners help Pascal
Reply: (edit)

G'day,

Yes, I suspected that you didn't mean that code.It is possibly that gtr than and lt problem that plagues this forum (sorry if that sounds harsh but there are people on here who are trying to grapple with code that is posted correctly..)
OK I'll have to have a think- still try out my idea and George's and geolew though- and get back to you.
Some comments on the code thus far:
1 there is a repeat loop lurking without and until on procedure get_details (again, probably not what the programmer intended)
2 You don't need the begin/end combination for a repeat until loop (it is already considered a compound statement) although I'm not sure whether the compiler will just ignore it (you'll see when you try to compile it).
Finally, I tinkered with your code and got this:

program details;

uses crt;

var
name, sex, occupation :string;
age :integer;
NewRec :char;
procedure change_colors;
begin
textcolor (lightblue);
end; {change_colors}

{ask for personal details}
procedure get_details;

begin
clrscr;
write ('What is your name? ');
readln (name);
write ('What age are you? ');
readln (age);
if age = 0 then begin
gotoxy (35,10);
write ('Grow up quick!');
end else begin
write ('What sex are you? ');
readln (sex);
write ('What is your occupation? ');
readln (occupation);
end;
end; {get_details}

procedure print_details;
{prints out the details entered}

begin
clrscr;
gotoxy (20,10);
write ('Name: ');
gotoxy (20,11);
write ('Age: ');
gotoxy (20,12);
write ('Sex: ');
gotoxy (20,13);
write ('Occupation: ');
gotoxy (35,10);
write (name);
gotoxy (35, 11);
write (age);
gotoxy (35,12);
write (sex);
gotoxy (35,13);
write (occupation);
end; {print_details}

begin {main program}

repeat
get_details;
print_details;
writeln;
gotoxy(35,14);
write('Do you wish to enter another ? Y/N');
readln(NewRec);
until (NewRec [] 'Y');

end. {main program}
I hope that formats OK. Just remember that ] means greater than and [ means less than.
I also put an option for new records in the main function so that you can keep running it and debug it a lot easier and also trap any errors.
regards,
Elric


Report Offensive Follow Up For Removal

Response Number 16
Name: goosh
Date: March 29, 2003 at 09:10:28 Pacific
Subject: Beginners help Pascal
Reply: (edit)

Hi guys me again. Once again I'd like to say thanks for helping me out! Ok I still have the same problem. I can't get it to repeat printing "Grow up quick!" on a different line. If I enter 0. The only bit of the code I'm having problems with is the procedure get_details. The print_details seems to be ok. Oh yeah thanks Elric for adding the choice to carry on. I hade to modify it a bit because when I put 'Y' it just went to the end no matter what I typed. It only seems to work with = 'n'. There must be a way to get it to repeat asking for age and outputing "grow up quick" on lines below the previous? I just can't seem to think of it. I though doing next := next + 1 and then putting that in the gotoxy (35,next) at "grow up quick" would print it on a line below each time but it's not doing it.
Thanks again for your help I hope you have time to get back to me.

This is the code with what you said in it Elric but I'm getting an error in statement after it. I don't know why.

P.S. I can see the symols on here is there a problem with other people seeing them???

uses crt;
var
age, next:integer;
name, sex, occupation:string;
NewRec:char;

procedure change_colors;
begin
textbackground (yellow);
textcolor (lightblue);
end; {change_colors}


procedure get_details;
{ask for personal details}
begin

write ('What is your name? ');
readln (name);
write ('What age are you? ');
readln (age);

if (age = 0) then
begin
gotoxy (35,10);
writeln ('Grow up quick!');
end
else
begin

write ('What sex are you? ');
readln (sex);
write ('What is your occupation? ');
readln (occupation);
end; {get_details}

procedure print_details;
{prints out the details entered}
begin
clrscr;
gotoxy (20,10);
write ('Name: ');
gotoxy (20,11);
write ('Age: ');
gotoxy (20,12);
write ('Sex: ');
gotoxy (20,13);
write ('Occupation: ');
gotoxy (35,10);
write (name);
gotoxy (35, 11);
write (age);
gotoxy (35,12);
write (sex);
gotoxy (35,13);
writeln (occupation);
end; {print_details}

begin {main program}
repeat
clrscr;
change_colors;
get_details;
print_details;
gotoxy (20,15);
write ('Do you wish to enter another? Y/N ');
readln (NewRec);
until (NewRec = 'n') or (NewRec = 'N');
clrscr;
gotoxy (35,10); write ('BYE!');
delay (1000);
end. {main program}


Report Offensive Follow Up For Removal

Response Number 17
Name: elric
Date: March 29, 2003 at 09:29:43 Pacific
Subject: Beginners help Pascal
Reply: (edit)

G'day,

Got a better idea of what you are trying to do now that I have had a think.
My code fails the less than or equal to zero test, so I have structured it a little diffent so that a value of less than or equal to zero can be input: the programme then sits there until a valid number is entered and then carries on as normal once this occurs.

Here is the code:
program details;

uses crt;

var
name, sex, occupation :string;
age :integer;
NewRec :char;
procedure change_colors;
begin
textcolor (lightblue);
end; {change_colors}

{ask for personal details}
procedure get_details;

begin
(*----------Clear the Screen---------*)
clrscr;

(*---------Enter Name----------------*)
write ('What is your name? ');
readln (name);

(*---------Enter Age-----------------*)
write ('What age are you? ');
readln (age);

(* -----Stay here until user enters a valid age*)
if age = 0 then begin
repeat
gotoxy(1,2);(* Take this out if you want *)
(* message to increment*)
write('Grow up quick! ]');
readln(age);
until age ] 0;
end;

(*--------Enter Sex----------------*)
write ('What sex are you? ');
readln (sex);

(*--------Enter Occupation---------*)
write ('What is your occupation? ');
readln (occupation);


end; {get_details}

procedure print_details;
{prints out the details entered}

begin
clrscr;
gotoxy (20,10);
write ('Name: ');
gotoxy (20,11);
write ('Age: ');
gotoxy (20,12);
write ('Sex: ');
gotoxy (20,13);
write ('Occupation: ');
gotoxy (35,10);
write (name);
gotoxy (35, 11);
write (age);
gotoxy (35,12);
write (sex);
gotoxy (35,13);
write (occupation);
end; {print_details}

begin {main program}

repeat
get_details;
print_details;
writeln;
gotoxy(20,15);
write('Do you wish to enter another ? (Y/N)-]');
readln(NewRec);
until (NewRec [] 'Y');

end. {main program}
If you want the message 'grow up quick' to scroll down the page just remove the gotoxy(1,2) line and it will.
You dont need an index because the repeat/write/readln/until acts as one. It is readln that puts it down one line at a time.
Note: On the greater than and less than problem, I have come to this solution.
I load it into wordpad before pasting and then do two search and replaces (gtr with ] and less.. with [).
So now, if you cut and paste this code into your local editor just do the reverse search and replace and it should run with no hitches.
Just let me know if you have any problems with any of it.
regards,
Elric


Report Offensive Follow Up For Removal

Response Number 18
Name: elric
Date: March 29, 2003 at 09:48:46 Pacific
Subject: Beginners help Pascal
Reply: (edit)

G'day,

Goosh, we must have posted at the same time. Try my latest effort in the last post and let us know how you get on.
I'm not sure what happened with the choice thing; it worked on my compiler.
It is meant to be
until NewRec is not equal to 'Y'
ie it will keep entering records until you enter something other than 'Y'.
Anyway, it doesn't matter as long as it makes sense to you.
I also didn't put the message at colmn 35. If you want it to do that I think all you would have to do is put a gotoxy (35,?) BEFORE the repeat loop starts, although I haven't tried it.
regards,
Elric


Report Offensive Follow Up For Removal

Response Number 19
Name: elric
Date: March 29, 2003 at 10:02:03 Pacific
Subject: Beginners help Pascal
Reply: (edit)

G'day,

Just one more thing. Are you saying that you can see the greater than and less than signs?
Because I can't. All of the posts here that use them end up being taken out of the message, so whole lines get lost.
Are you entering the text at a college console (maybe using UNIX ??)?
People here have said that they can't see them so they have to use [ and ](square brackets} instead.
My code will have these in because I can't read the them so you will have to convert them back.
regards,
Elric


Report Offensive Follow Up For Removal

Response Number 20
Name: goosh
Date: March 29, 2003 at 15:54:08 Pacific
Subject: Beginners help Pascal
Reply: (edit)

Hi there Elric this is becoming mad. I looked at your code and I seen what you did by putting 'if age [=0 then begin' and that solved a major problem. I took out the readln at the end because that was leaving a blank line before more input was entered. I then managed to get the next := 11 and next := next plus one to work by placing them apart in the loop. Then the program seemed to do exactly what I wanted by asking for age and if 0 or less than 0 was input it would say grow up quick then it would print what age are you on another line down and also when 0 or les than was inout again it would print grow up quick a line down and so on. So I was happy but alas not for long because when I pressed Y at the chioce of doing it again the question what age are you dissapears! I don't see how this is possible because that repeat loop in the main program starts the whole thing from fresh. What I noticed is when it repeats the program it is saving the age input from the first time. I don't see how this can be? Oh yeah and I must be blind because no I can't see the gtr or less than signs. i just must've got confused looking at Turbo and the screen here. Oh yeah I'm on turbo 7. I will paste the code and see what you think.

uses crt;
var
age, next:integer;
name, sex, occupation:string;
NewRec:char;

procedure change_colors;
begin
textbackground (yellow);
textcolor (lightblue);
end; {change_colors}


procedure get_details;
{ask for personal details}
begin

write ('What is your name? ');
readln (name);


if (age [= 0) then
begin
next := 10;
repeat
write ('What age are you? ');
readln (age);
gotoxy (35,next);
writeln ('Grow up quick!');

next := next + 1;
until age ] 0;
end;

write ('What sex are you? ');
readln (sex);
write ('What is your occupation? ');
readln (occupation);
end; {get_details}

procedure print_details;
{prints out the details entered}
begin
clrscr;
gotoxy (20,10);
write ('Name: ');
gotoxy (20,11);
write ('Age: ');
gotoxy (20,12);
write ('Sex: ');
gotoxy (20,13);
write ('Occupation: ');
gotoxy (35,10);
write (name);
gotoxy (35, 11);
write (age);
gotoxy (35,12);
write (sex);
gotoxy (35,13);
writeln (occupation);
end; {print_details}

begin {main program}
repeat
clrscr;
change_colors;
get_details;
print_details;
gotoxy (20,15);
write ('Do you wish to enter another? Y/N ');
readln (NewRec);
until (NewRec = 'n') or (NewRec = 'N');
clrscr;
gotoxy (35,10); write ('BYE!');
delay (1000);
end. {main program}


Report Offensive Follow Up For Removal

Response Number 21
Name: elric
Date: March 30, 2003 at 04:58:03 Pacific
Subject: Beginners help Pascal
Reply: (edit)

G'day,

I didn't realise that you wanted to repeat the "what age are you?" again each time.
That's no problem. I'm using turbo 7.0 too.
Unfortunately, I hate to keep on about this, since it is no fault of either of us, but your code is missing the code where the gtr and less than signs should be. I might be able to work out what you are tryng to do from what I can retrieve, but another idea is to repost with [ and ] in place or you can email it to me at prolls@emailbox.com.au and hope thta gets through in ok.
regards,
Elric


Report Offensive Follow Up For Removal

Response Number 22
Name: elric
Date: March 30, 2003 at 05:05:18 Pacific
Subject: Beginners help Pascal
Reply: (edit)

G'day,

Ignore that last bit about not seeing the [ and ]signs... It's been along day.
I'll have a look and get back to you.
regards,
Elric


Report Offensive Follow Up For Removal

Response Number 23
Name: elric
Date: March 30, 2003 at 09:35:55 Pacific
Subject: Beginners help Pascal
Reply: (edit)

G'day,

Well, I've had a lie down now and I feel much better. I had a quick look at your code and made these changes.
I didn't change this, but one word of warning: try to keep away from reserved words. Next is not one in Pascal but if you port this to say, VB6 or QBASIC errors will flag. Just a thought.
Now I hacked your routine a bit, but I think the best way to look at the code is this:
1 You are trying to trap an error condition
2 I have written it so that the error trap can be "plugged in"
3 ie the code works fine without it; say you didn't care about the age thing anymore then all you would have to do is rem out all code between the if then clause.
4 So the if statement will trap or determine the error condition.
5 The repeat and until loop holds the user there until he/ she enters a valid number.
6 The rest is purely cosmetic: I changed the error text to 'Please re-enter' but you can change that to whatever you like.
Similarly I put the gotoxy(35,next-1) to remove the previous 'grow up quick' text so that you just have the pair scrolling down the screen; if you remove it you will get multiple instances of that text line.
Some final comments; I'm not sure why you want to jump to the middle of the screen just for that error condition because you run straight off the end of the screen fairly quickly, but you could -now that you have the structure- play with those gotoxy statements so the the two line just keep overwriting themselves and therefore you stay on the screen.These are just cosmetic things of course- have a think about the structure of the programme first.
Finally, you'll have to rem back the colour statements- they were doing my eyes in..
regards,
Elric

(********** test version 4 ****************)

program goosh4;

uses crt;

var
age, next : integer;
name, sex, occupation: string;
NewRec : char;

(*procedure change_colors;
begin
textbackground (yellow);
textcolor (lightblue);
end; {change_colors}*)

procedure get_details;
{ask for personal details}
begin

write ('What is your name? ');
readln (name);

write ('What age are you? ');
readln (age);

(* This is the error trap routine ******************)
if (age [= 0) then begin
next := 10;
repeat
gotoxy (35,next);
writeln('Grow up quick! ');
gotoxy (35,next-1);
write (' ');
gotoxy (35,next+1);
write ('Please Re-enter ');
readln (age);
next := next +1;
until age ] 0;
end;
(***** Just cut this out and the prog will still work ***)

write ('What sex are you? ');
readln (sex);
write ('What is your occupation? ');
readln (occupation);
end; {get_details}

procedure print_details;
{prints out the details entered}
begin
clrscr;
gotoxy (20,10);
write ('Name: ');
gotoxy (20,11);
write ('Age: ');
gotoxy (20,12);
write ('Sex: ');
gotoxy (20,13);
write ('Occupation: ');
gotoxy (35,10);
write (name);
gotoxy (35, 11);
write (age);
gotoxy (35,12);
write (sex);
gotoxy (35,13);
writeln (occupation);
end; {print_details}


begin {main program}
repeat
clrscr;
(*change_colors;*)
get_details;
print_details;
gotoxy (20,15);
write ('Do you wish to enter another? Y/N ');
readln (NewRec);
until (NewRec = 'n') or (NewRec = 'N');

clrscr;
gotoxy (35,10); write ('BYE!');
delay (1000);
end. {main program}


Report Offensive Follow Up For Removal

Response Number 24
Name: elric
Date: March 30, 2003 at 09:48:29 Pacific
Subject: Beginners help Pascal
Reply: (edit)

G'day,

Another thing this editor seems to do: all the formating of those write and writeln statements has gone. They should be padded with spaces so that they are equal in length, sadly that doesn't seem to show here.
You will have to play with them.. I'll use an ***** here just in case they don't show up here either ( I think it's just pasted text that is affected) eg

writeln('Grow up quick*****')
write(*******************')
write('Please re-enter***')
You get the idea anyway.
regards,
Elric


Report Offensive Follow Up For Removal

Response Number 25
Name: goosh
Date: March 30, 2003 at 12:32:27 Pacific
Subject: Beginners help Pascal
Reply: (edit)

Hey Elric my saviour!!! Wow that works perfectly. I like the way you put next-1 because it does save the screen space. I would paste the code but basically I just copied what you did so no need. Hehe those colors were pretty dreadful eh. I changed them to textbackground red textcolor white. Just a thought though it's not neccessary because you've solved my main problem. When a value less then 0 is input more than twice then the 'What sex are you' question obviously goes back up the screen. is there a simple way to make it go to the next line no matter how many times less than 0 is input? not that i need it for such a simple program I was just wondering. Also I don't know how to change the screen color. I looked for it in help but I couldn't see it. Do you know?
Once again thanks very much for your help!!!!
One thing's for sure I think I've learned how to use the 'if' and 'repeat' statements a bit more now!
Lol you probably already guessed I'm going to post another problem already !EEK!


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Beginners help Pascal

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software