943,906 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Jan 4th, 2009
0

Lottery Validation

Expand Post »
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)
  1. {Note - Validation should occur in the procedure 'get user numbers' . I have removed any attempts so its easier to see}
  2.  
  3. program test;
  4. uses
  5. EmptyPlaceHolderUnit;
  6.  
  7. type
  8. a = array[1..7] of integer; {Starts an array that will store the random numbers created by the lottery}
  9. b = array[1..7] of integer; {Starts an array that will store the users seven lottery numbers}
  10.  
  11. var
  12. i : integer;
  13. k, j, tmp: integer;
  14. numbers: a;
  15. user: b;
  16. counter: integer;
  17.  
  18. var
  19. subscript: integer;
  20. q: integer;
  21.  
  22. procedure initialise (var counter: integer);
  23. begin
  24. counter := 0; {Starts Counter}
  25. end;
  26.  
  27. procedure get_user_numbers (var user: b);
  28.  
  29. begin
  30. writeln ;
  31. writeln( ' You are now playing the Lottery Game ' );
  32. writeln ;
  33. write('Could You Please Select Your Seven Lottery Numbers Between the Range 0 - 49 ');
  34. writeln;
  35. for subscript := 1 to 7 do
  36. begin {Gets user to enter their selected lottery Numbers}
  37. readln(user[subscript]); {Stores User Numbers in the array 'user'}
  38. end;
  39. end;
  40.  
  41. procedure start_game (i:integer; var numbers: a; k ,j, tmp: integer);
  42.  
  43. var
  44. q: integer;
  45.  
  46. begin
  47. writeln;
  48. writeln (' ,---'); {Starting Game Graphic}
  49. writeln (' ( -_-('); {Due to limitations of the interface in Pascal}
  50. writeln (' ) .__/ )'); {I used ASCII art to create a graphic relevant to the game}
  51. writeln (' _/ _/_( / _.---._');
  52. writeln (' (__/ _ _) ,-._ / o \');
  53. writeln (' //)__(\/,-` |_| O o o O|');
  54. writeln ('_\\///==o=\'' | O o o O |');
  55. writeln (' `-''\ / \O o o /');
  56. writeln (' )___\ `''-.-\\');
  57. writeln (' / ,\ \ ____)_(____');
  58. writeln (' / / \ \ ''--..---,,-- ');
  59. writeln (' /() >() \\_//');
  60. writeln (' |\_\ |\_\ /,-.\');
  61.  
  62.  
  63. writeln ;
  64. writeln (' And tonights Lottery Balls are...');
  65. writeln ;
  66.  
  67.  
  68. begin
  69. randomize;
  70. for q := 1 to 7 do
  71. begin
  72. numbers[q] := (random(50)); {Selects and displays 7 random integers from the range 0 - 49}
  73. write(numbers[q],' ');
  74. end;
  75. end;
  76. end;
  77.  
  78. procedure sort_nums ( numbers: a; k, j, tmp: integer);
  79. var
  80. q: integer;
  81.  
  82. begin
  83.  
  84.  
  85.  
  86. for k := 1 to 6 do
  87. for j := k + 1 to 7 do
  88. if numbers[k] > numbers[j] then {Sorts numbers into ascending order}
  89. begin
  90. tmp := numbers[k];
  91. numbers[k] := numbers[j];
  92. numbers[j] := tmp;
  93. end;
  94.  
  95.  
  96. writeln;
  97. writeln;
  98. writeln('And tonights big money balls in ascending order are');
  99. writeln;
  100. writeln;
  101. writeln;
  102.  
  103.  
  104.  
  105. writeln(' *** ### ### ***');
  106. writeln(' *## ##*');
  107. writeln(' *## ##*');
  108. writeln(' *## ##*');
  109.  
  110. for k := 1 to 7 do
  111.  
  112. writeln(' *## ',k,': ',numbers[k],' ##*');
  113.  
  114.  
  115. writeln(' *## ##*'); {Displays the numbers in ascending order in a graphic}
  116. writeln(' *## ##*');
  117. writeln(' *## ##*');
  118. writeln(' *** ### ### *** ');
  119. writeln;
  120. writeln;
  121.  
  122.  
  123. end;
  124.  
  125. procedure search_for_numbers1 (numbers: a; user: b; var counter: integer);
  126. var
  127. q: integer;
  128. begin
  129. for q := 1 to 7 do {Seven seperate procedures that see if user numbers match lottery numbers}
  130. begin {If a match is found 1 is added to the counter}
  131. if user[1] = numbers[q] then
  132. counter := counter + 1;
  133. end;
  134. end;
  135.  
  136. procedure search_for_numbers2 (numbers: a; user: b; var counter: integer);
  137. var
  138. q: integer;
  139. begin
  140. for q := 1 to 7 do
  141. begin
  142. if user[2] = numbers[q] then
  143. counter := counter + 1;
  144. end;
  145. end;
  146.  
  147. procedure search_for_numbers3 (numbers: a; user: b; var counter: integer);
  148. var
  149. q: integer;
  150. begin
  151. for q := 1 to 7 do
  152. begin
  153. if user[3] = numbers[q] then
  154. counter := counter + 1;
  155. end;
  156. end;
  157.  
  158. procedure search_for_numbers4 (numbers: a; user: b; var counter: integer);
  159. var
  160. q: integer;
  161. begin
  162. for q := 1 to 7 do
  163. begin
  164. if user[4] = numbers[q] then
  165. counter := counter + 1;
  166. end;
  167. end;
  168.  
  169. procedure search_for_numbers5 (numbers: a; user: b; var counter: integer);
  170. var
  171. q: integer;
  172. begin
  173. for q := 1 to 7 do
  174. begin
  175. if user[5] = numbers[q] then
  176. counter := counter + 1;
  177. end;
  178. end;
  179.  
  180. procedure search_for_numbers6 (numbers: a; user: b; var counter: integer);
  181. var
  182. q: integer;
  183. begin
  184. for q := 1 to 7 do
  185. begin
  186. if user[6] = numbers[q] then
  187. counter := counter + 1;
  188. end;
  189. end;
  190.  
  191. procedure search_for_numbers7 (numbers: a; user: b; var counter: integer);
  192. var
  193. q: integer;
  194. begin
  195. for q := 1 to 7 do
  196. begin
  197. if user[7] = numbers[q] then
  198. counter := counter + 1;
  199. end;
  200. end;
  201.  
  202.  
  203.  
  204. procedure display_monies0 (counter: integer);
  205. begin
  206. if counter = 0 then
  207. writeln(' /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\');
  208. if counter = 0 then
  209. writeln(' / \');
  210. if counter = 0 then
  211. writeln('| You have Matched ',counter,' Balls and Have Won Nothing ! |');
  212. if counter = 0 then
  213. writeln(' \ /');
  214. if counter = 0 then
  215. writeln(' \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/');
  216. end;
  217.  
  218. { Seven seperate programs that assess value of counters and display prize money}
  219. {If counter = ?? then you have won ??}
  220. procedure display_monies1 (counter: integer);
  221. begin
  222.  
  223. if counter = 1 then
  224. writeln(' /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\');
  225. if counter = 1 then
  226. writeln(' / \');
  227. if counter = 1 then
  228. writeln('| You have Matched ',counter,' Ball and Have Won 1 Pound |');
  229. if counter = 1 then
  230. writeln(' \ /');
  231. if counter = 1 then
  232. writeln(' \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/');
  233. end;
  234.  
  235. procedure display_monies2 (counter: integer);
  236. begin
  237.  
  238. if counter = 2 then
  239. writeln(' /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\');
  240. if counter = 2 then
  241. writeln(' / \');
  242. if counter = 2 then
  243. writeln('| You have Matched ',counter,' Balls and Have Won 5 Pounds |');
  244. if counter = 2 then
  245. writeln(' \ /');
  246. if counter = 2 then
  247. writeln(' \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/');
  248. end;
  249.  
  250. procedure display_monies3 (counter: integer);
  251. begin
  252.  
  253. if counter = 3 then
  254. writeln(' /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\');
  255. if counter = 3 then
  256. writeln(' / \');
  257. if counter = 3 then
  258. writeln('| You have Matched ',counter,' Balls and Have Won 10 Pounds |');
  259. if counter = 3 then
  260. writeln(' \ /');
  261. if counter = 3 then
  262. writeln(' \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/');
  263. end;
  264.  
  265. procedure display_monies4 (counter: integer);
  266. begin
  267.  
  268. if counter = 4 then
  269. writeln(' /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\');
  270. if counter = 4 then
  271. writeln(' / \');
  272. if counter = 4 then
  273. writeln('| You have Matched ',counter,' Balls and Have Won 5000 Pounds |');
  274. if counter = 4 then
  275. writeln(' \ /');
  276. if counter = 4 then
  277. writeln(' \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/');
  278. end;
  279.  
  280. procedure display_monies5 (counter: integer);
  281. begin
  282.  
  283. if counter = 5 then
  284. writeln(' /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\');
  285. if counter = 5 then
  286. writeln(' / \');
  287. if counter = 5 then
  288. writeln('| You have Matched ',counter,' Balls and Have Won 25,000 Pounds |');
  289. if counter = 5 then
  290. writeln(' \ /');
  291. if counter = 5 then
  292. writeln(' \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/');
  293. end;
  294.  
  295.  
  296. procedure display_monies6 (counter: integer);
  297. begin
  298.  
  299. if counter = 6 then
  300. writeln(' /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\');
  301. if counter = 6 then
  302. writeln(' / \');
  303. if counter = 6 then
  304. writeln('| You have Matched ',counter,' Balls and Have Won 375,000 Pounds |');
  305. if counter = 6 then
  306. writeln(' \ /');
  307. if counter = 6 then
  308. writeln(' \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/');
  309. end;
  310.  
  311.  
  312. procedure display_monies7 (counter: integer);
  313. begin
  314.  
  315. if counter = 7 then
  316. writeln(' /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\');
  317. if counter = 7 then
  318. writeln(' / \');
  319. if counter = 7 then
  320. writeln('| You have Matched ',counter,' Balls and Have Won 4,300,000 Pounds |');
  321. if counter = 7 then
  322. writeln(' \ /');
  323. if counter = 7 then
  324. writeln(' \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/');
  325. end;
  326.  
  327.  
  328.  
  329. procedure show_prizes (i: integer);
  330.  
  331. begin
  332. writeln;
  333. writeln ('The prize monies for Tonights Game Is');
  334. writeln;
  335. writeln (' 0 Balls: 0 pounds');
  336. writeln (' 1 Ball: 1 pounds');
  337. writeln (' 2 Balls: 5 pounds');
  338. writeln (' 3 Balls: 10 pounds'); {A table of the prizes available}
  339. writeln (' 4 Balls: 5000 pounds');
  340. writeln (' 5 Balls: 25,000 pounds');
  341. writeln (' 6 Balls: 375,000 pounds');
  342. writeln (' 7 Balls: 4,300,000 pounds');
  343. end;
  344.  
  345.  
  346.  
  347. begin
  348. initialise(counter);
  349. get_user_numbers(user);
  350. start_game(i, numbers, k ,j, tmp);
  351. sort_nums(numbers, k, j, tmp);
  352. search_for_numbers1(numbers,user,counter);
  353. search_for_numbers2(numbers,user,counter);
  354. search_for_numbers3(numbers,user,counter);
  355. search_for_numbers4(numbers,user,counter);
  356. search_for_numbers5(numbers,user,counter);
  357. search_for_numbers6(numbers,user,counter);
  358. search_for_numbers7(numbers,user,counter);
  359. display_monies0(counter);
  360. display_monies1(counter);
  361. display_monies2(counter);
  362. display_monies3(counter);
  363. display_monies4(counter);
  364. display_monies5(counter);
  365. display_monies6(counter);
  366. display_monies7(counter);
  367. show_prizes(i);
  368. end.
Last edited by Narue; Jan 4th, 2009 at 9:15 am. Reason: added code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
smorton123 is offline Offline
8 posts
since Nov 2008
Jan 4th, 2009
0

Re: Lottery Validation

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)
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Jan 4th, 2009
0

Re: Lottery Validation

Correct but weird :
pascal Syntax (Toggle Plain Text)
  1. procedure display_monies0 (counter: integer);
  2. begin
  3. if counter = 0 then
  4. writeln(' /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\');
  5. if counter = 0 then
  6. writeln(' / \');
  7. if counter = 0 then
  8. writeln('| You have Matched ',counter,' Balls and Have Won Nothing ! |');
  9. if counter = 0 then
  10. writeln(' \ /');
  11. if counter = 0 then
  12. writeln(' \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/');
  13. end;
Why not
pascal Syntax (Toggle Plain Text)
  1. procedure display_monies0 (counter: integer);
  2. begin
  3. if counter = 0 then
  4. begin
  5. writeln(' /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\');
  6. writeln(' / \');
  7. writeln('| You have Matched ',counter,' Balls and Have Won Nothing ! |');
  8. writeln(' \ /');
  9. writeln(' \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/');
  10. end;
  11. end;
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Jan 4th, 2009
0

Re: Lottery Validation

The program first of all didnt recognise the valiation, then it did not work correctly with the array. Thats the part I am struggling with validation on an array. I can do validation on a variable but dont no where to start with an array ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
smorton123 is offline Offline
8 posts
since Nov 2008
Jan 4th, 2009
0

Re: Lottery Validation

Have you tried a for loop on the array to work through each item?
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Jan 4th, 2009
0

Re: Lottery Validation

Click to Expand / Collapse  Quote originally posted by LizR ...
Have you tried a for loop on the array to work through each item?
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 ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
smorton123 is offline Offline
8 posts
since Nov 2008
Jan 4th, 2009
0

Re: Lottery Validation

How to validate it? thats kinda something I want you to work out, so..

Stepping through an array..
Pascal and Delphi Syntax (Toggle Plain Text)
  1. for i:=1 to 7 do
  2. begin
  3. writeln(a[i]);
  4. 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
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Jan 4th, 2009
0

Re: Lottery Validation

I dont actually have Pascal on this machine but here's a rough idea

Pascal and Delphi Syntax (Toggle Plain Text)
  1. for i:=1 to 7 do
  2. writeln (‘Please enter 7 numbers’)
  3. begin
  4. readln(a[i]);
  5. if [a] (< 1) or > (>7) the
  6. writeln (‘Please re-enter the number between 1 and 7)
  7. until [a] (=> 1 and <=7)
  8. 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
smorton123 is offline Offline
8 posts
since Nov 2008
Jan 4th, 2009
0

Re: Lottery Validation

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
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Jan 4th, 2009
0

Re: Lottery Validation

Thanks for the help, some good stuff to learn. However could you please tell me what should go instead of the [a] as this is causing me the most trouble. Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
smorton123 is offline Offline
8 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Pascal and Delphi Forum Timeline: Disabling an Array of Controls in Delphi 7?
Next Thread in Pascal and Delphi Forum Timeline: Is point inside polygon ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC