944,167 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1559
  • C RSS
Aug 12th, 2005
0

how to eliminate all keyboard inputs accept a certain range

Expand Post »
hi guys and gals, just a young padawan learner trying to write this poker program from scratch. i know their are programs out their but nothing like the one you write your self. Im trying to have my program read in the number of players from the keyboard input. I have eliminated all the numbers i dont want. but what if someone enters in letters or special characters how do i eliminate all the keyboard functions except my small range of numbers. My first guess was to have the 1-20 in ascii and all other ascii codes would be a bad input didnt know how to write it. this is probaly elimentary but im stomped heres the little code i have wrote. numofplayers is a int i made it a char but that didnt work. also i hear goto statements are bad how could i avoid the first two goto statements in this elimentary program. thanks i appreciate any help.

  1. #include <stdio.h>
  2. #include <iostream.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <conio.h>
  7.  
  8.  
  9.  
  10.  
  11. //================================================== main
  12. int main() {
  13. int numofplayers=0;
  14. char random_play_flag;
  15. char play_again;
  16.  
  17.  
  18.  
  19.  
  20. //char CARD_FACES[13] = {'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'};
  21. //char CARD_SUITS[4] = {'S', 'H', 'C', 'D'};
  22.  
  23. char cards[52][25] = { "SA", "S2", "S3", "S4", "S5", "S6", "S7",
  24. "S8", "S9","ST", "SJ", "SQ","SK",
  25. "HA", "H2", "H3", "H4", "H5", "H6", "H7"
  26. , "H8", "H9", "HT", "HJ", "HQ","HK",
  27. "CA", "C2", "C3", "C4", "C5", "C6", "C7"
  28. , "C8", "C9", "CT", "CJ", "CQ","CK",
  29. "DA", "D2", "D3", "D4", "D5", "D6", "D7"
  30. , "D8", "D9", "DT", "DJ", "DQ","DK" };
  31.  
  32. char players[52][25];
  33. char deck[52][25];
  34. char dealer[52][25];
  35. char complete_hand[52][25];
  36.  
  37.  
  38. play_again_label:
  39. cout << endl;
  40. cout << "Enter Number of Players in Game: ";
  41. cin >>numofplayers;
  42.  
  43. if (numofplayers < 1 || numofplayers > 20)
  44. {
  45. cout << "Please try a number less than 20";
  46. goto play_again_label;
  47. }
  48.  
  49.  
  50.  
  51. start_again:
  52. cout << "Do you want Random Play (Y/N)";
  53. cin >> random_play_flag ;
  54.  
  55. if ( random_play_flag != 'y' && random_play_flag != 'Y' && random_play_flag != 'n' && random_play_flag != 'N')
  56. {
  57. cout << "Please type y or n";
  58. cout << endl;
  59. goto start_again;
  60. }
  61.  
  62. //shuffle deck
  63. srand((unsigned)time(0));
  64. int gen_random=0;
  65. int random_integer;
  66. int lowest=0, highest=51;
  67. int range=(highest-lowest)+1;
  68. for (int d=0;d<52;d++){
  69. pick_num:
  70. random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
  71. // printf("rand num=%d\n", random_integer);
  72. strcpy(deck[d],cards[random_integer]);
  73. int x=0;
  74. while( x < d){
  75. if (!strcmp(deck[x], cards[random_integer])){
  76. // printf("DUPS deck=%s random card=%s\n", deck[x], cards[random_integer]);
  77. goto pick_num;
  78. }
  79. x++;
  80. }
  81. }
  82. // for (int dd=0;dd<52;dd++){
  83. // printf("----->Random card =%s\n",deck[dd]);
  84. // }
  85.  
  86. if (random_play_flag == 'Y' || random_play_flag =='y')
  87. {
  88.  
  89.  
  90.  
  91. for (int numcards=0;numcards<2;numcards++){ //2 sets of cards
  92. for(int index=0; index < numofplayers; index++){
  93. if (numcards == 0){
  94. strcpy(players[index],deck[index]);
  95. }
  96. else{
  97.  
  98. strcat(players[index]," ");
  99. strcat(players[index],deck[index+numofplayers]);
  100. // printf("IN card 2=%d %d %s\n",numcards,index,deck[index+numofplayers]);
  101. }
  102. }
  103. }
  104. }
  105. else if (random_play_flag == 'N' || random_play_flag =='n'){
  106. // cout << "No random play" ;
  107. for (int l=0; l<numofplayers;l++){
  108. printf("\n\nEnter Hand for Player %d = \n\n",l+1);
  109. cout << "Enter Card 1:\n";
  110. cin >> players[l];
  111. cout << "Enter Card 2:\n";
  112. strcat(players[l]," ");
  113. cin >> deck[0];
  114. strcat(players[l],deck[0]);
  115. }
  116. }
  117.  
  118. //Dealer
  119. //Burn a card - start with (numplayers*2)+1
  120. //Deal 3 cards
  121. strcpy(dealer[0],deck[(numofplayers*2)+1]);
  122. strcat(dealer[0]," ");
  123. strcat(dealer[0],deck[(numofplayers*2)+2]);
  124. strcat(dealer[0]," ");
  125. strcat(dealer[0],deck[(numofplayers*2)+3]);
  126. //1 dead card
  127. //deal card
  128. strcat(dealer[0]," ");
  129. strcat(dealer[0],deck[(numofplayers*2)+5]);
  130. //1 dead card
  131. //deal card
  132. strcat(dealer[0]," ");
  133. strcat(dealer[0],deck[(numofplayers*2)+7]);
  134.  
  135. for (int z=0; z<numofplayers;z++){
  136. printf("\n\nPlayer %d Hand\n", z+1);
  137. printf("%s\n\n",players[z]);
  138.  
  139. }
  140.  
  141. printf("\n\nDEALERS HAND=%s\n\n",dealer[0]);
  142.  
  143. //Evaluate Hand for a flush
  144.  
  145.  
  146.  
  147. for (int tt=0;tt<numofplayers;tt++){
  148. int diamondflush=0;
  149. int spadesflush=0;
  150. int clubflush=0;
  151. int heartflush=0;
  152.  
  153.  
  154.  
  155. strcpy(complete_hand[tt],dealer[0]);
  156. strcat(complete_hand[tt]," ");
  157. strcat(complete_hand[tt],players[tt]);
  158. printf("COMPLETE HAND FOR PLAYER %d = ",tt+1);
  159. for (int u=0;u<21;u++){
  160. if (complete_hand[tt][u] == 'D')
  161. printf("%c",char(5));
  162. else if (complete_hand[tt][u] == 'S')
  163. printf("%c",char(4));
  164. else if (complete_hand[tt][u] == 'C')
  165. printf("%c",char(3));
  166. else if (complete_hand[tt][u] == 'H')
  167. printf("%c",char(6));
  168. else if (complete_hand[tt][u] == 'T')
  169. printf("%s","10");
  170. else
  171. printf("%c",complete_hand[tt][u]);
  172. }
  173. printf("\n");
  174. //printf("COMPLETE HAND FOR PLAYER %d = %s\n",tt+1, complete_hand[tt]);
  175. //printf("%c\n",complete_hand[0][1]);
  176. for (int r=0; r<21;r=r+3)
  177. {
  178. if (complete_hand[tt][r] == 'D')
  179. diamondflush++;
  180. if (complete_hand[tt][r] == 'S')
  181. spadesflush++;
  182. if (complete_hand[tt][r] == 'C')
  183. clubflush++;
  184. if (complete_hand[tt][r] == 'H')
  185. heartflush++;
  186. //printf("%c\n",char(5));
  187. }
  188.  
  189. if (diamondflush >=5)
  190. printf("You got a Flush\n");
  191. if (spadesflush >=5)
  192. printf("You got a Flush\n");
  193. if (clubflush >=5)
  194. printf("You got a Flush\n");
  195. if (heartflush >=5)
  196. printf("You got a Flush\n");
  197.  
  198. int twos=0;
  199. int threes=0;
  200. int fours=0;
  201. int fives=0;
  202. int six=0;
  203. int seven=0;
  204. int eight=0;
  205. int nine=0;
  206. int ten=0;
  207. int ace=0;
  208. int king=0;
  209. int queen=0;
  210. int jack=0;
  211. char three_cards='N';
  212. char two_cards='N';
  213.  
  214.  
  215. for (int rr=1; rr<21;rr=rr+3)
  216. {
  217. if (complete_hand[tt][rr] == '2')
  218. twos++;
  219. if (complete_hand[tt][rr] == '3')
  220. threes++;
  221. if (complete_hand[tt][rr] == '4')
  222. fours++;
  223. if (complete_hand[tt][rr] == '5')
  224. fives++;
  225. if (complete_hand[tt][rr] == '6')
  226. six++;
  227. if (complete_hand[tt][rr] == '7')
  228. seven++;
  229. if (complete_hand[tt][rr] == '8')
  230. eight++;
  231. if (complete_hand[tt][rr] == '9')
  232. nine++;
  233. if (complete_hand[tt][rr] == 'T')
  234. ten++;
  235. if (complete_hand[tt][rr] == 'A')
  236. ace++;
  237. if (complete_hand[tt][rr] == 'K')
  238. king++;
  239. if (complete_hand[tt][rr] == 'Q')
  240. queen++;
  241. if (complete_hand[tt][rr] == 'J')
  242. jack++;
  243. //printf("number=%c\n",complete_hand[tt][rr]);
  244. }
  245.  
  246.  
  247.  
  248. if (twos==4)
  249. printf("Four of a Kind 2\n");
  250. else if (twos==3){
  251. printf("Three of a Kind 2\n");
  252. three_cards='Y';
  253. }
  254. else if (twos==2){
  255. printf("Pairs 2\n");
  256. two_cards='Y';
  257. }
  258.  
  259. if (threes==4)
  260. printf("Four of a Kind 3\n");
  261. else if (threes==3){
  262. printf("Three of a Kind 3\n");
  263. three_cards='Y';
  264. }
  265. else if (threes==2){
  266. printf("Pairs 3\n");
  267. two_cards='Y';
  268. }
  269.  
  270. if (fours==4)
  271. printf("Four of a Kind 4\n");
  272. else if (fours==3){
  273. printf("Three of a Kind 4\n");
  274. three_cards='Y';
  275. }
  276. else if(fours==2){
  277. printf("Pairs 4\n");
  278. two_cards='Y';
  279. }
  280.  
  281. if (fives==4)
  282. printf("Four of a Kind 5\n");
  283. else if(fives==3){
  284. printf("Three of a Kind 5\n");
  285. three_cards='Y';
  286. }
  287. else if (fives==2){
  288. printf("Pairs 5\n");
  289. two_cards='Y';
  290. }
  291. if (six==4)
  292. printf("Four of a Kind 6\n");
  293. else if (six==3){
  294. printf("Three of a Kind 6\n");
  295. three_cards='Y';
  296. }
  297. else if (six==2){
  298. printf("Pairs 6\n");
  299. two_cards='Y';
  300. }
  301. if (seven==4)
  302. printf("Four of a Kind 7\n");
  303. else if(seven==3){
  304. printf("Three of a Kind 7\n");
  305. three_cards='Y';
  306. }
  307. else if(seven==2){
  308. printf("Pairs 7\n");
  309. two_cards='Y';
  310. }
  311. if (eight==4)
  312. printf("Four of a Kind 8\n");
  313. else if (eight==3){
  314. printf("Three of a Kind 8\n");
  315. three_cards='Y';
  316. }
  317. else if (eight==2){
  318. printf("Pairs 8\n");
  319. two_cards='Y';
  320. }
  321. if (nine==4)
  322. printf("Four of a Kind 9\n");
  323. else if (nine==3){
  324. printf("Three of a Kind 9\n");
  325. three_cards='Y';
  326. }
  327. else if (nine==2){
  328. printf("Pairs 9\n");
  329. two_cards='Y';
  330. }
  331. if (ten==4)
  332. printf("Four of a Kind 10\n");
  333. else if (ten==3){
  334. printf("Three of a Kind 10\n");
  335. three_cards='Y';
  336. }
  337. else if (ten==2){
  338. printf("Pairs 10\n");
  339. two_cards='Y';
  340. }
  341. if (ace==4)
  342. printf("Four of a Kind A\n");
  343. else if (ace==3){
  344. printf("Three of a Kind A\n");
  345. three_cards='Y';
  346. }
  347. else if (ace==2){
  348. printf("Pairs A\n");
  349. two_cards='Y';
  350. }
  351. if (king==4)
  352. printf("Four of a Kind K\n");
  353. else if (king==3){
  354. printf("Three of a Kind K\n");
  355. three_cards='Y';
  356. }
  357. else if (king==2){
  358. printf("Pairs Y\n");
  359. two_cards='Y';
  360. }
  361. if (queen==4)
  362. printf("Four of a Kind Q\n");
  363. else if (queen==3){
  364. printf("Three of a Kind Q\n");
  365. three_cards='Y';
  366. }
  367. else if (queen==2){
  368. printf("Pairs Q\n");
  369. two_cards='Y';
  370. }
  371. if (jack==4)
  372. printf("Four of a Kind J\n");
  373. else if (jack==3){
  374. printf("Three of a Kind J\n");
  375. three_cards='Y';
  376. }
  377. else if(jack==2){
  378. printf("Pairs J\n");
  379. two_cards='Y';
  380. }
  381.  
  382.  
  383. if (three_cards =='Y' && two_cards == 'Y')
  384. printf("Full House\n");
  385.  
  386. }
  387.  
  388. cout << "Play again? ";
  389. cin >>play_again;
  390. if (play_again == 'Y' || play_again =='y') {
  391. system("CLS");
  392. goto play_again_label;
  393. }
  394. return 0;
  395. }//end main
<< moderator edit: added [code][/code] tags >>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bruce3210 is offline Offline
3 posts
since Aug 2005
Aug 12th, 2005
0

Re: how to eliminate all keyboard inputs accept a certain range

In general you can do something like this although you should drop the deprecated headers for the std headers.
  1. int input;
  2. while(!(cin>>input))
  3. {
  4. cerr<<endl<<"enter an int wally!!!"<<endl;
  5. cin.clear();
  6. cin.ignore(numeric_limits<streamsize>::max(),'\n');
  7. }
  8. // if int entered, we get to here
This will grab input from the keyboard and junk a non-integer input and wait for more input. If an int is entered then code proceeds past the comment.
It is also possible to validate input like this to a range of ints something like this (from a previous thread I participated in).....
  1. while (!(cin>>row>>seatnum) || row<1 || row>12 || seatnum<1 || seatnum>4)
  2. {
  3. cerr<<endl<<"error! re-enter"<<endl;
  4. cin.clear();
  5. cin.ignore(numeric_limits<streamsize>::max(),'\n');
  6. }
  7. AllocSeat(row,seatnum);
Although often I will make a validate function ifthe expression gets complicated and just call that function there instead.
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Aug 12th, 2005
0

Re: how to eliminate all keyboard inputs accept a certain range

mmhm, there are a few ways of doing this. stoned_coder listed one way. I am in a hurry and have to go... this is what I did. I suggested using atoi... converting a string to an integer and then using your if statment... there is also a function called kbhit... but I dont have time to go into details.

as far as removing goto's you should used structured programming. reading up on how to do function calls would be a good start. I removed the first call to goto_xxx by changing the goto to a loop (do until loop)... you should also read up on the diffrent type of loops..
for loop/ do loop / while loop...

best of luck... gota run!

  1. #include <stdio.h>
  2. #include <iostream.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <conio.h>
  7.  
  8.  
  9.  
  10.  
  11. //================================================== main
  12. int main()
  13. {
  14. int numofplayers=0;
  15. char strnumofplayers[3];
  16. char random_play_flag;
  17. char play_again;
  18.  
  19.  
  20.  
  21.  
  22. //char CARD_FACES[13] = {'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'};
  23. //char CARD_SUITS[4] = {'S', 'H', 'C', 'D'};
  24.  
  25. char cards[52][25] = { "SA", "S2", "S3", "S4", "S5", "S6", "S7",
  26. "S8", "S9","ST", "SJ", "SQ","SK",
  27. "HA", "H2", "H3", "H4", "H5", "H6", "H7"
  28. , "H8", "H9", "HT", "HJ", "HQ","HK",
  29. "CA", "C2", "C3", "C4", "C5", "C6", "C7"
  30. , "C8", "C9", "CT", "CJ", "CQ","CK",
  31. "DA", "D2", "D3", "D4", "D5", "D6", "D7"
  32. , "D8", "D9", "DT", "DJ", "DQ","DK"};
  33.  
  34. char players[52][25];
  35. char deck[52][25];
  36. char dealer[52][25];
  37. char complete_hand[52][25];
  38.  
  39. play_again_label:
  40.  
  41. do
  42. {
  43.  
  44. cout << endl;
  45. cout << "Enter Number of Players in Game: ";
  46. cin >> strnumofplayers;
  47. numofplayers = atoi(strnumofplayers);
  48. if ( numofplayers == 0 ) return 0; // quit the game!
  49. if ( numofplayers < 1 || numofplayers > 20 )
  50. {
  51. cout << "Please try a number less than 20";
  52. }
  53.  
  54. } while ( numofplayers < 1 || numofplayers > 20 );
  55.  
  56.  
  57. start_again:
  58. cout << "Do you want Random Play (Y/N)";
  59. cin >> random_play_flag ;
  60.  
  61. if ( random_play_flag != 'y' && random_play_flag != 'Y' && random_play_flag != 'n' && random_play_flag != 'N' )
  62. {
  63. cout << "Please type y or n";
  64. cout << endl;
  65. goto start_again;
  66. }
  67.  
  68. //shuffle deck
  69. srand((unsigned)time(0));
  70. int gen_random=0;
  71. int random_integer;
  72. int lowest=0, highest=51;
  73. int range=(highest-lowest)+1;
  74. for ( int d=0;d<52;d++ )
  75. {
  76. pick_num:
  77. random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
  78. // printf("rand num=%d\n", random_integer);
  79. strcpy(deck[d],cards[random_integer]);
  80. int x=0;
  81. while ( x < d )
  82. {
  83. if ( !strcmp(deck[x], cards[random_integer]) )
  84. {
  85. // printf("DUPS deck=%s random card=%s\n", deck[x], cards[random_integer]);
  86. goto pick_num;
  87. }
  88. x++;
  89. }
  90. }
  91. // for (int dd=0;dd<52;dd++){
  92. // printf("----->Random card =%s\n",deck[dd]);
  93. // }
  94.  
  95. if ( random_play_flag == 'Y' || random_play_flag =='y' )
  96. {
  97.  
  98.  
  99.  
  100. for ( int numcards=0;numcards<2;numcards++ )
  101. { //2 sets of cards
  102. for ( int index=0; index < numofplayers; index++ )
  103. {
  104. if ( numcards == 0 )
  105. {
  106. strcpy(players[index],deck[index]);
  107. }
  108. else
  109. {
  110.  
  111. strcat(players[index]," ");
  112. strcat(players[index],deck[index+numofplayers]);
  113. // printf("IN card 2=%d %d %s\n",numcards,index,deck[index+numofplayers]);
  114. }
  115. }
  116. }
  117. }
  118. else if ( random_play_flag == 'N' || random_play_flag =='n' )
  119. {
  120. // cout << "No random play" ;
  121. for ( int l=0; l<numofplayers;l++ )
  122. {
  123. printf("\n\nEnter Hand for Player %d = \n\n",l+1);
  124. cout << "Enter Card 1:\n";
  125. cin >> players[l];
  126. cout << "Enter Card 2:\n";
  127. strcat(players[l]," ");
  128. cin >> deck[0];
  129. strcat(players[l],deck[0]);
  130. }
  131. }
  132.  
  133. //Dealer
  134. //Burn a card - start with (numplayers*2)+1
  135. //Deal 3 cards
  136. strcpy(dealer[0],deck[(numofplayers*2)+1]);
  137. strcat(dealer[0]," ");
  138. strcat(dealer[0],deck[(numofplayers*2)+2]);
  139. strcat(dealer[0]," ");
  140. strcat(dealer[0],deck[(numofplayers*2)+3]);
  141. //1 dead card
  142. //deal card
  143. strcat(dealer[0]," ");
  144. strcat(dealer[0],deck[(numofplayers*2)+5]);
  145. //1 dead card
  146. //deal card
  147. strcat(dealer[0]," ");
  148. strcat(dealer[0],deck[(numofplayers*2)+7]);
  149.  
  150. for ( int z=0; z<numofplayers;z++ )
  151. {
  152. printf("\n\nPlayer %d Hand\n", z+1);
  153. printf("%s\n\n",players[z]);
  154.  
  155. }
  156.  
  157. printf("\n\nDEALERS HAND=%s\n\n",dealer[0]);
  158.  
  159. //Evaluate Hand for a flush
  160.  
  161.  
  162.  
  163. for ( int tt=0;tt<numofplayers;tt++ )
  164. {
  165. int diamondflush=0;
  166. int spadesflush=0;
  167. int clubflush=0;
  168. int heartflush=0;
  169.  
  170.  
  171.  
  172. strcpy(complete_hand[tt],dealer[0]);
  173. strcat(complete_hand[tt]," ");
  174. strcat(complete_hand[tt],players[tt]);
  175. printf("COMPLETE HAND FOR PLAYER %d = ",tt+1);
  176. for ( int u=0;u<21;u++ )
  177. {
  178. if ( complete_hand[tt][u] == 'D' )
  179. printf("%c",char(5));
  180. else if ( complete_hand[tt][u] == 'S' )
  181. printf("%c",char(4));
  182. else if ( complete_hand[tt][u] == 'C' )
  183. printf("%c",char(3));
  184. else if ( complete_hand[tt][u] == 'H' )
  185. printf("%c",char(6));
  186. else if ( complete_hand[tt][u] == 'T' )
  187. printf("%s","10");
  188. else
  189. printf("%c",complete_hand[tt][u]);
  190. }
  191. printf("\n");
  192. //printf("COMPLETE HAND FOR PLAYER %d = %s\n",tt+1, complete_hand[tt]);
  193. //printf("%c\n",complete_hand[0][1]);
  194. for ( int r=0; r<21;r=r+3 )
  195. {
  196. if ( complete_hand[tt][r] == 'D' )
  197. diamondflush++;
  198. if ( complete_hand[tt][r] == 'S' )
  199. spadesflush++;
  200. if ( complete_hand[tt][r] == 'C' )
  201. clubflush++;
  202. if ( complete_hand[tt][r] == 'H' )
  203. heartflush++;
  204. //printf("%c\n",char(5));
  205. }
  206.  
  207. if ( diamondflush >=5 )
  208. printf("You got a Flush\n");
  209. if ( spadesflush >=5 )
  210. printf("You got a Flush\n");
  211. if ( clubflush >=5 )
  212. printf("You got a Flush\n");
  213. if ( heartflush >=5 )
  214. printf("You got a Flush\n");
  215.  
  216. int twos=0;
  217. int threes=0;
  218. int fours=0;
  219. int fives=0;
  220. int six=0;
  221. int seven=0;
  222. int eight=0;
  223. int nine=0;
  224. int ten=0;
  225. int ace=0;
  226. int king=0;
  227. int queen=0;
  228. int jack=0;
  229. char three_cards='N';
  230. char two_cards='N';
  231.  
  232.  
  233. for ( int rr=1; rr<21;rr=rr+3 )
  234. {
  235. if ( complete_hand[tt][rr] == '2' )
  236. twos++;
  237. if ( complete_hand[tt][rr] == '3' )
  238. threes++;
  239. if ( complete_hand[tt][rr] == '4' )
  240. fours++;
  241. if ( complete_hand[tt][rr] == '5' )
  242. fives++;
  243. if ( complete_hand[tt][rr] == '6' )
  244. six++;
  245. if ( complete_hand[tt][rr] == '7' )
  246. seven++;
  247. if ( complete_hand[tt][rr] == '8' )
  248. eight++;
  249. if ( complete_hand[tt][rr] == '9' )
  250. nine++;
  251. if ( complete_hand[tt][rr] == 'T' )
  252. ten++;
  253. if ( complete_hand[tt][rr] == 'A' )
  254. ace++;
  255. if ( complete_hand[tt][rr] == 'K' )
  256. king++;
  257. if ( complete_hand[tt][rr] == 'Q' )
  258. queen++;
  259. if ( complete_hand[tt][rr] == 'J' )
  260. jack++;
  261. //printf("number=%c\n",complete_hand[tt][rr]);
  262. }
  263.  
  264.  
  265.  
  266. if ( twos==4 )
  267. printf("Four of a Kind 2\n");
  268. else if ( twos==3 )
  269. {
  270. printf("Three of a Kind 2\n");
  271. three_cards='Y';
  272. }
  273. else if ( twos==2 )
  274. {
  275. printf("Pairs 2\n");
  276. two_cards='Y';
  277. }
  278.  
  279. if ( threes==4 )
  280. printf("Four of a Kind 3\n");
  281. else if ( threes==3 )
  282. {
  283. printf("Three of a Kind 3\n");
  284. three_cards='Y';
  285. }
  286. else if ( threes==2 )
  287. {
  288. printf("Pairs 3\n");
  289. two_cards='Y';
  290. }
  291.  
  292. if ( fours==4 )
  293. printf("Four of a Kind 4\n");
  294. else if ( fours==3 )
  295. {
  296. printf("Three of a Kind 4\n");
  297. three_cards='Y';
  298. }
  299. else if ( fours==2 )
  300. {
  301. printf("Pairs 4\n");
  302. two_cards='Y';
  303. }
  304.  
  305. if ( fives==4 )
  306. printf("Four of a Kind 5\n");
  307. else if ( fives==3 )
  308. {
  309. printf("Three of a Kind 5\n");
  310. three_cards='Y';
  311. }
  312. else if ( fives==2 )
  313. {
  314. printf("Pairs 5\n");
  315. two_cards='Y';
  316. }
  317. if ( six==4 )
  318. printf("Four of a Kind 6\n");
  319. else if ( six==3 )
  320. {
  321. printf("Three of a Kind 6\n");
  322. three_cards='Y';
  323. }
  324. else if ( six==2 )
  325. {
  326. printf("Pairs 6\n");
  327. two_cards='Y';
  328. }
  329. if ( seven==4 )
  330. printf("Four of a Kind 7\n");
  331. else if ( seven==3 )
  332. {
  333. printf("Three of a Kind 7\n");
  334. three_cards='Y';
  335. }
  336. else if ( seven==2 )
  337. {
  338. printf("Pairs 7\n");
  339. two_cards='Y';
  340. }
  341. if ( eight==4 )
  342. printf("Four of a Kind 8\n");
  343. else if ( eight==3 )
  344. {
  345. printf("Three of a Kind 8\n");
  346. three_cards='Y';
  347. }
  348. else if ( eight==2 )
  349. {
  350. printf("Pairs 8\n");
  351. two_cards='Y';
  352. }
  353. if ( nine==4 )
  354. printf("Four of a Kind 9\n");
  355. else if ( nine==3 )
  356. {
  357. printf("Three of a Kind 9\n");
  358. three_cards='Y';
  359. }
  360. else if ( nine==2 )
  361. {
  362. printf("Pairs 9\n");
  363. two_cards='Y';
  364. }
  365. if ( ten==4 )
  366. printf("Four of a Kind 10\n");
  367. else if ( ten==3 )
  368. {
  369. printf("Three of a Kind 10\n");
  370. three_cards='Y';
  371. }
  372. else if ( ten==2 )
  373. {
  374. printf("Pairs 10\n");
  375. two_cards='Y';
  376. }
  377. if ( ace==4 )
  378. printf("Four of a Kind A\n");
  379. else if ( ace==3 )
  380. {
  381. printf("Three of a Kind A\n");
  382. three_cards='Y';
  383. }
  384. else if ( ace==2 )
  385. {
  386. printf("Pairs A\n");
  387. two_cards='Y';
  388. }
  389. if ( king==4 )
  390. printf("Four of a Kind K\n");
  391. else if ( king==3 )
  392. {
  393. printf("Three of a Kind K\n");
  394. three_cards='Y';
  395. }
  396. else if ( king==2 )
  397. {
  398. printf("Pairs Y\n");
  399. two_cards='Y';
  400. }
  401. if ( queen==4 )
  402. printf("Four of a Kind Q\n");
  403. else if ( queen==3 )
  404. {
  405. printf("Three of a Kind Q\n");
  406. three_cards='Y';
  407. }
  408. else if ( queen==2 )
  409. {
  410. printf("Pairs Q\n");
  411. two_cards='Y';
  412. }
  413. if ( jack==4 )
  414. printf("Four of a Kind J\n");
  415. else if ( jack==3 )
  416. {
  417. printf("Three of a Kind J\n");
  418. three_cards='Y';
  419. }
  420. else if ( jack==2 )
  421. {
  422. printf("Pairs J\n");
  423. two_cards='Y';
  424. }
  425.  
  426.  
  427. if ( three_cards =='Y' && two_cards == 'Y' )
  428. printf("Full House\n");
  429.  
  430. }
  431.  
  432. cout << "Play again? ";
  433. cin >>play_again;
  434. if ( play_again == 'Y' || play_again =='y' )
  435. {
  436. system("CLS");
  437. goto play_again_label;
  438. }
  439. return 0;
  440. }//end main
<< moderator edit: added [code][/code] tags and indenting >>
Reputation Points: 15
Solved Threads: 10
Unverified User
BinaryMayhem is offline Offline
173 posts
since Jun 2004
Aug 12th, 2005
0

Re: how to eliminate all keyboard inputs accept a certain range

hey stoned coder got a couple of questions. first thanks for responding its always hard to find good help. thanks to binary mayhem as well. one day when i am a gozillionare i will remember you guys. but what does the ! point in the while statement do. i know cin grabs the input stores it in input. what does the login on the ! point mean. what does cerr do. the .clear() is a function im guessing that is a built in function in the stanard library what does that do. if you could elaborate on this hold line i would be much abliged cin.ignore(numeric_limits<streamsize>::max(),'\n');
i just learn funny. i have to do a line by line every word thing. i pick up really fast if explained. kinda wierd i guess. thanks binary mayhem thanks you know i understand that and i had found that atoi keyword but i didnt think to use that thanks. while i got you guys attention do you guys ever write to the an example would be much abliged and you know how you give variables and amount and it stores that in memory. how do you erase all the amounts or do the variables just get erased at the end of the program. thanks.
bruce wayne
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bruce3210 is offline Offline
3 posts
since Aug 2005
Aug 12th, 2005
0

Re: how to eliminate all keyboard inputs accept a certain range

binary mayhem i put in what you wrote with the loop. if i put in a letter it quits the program. when atoi converts it to an int does it convert it to the decimal number of the letter or ascii. when a letter is put in i probaly want to have it loop back again. what about this you think.

  1. #include <stdio.h>
  2. #include <iostream.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <conio.h>
  7.  
  8.  
  9.  
  10. const int maxplayers=100;
  11.  
  12. //================================================== main
  13. int main() {
  14. char num_players[3];
  15. int numofplayers=0;
  16. char random_play_flag;
  17. char play_again;
  18.  
  19.  
  20.  
  21.  
  22. //char CARD_FACES[13] = {'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'};
  23. //char CARD_SUITS[4] = {'S', 'H', 'C', 'D'};
  24.  
  25. char cards[52][25] = { "SA", "S2", "S3", "S4", "S5", "S6", "S7",
  26. "S8", "S9","ST", "SJ", "SQ","SK",
  27. "HA", "H2", "H3", "H4", "H5", "H6", "H7"
  28. , "H8", "H9", "HT", "HJ", "HQ","HK",
  29. "CA", "C2", "C3", "C4", "C5", "C6", "C7"
  30. , "C8", "C9", "CT", "CJ", "CQ","CK",
  31. "DA", "D2", "D3", "D4", "D5", "D6", "D7"
  32. , "D8", "D9", "DT", "DJ", "DQ","DK" };
  33.  
  34. char players[52][25];
  35. char deck[52][25];
  36. char dealer[52][25];
  37. char complete_hand[52][25];
  38.  
  39.  
  40. /* srand(time(0)); // Initializes random 'seed'.*/
  41.  
  42. play_again_label:
  43. cout << "Enter Number of Players in Game: ";
  44. cin >>num_players;
  45. int slen = strlen(num_players);
  46. if (slen ==0 || slen >3){
  47. printf("Try Again\n");
  48. goto play_again_label;
  49. }
  50. for (int a=0;a<slen;a++){
  51. if (num_players[a] < '0' || num_players[a] > '9') {
  52. printf("Try Again\n");
  53. goto play_again_label;
  54. }
  55. numofplayers = atoi(num_players);
  56.  
  57. }
  58. if (numofplayers < 1 || numofplayers >20){
  59. printf("Try Again. With a number less than 20\n");
  60. goto play_again_label;
  61. }
  62. cout << "Do you want Random Play (Y/N)";
  63. cin >> random_play_flag ;
<< moderator edit: added code tags: [code][/code] >>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bruce3210 is offline Offline
3 posts
since Aug 2005
Aug 12th, 2005
0

Re: how to eliminate all keyboard inputs accept a certain range

Associated with cin are a set of error flags. The important one at this time is the fail flag. If the cin>>input goes to plan and grabs an int then this expression returns true through the overloaded operator void* (prolly too much info at this stage). operator ! is the not operator. It makes true flase and false true. Thats about it. cerr is like cout. its basically an error stream thats attatched to the console, so you could have used cout there but I like cerr as it can be easily rerouted to a logfile and cout left pointing to the console screen. If the cin>>input doesnt go to plan then the fail flag gets set and this causes the expression to return false. Then we enter into the block where firstly an error message is printed then the garbage in the stream is extracted. This is a two stage process. As you remember to get here the fail flag is set, but all ops on a failed stream are not garaunteed. So firstly we use a member function called clear to set the error flags back to good. Then we use another member function ignore to remove the detritus from the stream. That expression removes as many chars as possible or up to the next newline.
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Aug 13th, 2005
0

Re: how to eliminate all keyboard inputs accept a certain range

Quote ...
binary mayhem i put in what you wrote with the loop. if i put in a letter it quits the program. when atoi converts it to an int does it convert it to the decimal number of the letter or ascii. when a letter is put in i probaly want to have it loop back again. what about this you think.
sorry about that... I was in a huge rush... I didn't really think about that. no.. atoi does not convert it to ascii. it checks the string and converts all the numbers over. if it reaches a character it will stop reading and report back what it found numericaly. so A = 0.

I put the if (numofplayers==0) return 0; to quit the app. (what if the person does not want to play?) what should have been does was compared it to -1 so it would quit the app and zero would have just reported an error.
Reputation Points: 15
Solved Threads: 10
Unverified User
BinaryMayhem is offline Offline
173 posts
since Jun 2004

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 C Forum Timeline: convert int back to char
Next Thread in C Forum Timeline: can anyone please brief me on argc and argv in C.





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


Follow us on Twitter


© 2011 DaniWeb® LLC