| | |
Lottery Validation
Please support our Pascal and Delphi advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2008
Posts: 8
Reputation:
Solved Threads: 0
I'm almost finished my lottery program but having trouble with the validation. In the program the user enters their own numbers, these numbers are then stored in the array user. I want the program to validate these numbers to ensure each number is between 0 and 49. I have tried many methods bu none had the correct response. Any help would be much appreciated.
pascal Syntax (Toggle Plain Text)
{Note - Validation should occur in the procedure 'get user numbers' . I have removed any attempts so its easier to see} program test; uses EmptyPlaceHolderUnit; type a = array[1..7] of integer; {Starts an array that will store the random numbers created by the lottery} b = array[1..7] of integer; {Starts an array that will store the users seven lottery numbers} var i : integer; k, j, tmp: integer; numbers: a; user: b; counter: integer; var subscript: integer; q: integer; procedure initialise (var counter: integer); begin counter := 0; {Starts Counter} end; procedure get_user_numbers (var user: b); begin writeln ; writeln( ' You are now playing the Lottery Game ' ); writeln ; write('Could You Please Select Your Seven Lottery Numbers Between the Range 0 - 49 '); writeln; for subscript := 1 to 7 do begin {Gets user to enter their selected lottery Numbers} readln(user[subscript]); {Stores User Numbers in the array 'user'} end; end; procedure start_game (i:integer; var numbers: a; k ,j, tmp: integer); var q: integer; begin writeln; writeln (' ,---'); {Starting Game Graphic} writeln (' ( -_-('); {Due to limitations of the interface in Pascal} writeln (' ) .__/ )'); {I used ASCII art to create a graphic relevant to the game} writeln (' _/ _/_( / _.---._'); writeln (' (__/ _ _) ,-._ / o \'); writeln (' //)__(\/,-` |_| O o o O|'); writeln ('_\\///==o=\'' | O o o O |'); writeln (' `-''\ / \O o o /'); writeln (' )___\ `''-.-\\'); writeln (' / ,\ \ ____)_(____'); writeln (' / / \ \ ''--..---,,-- '); writeln (' /() >() \\_//'); writeln (' |\_\ |\_\ /,-.\'); writeln ; writeln (' And tonights Lottery Balls are...'); writeln ; begin randomize; for q := 1 to 7 do begin numbers[q] := (random(50)); {Selects and displays 7 random integers from the range 0 - 49} write(numbers[q],' '); end; end; end; procedure sort_nums ( numbers: a; k, j, tmp: integer); var q: integer; begin for k := 1 to 6 do for j := k + 1 to 7 do if numbers[k] > numbers[j] then {Sorts numbers into ascending order} begin tmp := numbers[k]; numbers[k] := numbers[j]; numbers[j] := tmp; end; writeln; writeln; writeln('And tonights big money balls in ascending order are'); writeln; writeln; writeln; writeln(' *** ### ### ***'); writeln(' *## ##*'); writeln(' *## ##*'); writeln(' *## ##*'); for k := 1 to 7 do writeln(' *## ',k,': ',numbers[k],' ##*'); writeln(' *## ##*'); {Displays the numbers in ascending order in a graphic} writeln(' *## ##*'); writeln(' *## ##*'); writeln(' *** ### ### *** '); writeln; writeln; end; procedure search_for_numbers1 (numbers: a; user: b; var counter: integer); var q: integer; begin for q := 1 to 7 do {Seven seperate procedures that see if user numbers match lottery numbers} begin {If a match is found 1 is added to the counter} if user[1] = numbers[q] then counter := counter + 1; end; end; procedure search_for_numbers2 (numbers: a; user: b; var counter: integer); var q: integer; begin for q := 1 to 7 do begin if user[2] = numbers[q] then counter := counter + 1; end; end; procedure search_for_numbers3 (numbers: a; user: b; var counter: integer); var q: integer; begin for q := 1 to 7 do begin if user[3] = numbers[q] then counter := counter + 1; end; end; procedure search_for_numbers4 (numbers: a; user: b; var counter: integer); var q: integer; begin for q := 1 to 7 do begin if user[4] = numbers[q] then counter := counter + 1; end; end; procedure search_for_numbers5 (numbers: a; user: b; var counter: integer); var q: integer; begin for q := 1 to 7 do begin if user[5] = numbers[q] then counter := counter + 1; end; end; procedure search_for_numbers6 (numbers: a; user: b; var counter: integer); var q: integer; begin for q := 1 to 7 do begin if user[6] = numbers[q] then counter := counter + 1; end; end; procedure search_for_numbers7 (numbers: a; user: b; var counter: integer); var q: integer; begin for q := 1 to 7 do begin if user[7] = numbers[q] then counter := counter + 1; end; end; procedure display_monies0 (counter: integer); begin if counter = 0 then writeln(' /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\'); if counter = 0 then writeln(' / \'); if counter = 0 then writeln('| You have Matched ',counter,' Balls and Have Won Nothing ! |'); if counter = 0 then writeln(' \ /'); if counter = 0 then writeln(' \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/'); end; { Seven seperate programs that assess value of counters and display prize money} {If counter = ?? then you have won ??} procedure display_monies1 (counter: integer); begin if counter = 1 then writeln(' /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\'); if counter = 1 then writeln(' / \'); if counter = 1 then writeln('| You have Matched ',counter,' Ball and Have Won 1 Pound |'); if counter = 1 then writeln(' \ /'); if counter = 1 then writeln(' \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/'); end; procedure display_monies2 (counter: integer); begin if counter = 2 then writeln(' /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\'); if counter = 2 then writeln(' / \'); if counter = 2 then writeln('| You have Matched ',counter,' Balls and Have Won 5 Pounds |'); if counter = 2 then writeln(' \ /'); if counter = 2 then writeln(' \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/'); end; procedure display_monies3 (counter: integer); begin if counter = 3 then writeln(' /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\'); if counter = 3 then writeln(' / \'); if counter = 3 then writeln('| You have Matched ',counter,' Balls and Have Won 10 Pounds |'); if counter = 3 then writeln(' \ /'); if counter = 3 then writeln(' \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/'); end; procedure display_monies4 (counter: integer); begin if counter = 4 then writeln(' /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\'); if counter = 4 then writeln(' / \'); if counter = 4 then writeln('| You have Matched ',counter,' Balls and Have Won 5000 Pounds |'); if counter = 4 then writeln(' \ /'); if counter = 4 then writeln(' \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/'); end; procedure display_monies5 (counter: integer); begin if counter = 5 then writeln(' /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\'); if counter = 5 then writeln(' / \'); if counter = 5 then writeln('| You have Matched ',counter,' Balls and Have Won 25,000 Pounds |'); if counter = 5 then writeln(' \ /'); if counter = 5 then writeln(' \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/'); end; procedure display_monies6 (counter: integer); begin if counter = 6 then writeln(' /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\'); if counter = 6 then writeln(' / \'); if counter = 6 then writeln('| You have Matched ',counter,' Balls and Have Won 375,000 Pounds |'); if counter = 6 then writeln(' \ /'); if counter = 6 then writeln(' \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/'); end; procedure display_monies7 (counter: integer); begin if counter = 7 then writeln(' /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\'); if counter = 7 then writeln(' / \'); if counter = 7 then writeln('| You have Matched ',counter,' Balls and Have Won 4,300,000 Pounds |'); if counter = 7 then writeln(' \ /'); if counter = 7 then writeln(' \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/'); end; procedure show_prizes (i: integer); begin writeln; writeln ('The prize monies for Tonights Game Is'); writeln; writeln (' 0 Balls: 0 pounds'); writeln (' 1 Ball: 1 pounds'); writeln (' 2 Balls: 5 pounds'); writeln (' 3 Balls: 10 pounds'); {A table of the prizes available} writeln (' 4 Balls: 5000 pounds'); writeln (' 5 Balls: 25,000 pounds'); writeln (' 6 Balls: 375,000 pounds'); writeln (' 7 Balls: 4,300,000 pounds'); end; begin initialise(counter); get_user_numbers(user); start_game(i, numbers, k ,j, tmp); sort_nums(numbers, k, j, tmp); search_for_numbers1(numbers,user,counter); search_for_numbers2(numbers,user,counter); search_for_numbers3(numbers,user,counter); search_for_numbers4(numbers,user,counter); search_for_numbers5(numbers,user,counter); search_for_numbers6(numbers,user,counter); search_for_numbers7(numbers,user,counter); display_monies0(counter); display_monies1(counter); display_monies2(counter); display_monies3(counter); display_monies4(counter); display_monies5(counter); display_monies6(counter); display_monies7(counter); show_prizes(i); end.
Last edited by Narue; Jan 4th, 2009 at 9:15 am. Reason: added code tags
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
Well, you seem to have grasped the if statement, what issues did you have when deciding wether all the numbers entered were between 1 and 49 (and I guess not used twice)
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Correct but weird :
Why not
pascal Syntax (Toggle Plain Text)
procedure display_monies0 (counter: integer); begin if counter = 0 then writeln(' /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\'); if counter = 0 then writeln(' / \'); if counter = 0 then writeln('| You have Matched ',counter,' Balls and Have Won Nothing ! |'); if counter = 0 then writeln(' \ /'); if counter = 0 then writeln(' \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/'); end;
pascal Syntax (Toggle Plain Text)
procedure display_monies0 (counter: integer); begin if counter = 0 then begin writeln(' /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\'); writeln(' / \'); writeln('| You have Matched ',counter,' Balls and Have Won Nothing ! |'); writeln(' \ /'); writeln(' \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/'); end; end;
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Nov 2008
Posts: 8
Reputation:
Solved Threads: 0
I have tried that but the program ran with no effect. I couldnt seem to get the loop to run with the array. Would you be able to show an example of how to validate/ loop with an array no matter how basic ?
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
How to validate it? thats kinda something I want you to work out, so..
Stepping through an array..
So, that will write out your elements..
Now, see if you can add enough code to test if its valid or not.
You'll probably want 1 more variable, and you'll need your if statements..
See if you can tell me what you think it should be
Stepping through an array..
Pascal and Delphi Syntax (Toggle Plain Text)
for i:=1 to 7 do begin writeln(a[i]); end;
So, that will write out your elements..
Now, see if you can add enough code to test if its valid or not.
You'll probably want 1 more variable, and you'll need your if statements..
See if you can tell me what you think it should be
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
•
•
Join Date: Nov 2008
Posts: 8
Reputation:
Solved Threads: 0
I dont actually have Pascal on this machine but here's a rough idea
This is effectively what I want my program to do where it takes in seven numbers between X and Y. If any number exceeds this range then it displays an error message and will not accept that number until it fits into the specified range. But I know the validation is not correct.
Pascal and Delphi Syntax (Toggle Plain Text)
for i:=1 to 7 do writeln (‘Please enter 7 numbers’) begin readln(a[i]); if [a] (< 1) or > (>7) the writeln (‘Please re-enter the number between 1 and 7’) until [a] (=> 1 and <=7) end;
This is effectively what I want my program to do where it takes in seven numbers between X and Y. If any number exceeds this range then it displays an error message and will not accept that number until it fits into the specified range. But I know the validation is not correct.
Last edited by smorton123; Jan 4th, 2009 at 2:24 pm.
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
Well you're right in that that wouldnt work but you arent as far off as you think.
You ask for 7 numbers, thats fine.
the only part is wrong really is the if and until.. (as you're arent actually asking for a number you're just writing out please reenter the number.. but also an if and until dont go together.
You might want to read up on how the "until" function works, such as at http://delphibasics.co.uk/RTL.asp?Name=Until
But the general premise is OK, although [a] would fail, as would the (->1 and <=7) and theres no repeat with your until
But the logic you have isnt that far wrong.
Read up on while loops too, theres a huge difference between them, but you may what whiles as well later
http://delphibasics.co.uk/RTL.asp?Name=While
See if you can work out what to do next
You ask for 7 numbers, thats fine.
the only part is wrong really is the if and until.. (as you're arent actually asking for a number you're just writing out please reenter the number.. but also an if and until dont go together.
You might want to read up on how the "until" function works, such as at http://delphibasics.co.uk/RTL.asp?Name=Until
But the general premise is OK, although [a] would fail, as would the (->1 and <=7) and theres no repeat with your until
But the logic you have isnt that far wrong.Read up on while loops too, theres a huge difference between them, but you may what whiles as well later

http://delphibasics.co.uk/RTL.asp?Name=While
See if you can work out what to do next
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
![]() |
Other Threads in the Pascal and Delphi Forum
- Previous Thread: Disabling an Array of Controls in Delphi 7?
- Next Thread: Is point inside polygon ?
| Thread Tools | Search this Thread |






