<mosaic.cnfolio.com>
PROGRAM
1. #include <stdio.h>
2. #include <time.h>
3. #include <string.h>
4. #include <stdlib.h>
5.
6. /*  START */
7.
8.
9. /* scan 4 pairs of inputs */
10.
11. /* print explanation for inputs */
12.
13. /* IF bet0 is less than 0, print error, return 0 */
14. /* IF bet1 is less than 0, print error, return 0 */
15. /* IF bet2 is less than 0, print error, return 0 */
16. /* IF bet3 is less than 0, print error, return 0 */
17.
18.
19. /* IF random number is 38 then represented number is 00 */
20. /*   IF first bet equals 00 then set win0 = TRUE */
21. /*       IF second bet equals 00 then set win1 = TRUE */
22. /*   IF third bet equals 00 then set win2 = TRUE */
23. /*      IF fourth bet equals 00 then set win3 = TRUE */
24.
25.
26. /* IF random number is anything but 38 */
27. /*       IF first bet equals the random number then set win0 = TRUE */
28. /*       IF second bet equals the random number then set win1 = TRUE */
29. /*       IF third bet equals the random number then set win2 = TRUE */
30. /*       IF fourth bet equals the random number then set win3 = TRUE */
31.
32.
33. /* IF win0 = true then print a win and how much has been won for bet0. */
34. /*   IF win0 = false then print a lose and ask user to try again. */
35. /* IF win1 = true then print a win and how much has been won for bet1 */
36. /*   IF win1 = false then print a lose and ask user to try again. */
37. /* IF win2 = true then print a win and how much has been won for bet2 */
38. /*   IF win2 = false then print a lose and ask user to try again. */
39. /* IF win3 = true then print a win and how much has been won for bet3 */
40. /* IF win3 = false then print a lose and ask user to try again. */
41.
42. /*   print total lost / gained including original bet. */
43.
44. /* END  */
45.
46.
47.
48.
49. int main(void)
50. {
51.
52. char  bet = { "\0" } ;
53. char  selection = { "\0" };
54. char  zerozero = {"00"} ;
55. char  randomnumber2  ;
56. int   randomnumber ;
57. int   win0 = 0 ;
58. int   win1 = 0 ;
59. int   win2 = 0 ;
60. int   win3 = 0 ;
61. int   total0 = 0 ;
62. int   total1 = 0 ;
63. int   total2 = 0 ;
64. int   total3 = 0 ;
65. int   winnings0, winnings1, winnings2, winnings3 ;
66. int   bet0a, bet1a, bet2a, bet3a ;
67.
68.
69.
70.
71. printf( "Please enter 4 bets with the number chosen and then money (GBP) with a space between." ) ;
72.
73. srand ( time( 0 ) ) ; /* Initialise the random number generation at current time. */
74. randomnumber = rand()%38 ; /* Rand maximum is 38 */
75.
76. scanf( "%s %s %s %s %s %s %s %s", selection, bet, selection, bet, selection, bet, selection, bet ) ; /* Scan as strings to seperate 00 and 0 inputs. Bets scanned as strings as multiple scans produced to many errors */
77.
78. bet0a = atoi(bet); /* atoi function converts bet0a from a string to a interger */
79. bet1a = atoi(bet);
80. bet2a = atoi(bet);
81. bet3a = atoi(bet);
82.
83. if( bet0a < 0 ) { /* if negatve, bet0 is entered, print an error, and terminate programme */
84. printf( "\nYour first bet is invalid" ) ;
85. return 0 ; }
86.
87. if( bet1a < 0 ) {
88. printf( "\nYour second bet is invalid" ) ;
89. return 0 ; }
90.
91. if( bet2a < 0 ) {
92. printf( "\nYour third bet is invalid" ) ;
93. return 0 ; }
94.
95. if( bet3a < 0 ) {
96. printf( "\nYour fourth bet is invalid" ) ;
97. return 0 ; }
98.
99. winnings0 = bet0a * 36 ; /* calculation of winnings for final printf. *36 as the winnings are *35 but the winner also recieves the original bet */
100. winnings1 = bet1a * 36 ;
101. winnings2 = bet2a * 36 ;
102. winnings3 = bet3a * 36 ;
103.
104. sprintf( randomnumber2 , "%d", randomnumber ) ; /* conversion of char to int for later comparison. comparing chars was producing to many errors*/
105.
106.    if (randomnumber==37) { /* if the random equals 37 then thats 00 */
107.
108.       printf( "\n\nThe winning number is 00! \n") ; /* user does not see that the actual random number is 37 */
109.
110.          if((strcmp(selection, zerozero) ==0)) { /* if selection0 = 00 then make win0 = 3 */
111.             win0 =3 ; }
112.          if((strcmp(selection, zerozero) ==0)) {
113.             win1 =3 ; }
114.          if((strcmp(selection, zerozero) ==0)) {
115.             win2 =3 ; }
116.          if((strcmp(selection, zerozero) ==0)) {
117.             win3 =3 ; }
118.
119.             }
120.
121.    if (randomnumber<37) { /* if winning number isnt 00 then use comparisons */
122.
123.       printf( "\n\nThe winning number is %d! \n", randomnumber ) ; /* print the winning random number */
124.
125.          if((strcmp(selection, randomnumber2) ==0)) { /* if compare selected number with random number then set win = 3 */
126.             win0 =3 ; }
127.          if((strcmp(selection, randomnumber2) ==0)) {
128.             win1 =3 ; }
129.          if((strcmp(selection, randomnumber2) ==0)) {
130.             win2 =3 ; }
131.          if((strcmp(selection, randomnumber2) ==0)) {
132.             win3 =3 ; }
133.
134.             }
135.
136.
137.
138.
139.       if (win0 == 3) { /* if win = 3 then first number wins and will print how much was won */
140.          printf("\nCongratulations! Your first bet has won £%d!", winnings0 ) ;
141.          total0 = winnings0 ; /* if this number is a win then total0 will become winnings0 */
142.          }
143.       if (win0 != 3) { /* if win does not get changed to 3 at any point then first bet has not won. */
144.          printf("\nUnlucky! Your first bet didnt win. Try again next time?") ;
145.          } /* if the first number is not a win then note that total0 remains 0 */
146.
147.
148.       if (win1 == 3) {
149.          printf("\nCongratulations! Your second bet has won £%d!", winnings1 ) ;
150.          total1 = winnings1 ;
151.          }
152.       if (win1 != 3) {
153.          printf("\nUnlucky! Your second bet didnt win. Try again next time?") ;
154.          }
155.
156.
157.       if (win2 == 3){
158.          printf("\nCongratulations! Your third bet has won £%d!", winnings2 ) ;
159.          total2 = winnings2 ;
160.          }
161.       if (win2 != 3){
162.          printf("\nUnlucky! Your third bet didnt win. Try again next time?") ;
163.          }
164.
165.
166.       if (win3 == 3){
167.          printf("\nCongratulations! Your fourth bet has won £%d!", winnings3 ) ;
168.          total3 = winnings3 ;
169.          }
170.       if (win3 != 3){
171.          printf("\nUnlucky! Your fourth bet didnt win. Try again next time?") ;
172.          }
173.
174.             printf ( "\n\nTotal amount won = £%d" , total1 + total2 + total3 + total0 - bet0a - bet1a - bet2a - bet3a ) ; /* total is winning amount bet * 36, - original bets */
175. }