error checking of user input

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 13
Reputation: cap2361 is an unknown quantity at this point 
Solved Threads: 0
cap2361 cap2361 is offline Offline
Newbie Poster

error checking of user input

 
0
  #1
Apr 16th, 2005
I am having problems with error checking. I want to ensure that the user puts in a name with characters and not use numbers. My thinking is that since player_name is data type char that using ( !player_name) should indicate not to input an int. any suggestions on how to go about this is greatly appreciated. thanks. I have no errors when compiling this but it doesn't work either.






void game::get_name()
{
cout << "Enter your name? ";
cin.getline(player_name, 50);

if(!player_name )
{
cout << "You must enter a name and not a number!" << "\n";

cout << "Enter your name? ";
cin.getline(player_name, 50);
}

}
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: error checking of user input

 
0
  #2
Apr 16th, 2005
I'm not sure but why dont you google "is Alpha" We where taught this a while back and it spring'd back to mind when I reda your post so it must have some meaning... TeeHee
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: error checking of user input

 
0
  #3
Apr 16th, 2005
>I want to ensure that the user puts in a name with characters and not use numbers.
Why? Numbers would be a valid string, and forcing your players to enter alphabetic characters may be an unnecessary restriction. Especially since it requires more work and code from you, and therefore more debugging and possible errors.

However, to do it you can use the standard library:
  1. #include <algorithm>
  2. #include <cctype>
  3. #include <cstring>
  4. #include <functional>
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. int alpha ( int c )
  10. {
  11. return isalpha ( c );
  12. }
  13.  
  14. void getname ( char s[], int n )
  15. {
  16. while ( cin.getline ( s, n )
  17. && *find_if ( s, s + strlen ( s ), not1 ( ptr_fun ( alpha ) ) ) != '\0' )
  18. {
  19. cout<<"No digits allowed. Try again: ";
  20. }
  21. }
Though it's a little awkward using C-style strings. If you were using std::strings then it would be much easier:
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. void getname ( string& s )
  7. {
  8. while ( getline ( cin, s )
  9. && s.find_first_of ( "0123456789" ) != string::npos )
  10. {
  11. cout<<"No digits allowed. Try again: ";
  12. }
  13. }
To do it manually you can use isalpha in a loop (a C-style solution), but the code is longer and harder to get right:
  1. #include <cctype>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. void getname ( char s[], int n )
  7. {
  8. while ( cin.getline ( s, n ) ) {
  9. char *p = s;
  10.  
  11. while ( *p != '\0' ) {
  12. if ( !isalpha ( *p ) ) {
  13. cout<<"No digits allowed. Try again: ";
  14. break;
  15. }
  16.  
  17. ++p;
  18. }
  19.  
  20. if ( *p == '\0' )
  21. break;
  22. }
  23. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 13
Reputation: cap2361 is an unknown quantity at this point 
Solved Threads: 0
cap2361 cap2361 is offline Offline
Newbie Poster

Re: error checking of user input

 
0
  #4
Apr 16th, 2005
Thank you so much. It's the little things that I always forget or miss.
This is all that was required to make it work... not equal to NULL


if(player_name != '\0')
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: error checking of user input

 
0
  #5
Apr 16th, 2005
>not equal to NULL
'\0' and NULL should not be mixed. If you do so it makes your code harder to follow. '\0' is the null character and should only be used in character context, and NULL is the null pointer and should only be used in pointer context.

>if(player_name != '\0')
If you're checking to see if the input is numeric then this doesn't do what you think it does. It checks pointer_name to see if it's a null pointer. However, if it were a null pointer, the program would have crashed here:
  1. cin.getline(player_name, 50);
You've replaced one broken construct that doesn't do what you want with another broken construct that doesn't do what you want.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 13
Reputation: cap2361 is an unknown quantity at this point 
Solved Threads: 0
cap2361 cap2361 is offline Offline
Newbie Poster

Re: error checking of user input

 
0
  #6
Apr 17th, 2005
You are so right about NULL. I have been doing research and have found nothing. If I did find anything it confused me. I am having a hard time finding how to check a string array, everything seems to be checking for int. Here is my whole program.
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <fstream.h>
  5. #include <string.h>
  6. #include <windows.h> // WinApi header
  7. #include <ctype.h>
  8.  
  9. class game
  10. {
  11. public:
  12.  
  13. game(); //default constuctor
  14. void blackjack();
  15.  
  16. protected:
  17. void shuffle(); //prototype for shuffle prog.
  18. void hit(); //deals one card at a time
  19. void first_deal(); //deals 2 cards to each player
  20. void display(); //displays cards, score, and money
  21. void player_hit_stay(); //player chooses to hit or stay
  22. void dealer_hit_stay(); //automated dealer hit/stay, hit if total <= 16
  23. void player_hand_totaler(); //sums cards in player's hand
  24. void dealer_hand_totaler(); //sums cards in dealer's hand
  25. void ace_change(); //if total is > 12 then changes aces (11's) to 1's. reruns totaler everytime it changes an ace
  26. void bust_check(); //checks if either player busts
  27. void reshuffle(); //reruns shuffle if current card is 52
  28. void clear_screen(); //couts 20 clear lines
  29. void clear_hands(); // clears arrays for player and dealer hands, resets bust, winner, hand totals, and bet amount
  30. void winner_check(); //checks for busts or (if no bust) greater hand total
  31. void get_name(); //cin's the person's name
  32. void intro(); //displays program picture and author info
  33. void bet(); //allows for players to enter a bet. checks for too high bets and negative bets.
  34. void win_money(); //if player wins, adds 2*bet to total, 3*bet if dealer busts
  35. int deck[52], player_hand[15], dealer_hand[15]; //sets array for deck, and for each player's hand
  36. int current_card, player_total, dealer_total; //current card # (in deck), and player/dealer hand total
  37. int player_card_number, dealer_card_number; //card # in player/dealers's hand
  38. int player; //sets 1 for player and 2 for dealer, affects turn
  39. int bust; //if total is >21, sets bust=1 for player and =2 for dealer.
  40. int winner; //set by winner_check, 1 for player, 2 for dealer, 3 for tie
  41. int bet_amount; //amount the player bets. entered in bet function
  42. char player_hit; //player enters y or n if they want to hit
  43.  
  44. public:
  45. void display_header(); //file display
  46. void display_record(); //display name and money from record
  47. int money_total; //total money the player currently has
  48. char player_name[50]; //player can enter their name, up to 49 characters long
  49.  
  50. };
  51.  
  52.  
  53.  
  54. game::game() //default consturctor
  55. {
  56. int i=0;
  57. for(i=0; i<=51; i++) //initialzes deck
  58. {
  59. deck[i]=0;
  60. }
  61. for(i=0; i<=14; i++) //initializes player/dealer hand arrays
  62. {
  63. player_hand[i]=0;
  64. dealer_hand[i]=0;
  65. }
  66.  
  67. current_card = 0;
  68. player_total = 0;
  69. dealer_total = 0;
  70. player_card_number = 0;
  71. dealer_card_number = 0;
  72. player = 1;
  73. player_hit = ' ';
  74. bust = 0;
  75. winner = 0;
  76. money_total = 100;
  77.  
  78.  
  79. }
  80.  
  81.  
  82. void game::intro()
  83. {
  84.  
  85.  
  86. cout << " ------------- -------------\n"
  87. << " | ----- J| | A|\n"
  88. << " | '<' | | A |\n"
  89. << " | - | | A A |\n"
  90. << " | / \\ | | A A |\n"
  91. << " | +-( J )-+ | | AAAAAAA |\n"
  92. << " | \\ / | | A A |\n"
  93. << " | | | | | A A |\n"
  94. << " |J - - | |A |\n"
  95. << " ------------- -------------\n"
  96. << " BLACKJACK 21!\n";
  97.  
  98. cout << "\n";
  99. get_name();
  100.  
  101. }
  102.  
  103. void game::get_name()
  104. {
  105. cout << "Enter your name? ";
  106. cin.getline(player_name, 50);
  107.  
  108.  
  109. //if(player_name )
  110. {
  111. cout << "You must enter a name and not a digit!" << "\n""\n";
  112.  
  113. clear_screen();
  114.  
  115. cout << "Enter your name? ";
  116. cin.getline(player_name, 50);
  117. }
  118.  
  119.  
  120. }
  121.  
  122. //player make bet
  123. void game::bet()
  124. {
  125. cout << "\n\nyou have $" << money_total;
  126. cout << "\nplease only bet in whole dollar amounts.";
  127. cout << "\nhow much do want to you bet? ";
  128. cin >> bet_amount;
  129.  
  130.  
  131. //checks bet amount to be in range of money
  132. while ((bet_amount > money_total)||(bet_amount < 0))
  133.  
  134. {
  135. //if bet is over money in players bank
  136. if (bet_amount > money_total)
  137. {
  138. cout << "\nyou bet more money than you have. no, you can't bet your car keys. fool.\n\n";
  139.  
  140. clear_screen();
  141.  
  142.  
  143. cout << "\n\nyou have $" << money_total;
  144. cout << "\nplease only bet in whole dollar amounts.";
  145. cout << "\nhow much do want to you bet? ";
  146. cin >> bet_amount;
  147. }
  148. //can't bet a negative amount
  149. else if (bet_amount < 0)
  150. {
  151. cout << "\nyou can't bet negative money!\n\n";
  152.  
  153. clear_screen();
  154.  
  155. cout << "\n\nyou have $" << money_total;
  156. cout << "\nplease only bet in whole dollar amounts.";
  157. cout << "\nhow much do want to you bet? ";
  158. cin >> bet_amount;
  159.  
  160. }
  161.  
  162.  
  163. }
  164. money_total = money_total - bet_amount;
  165.  
  166. }
  167.  
  168. void game::blackjack() //"main" part of program
  169. {
  170. intro();
  171. char play_again = 'y';
  172. shuffle();
  173. do
  174.  
  175. {
  176. bet();
  177. clear_screen();
  178. first_deal();
  179. player_hit_stay();
  180. if (bust == 1)
  181. {
  182. clear_screen();
  183.  
  184. cout << "you bust!\n\n";
  185. //winner = 2;
  186. }
  187. else if (bust == 0)
  188. {
  189. dealer_hit_stay();
  190. if (bust == 2)
  191. {
  192. clear_screen();
  193. cout << "dealer busts!\n\n";
  194. //winner = 1;
  195. }
  196. else if (bust == 0)
  197. {
  198. clear_screen();
  199. cout << "Dealer stays.\n\n";
  200.  
  201. }
  202. }
  203.  
  204. winner_check();//here winner_check will be called
  205.  
  206. if (money_total > 0)
  207. {
  208. cout << "\n\nanother round? (y for yes, n for no) ";
  209. cin >> play_again;
  210. while ((play_again != 'y')&&(play_again != 'Y')&&(play_again != 'n')&&(play_again != 'N'))
  211. {
  212. cout << "\n\nyou did not enter a y or n. please try again: ";
  213. cin >> play_again;
  214. }
  215.  
  216. clear_hands();
  217. clear_screen();
  218.  
  219. }
  220.  
  221. } //keep playing game
  222. while ((money_total > 0)&&((play_again == 'y')||(play_again == 'Y')));
  223.  
  224. clear_screen();
  225.  
  226.  
  227. //to save player name and money win to record
  228. // if((money_total > 0) && (play_again = 'n')&&(play_again = 'N'))
  229. //{
  230. // cout << "Save your name and winnings (Y for yes and N for n)";
  231. // cin >> play_again;
  232. //if((play_again = 'y')&&(play_again = 'Y'))
  233. // {
  234. // ofstream tofile; //save_name();
  235. //}
  236.  
  237. //}
  238.  
  239.  
  240. if (money_total <= 0)
  241. {
  242. cout << "\n\nsorry, you ran out of money. goodbye!\n";
  243.  
  244. }
  245.  
  246. clear_screen();
  247. cout << "\n";
  248. }
  249.  
  250.  
  251.  
  252. void game::shuffle()
  253. {
  254. int unshuffled[] = {2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14,14};
  255. //sets the unshuffled deck.
  256. int picked = 0; //number variable, used in function
  257.  
  258. srand(time(0));
  259.  
  260. for(int x=0; x<=51; x++)
  261. {
  262. picked = (1 + rand() % 52) ;
  263.  
  264. while (unshuffled[picked -1] == 0)
  265. //tests if card picked was already picked (set to 0). if so, chooses another.
  266. {
  267. picked = (1 + rand() % 52) ;
  268. }
  269.  
  270. deck[x] = unshuffled[picked - 1]; //sets card in deck used to card in unshuffled deck.
  271. unshuffled[picked -1] = 0; //"deletes" card in unshuffled deck so it cannot be used again.
  272. current_card=0;
  273. }
  274.  
  275. }
  276.  
  277. void game::hit()
  278. {
  279. reshuffle();
  280. if (player==1)
  281. {
  282. player_hand[player_card_number]=deck[current_card];
  283. player_card_number++;
  284. player_hand_totaler();
  285. }
  286.  
  287. if (player==2)
  288. {
  289. dealer_hand[dealer_card_number]=deck[current_card];
  290. dealer_card_number++;
  291. dealer_hand_totaler();
  292. }
  293. current_card++;
  294. ace_change();
  295. bust_check();
  296.  
  297. }
  298.  
  299. void game::first_deal()
  300. {
  301. player=1;
  302. hit();
  303. hit();
  304. player++;
  305. hit();
  306. hit();
  307.  
  308. }
  309.  
  310. void game::display()
  311. {
  312. int c=0;
  313.  
  314. if (winner == 0)
  315. {
  316. cout << "Dealer:\n";
  317. }
  318. else
  319. {
  320. cout << "Dealer: (total) " << dealer_total << "\n";
  321. }
  322.  
  323. for (c=0; c<=14; c++)//draws top of card
  324. {
  325. if (dealer_hand[c] != 0)
  326. {
  327. cout << " +---+ ";
  328. }
  329. }
  330. cout << endl;
  331. for (c=0; c<=14; c++)//draws top of card
  332. {
  333. if (dealer_hand[c] != 0)
  334. {
  335. cout << " | | ";
  336. }
  337. }
  338. cout << endl;
  339.  
  340. for (c=0; c<=14; c++)
  341. {
  342. //displays a J, Q, or K instead of card values 12, 13, and 14
  343. //J-K is 12-14 so that the Ace can be 11 automatically
  344. if (dealer_hand[c] != 0)
  345. {
  346. cout << " | ";
  347. if ((winner == 0)&&(c == 0))
  348. {
  349. cout << "? ";
  350. }
  351. else if (dealer_hand[c]==10)
  352. {
  353. cout << "10";
  354. }
  355. else if ((dealer_hand[c]==11)||(dealer_hand[c]==1))
  356. {
  357. cout << "A ";
  358. }
  359. else if (dealer_hand[c]==12)
  360. {
  361. cout << "J ";
  362. }
  363. else if (dealer_hand[c]==13)
  364. {
  365. cout << "Q ";
  366. }
  367. else if (dealer_hand[c]==14)
  368. {
  369. cout << "K ";
  370. }
  371. else
  372. {
  373. cout << dealer_hand[c] << " ";
  374. }
  375.  
  376. cout << "| ";
  377. }
  378. }
  379. cout << endl;
  380.  
  381. for (c=0; c<=14; c++)//draws middle of card
  382. {
  383. if (dealer_hand[c] != 0)
  384. {
  385. cout << " | | ";
  386. }
  387. }
  388. cout << endl;
  389.  
  390. for (c=0; c<=14; c++)//draws bottom of card
  391. {
  392. if (dealer_hand[c] != 0)
  393. {
  394. cout << " +---+ ";
  395. }
  396. }
  397. //displays players name, total of hand and money
  398. cout << "\n\n" << player_name << ": (total) " << player_total << ", total money: $" << money_total << "\n";
  399.  
  400. for (c=0; c<=14; c++)//draws top of card
  401. {
  402. if (player_hand[c] != 0)
  403. {
  404. cout << " +---+ ";
  405. }
  406. }
  407. cout << endl;
  408. for (c=0; c<=14; c++)//draws top of card
  409. {
  410. if (player_hand[c] != 0)
  411. {
  412. cout << " | | ";
  413. }
  414. }
  415. cout << endl;
  416.  
  417. for (c=0; c<=14; c++)
  418. {
  419. //displays a J, Q, or K instead of card values 12, 13, and 14
  420. //J-K is 12-14 so that the Ace can be 11 automatically
  421. if (player_hand[c] != 0)
  422. {
  423. cout << " | ";
  424. if (player_hand[c]==10)
  425. {
  426. cout << "10";
  427. }
  428. else if ((player_hand[c]==11)||(player_hand[c]==1))
  429. {
  430. cout << "A ";
  431. }
  432. else if (player_hand[c]==12)
  433. {
  434. cout << "J ";
  435. }
  436. else if (player_hand[c]==13)
  437. {
  438. cout << "Q ";
  439. }
  440. else if (player_hand[c]==14)
  441. {
  442. cout << "K ";
  443. }
  444. else
  445. {
  446. cout << player_hand[c] << " ";
  447. }
  448. cout << "| ";
  449. }
  450. }
  451. cout << endl;
  452.  
  453. for (c=0; c<=14; c++)//draws middle of card
  454. {
  455. if (player_hand[c] != 0)
  456. {
  457. cout << " | | ";
  458. }
  459. }
  460. cout << endl;
  461.  
  462. for (c=0; c<=14; c++)//draws bottom of card
  463. {
  464. if (player_hand[c] != 0)
  465. {
  466. cout << " +---+ ";
  467. }
  468. }
  469.  
  470.  
  471. }
  472.  
  473. void game::player_hit_stay()
  474. {
  475. player=1;
  476. player_hit = 'y';
  477.  
  478. while (((player_hit == 'y')||(player_hit == 'Y'))&&(bust == 0))
  479. {
  480. clear_screen();
  481. display();
  482. cout << "\n\nwould you like to hit? (y for yes or n for no) ";
  483. cin >> player_hit;
  484. while ((player_hit != 'y')&&(player_hit != 'Y')&&(player_hit != 'n')&&(player_hit != 'N'))
  485. {
  486. cout << "\n\nyou did not enter a y or n. please try again: ";
  487. cin >> player_hit;
  488. }
  489.  
  490. if ((player_hit == 'y')||(player_hit == 'Y'))
  491. {
  492. hit();
  493. }
  494. }
  495.  
  496. }
  497.  
  498. void game::dealer_hit_stay()
  499. {
  500. player = 2;
  501. while ((dealer_total < 17)&&(bust != 2))
  502. {
  503. if (bust == 0)
  504. {
  505. hit();
  506. clear_screen();
  507. cout << "\nDealer hits!\n";
  508. display();
  509. }
  510. }
  511.  
  512. }
  513.  
  514.  
  515. //converts k,q,j to value of 10 for player hand
  516. void game::player_hand_totaler()
  517. {
  518. int c=0;
  519. player_total=0;
  520. for(c=0; c<=14; c++) //totals player hand
  521. {
  522. //this if statement adds 10 for J, Q, and K instead of 12, 13, and 14
  523. //J-K is 12-14 so that the Ace can be 11 automatically
  524. if ((player_hand[c]==12)||(player_hand[c]==13)||(player_hand[c]==14))
  525. {
  526. player_total=player_total+10;
  527. }
  528. else // adds current card
  529. {
  530. player_total=player_total+player_hand[c];
  531. }
  532. }
  533. //return;
  534. }
  535. //converts k,q,j to value of 10 for dealer hand
  536. void game::dealer_hand_totaler()
  537. {
  538. int c=0;
  539. dealer_total=0;
  540. for(c=0; c<=14; c++) //totals player hand
  541. {
  542. //this if statement adds 10 for J, Q, and K instead of 12, 13, and 14
  543. //J-K is 12-14 so that the Ace can be 11 automatically
  544. if ((dealer_hand[c]==12)||(dealer_hand[c]==13)||(dealer_hand[c]==14))
  545. {
  546. dealer_total=dealer_total+10;
  547. }
  548. else //else adds current card
  549. {
  550. dealer_total=dealer_total+dealer_hand[c];
  551. }
  552. }
  553.  
  554. }
  555. //clears the screen
  556. void game::clear_screen()
  557. {
  558. system("cls");
  559. }
  560. //clears cards for new hand
  561. void game::clear_hands()
  562. {
  563. int c=0;
  564. player_card_number = 0;
  565. dealer_card_number = 0;
  566. player_total = 0;
  567. dealer_total = 0;
  568. bust = 0;
  569. winner = 0;
  570. bet_amount = 0;
  571. for (c=0; c<=14; c++)
  572. {
  573. player_hand[c] = 0;
  574. }
  575. for (c=0; c<=14; c++)
  576. {
  577. dealer_hand[c] = 0;
  578. }
  579.  
  580. }
  581. //checks for ace to be 1 or 11
  582. void game::ace_change()
  583. {
  584. int c=0;
  585. if ((player_total >= 22)&&(player_total != 21))
  586. {
  587. for (c=0; c <=14; c++)
  588. {
  589. if (player_hand[c]==11)
  590. {
  591. player_hand[c]=1;
  592. player_hand_totaler();
  593. }
  594. }
  595. }
  596. if ((dealer_total >= 22)&&(dealer_total != 21))
  597. {
  598. for (c=0; c <=14; c++)
  599. {
  600. if (dealer_hand[c]==11)
  601. {
  602. dealer_hand[c]=1;
  603. dealer_hand_totaler();
  604. }
  605. }
  606. }
  607. player_hand_totaler();
  608. dealer_hand_totaler();
  609. }
  610. //reshuffle cards for new hand
  611. void game::reshuffle()
  612. {
  613. if (current_card == 52)
  614. {
  615. shuffle();
  616. current_card=0;
  617. }
  618.  
  619. }
  620. //checks for bust
  621. void game::bust_check()
  622. {
  623. if (player_total >= 22)
  624. {
  625. bust = 1;
  626. }
  627.  
  628. if (dealer_total >= 22)
  629. {
  630. bust = 2;
  631. }
  632.  
  633. }
  634.  
  635. //in this blackjack game, your $ is already subtracted. therefore,
  636. //when you lose, no money is subtracted because your bet has already been subtracted.
  637. void game::winner_check()
  638. {
  639. if (bust == 0)
  640. {
  641. if (player_total > dealer_total)
  642. {
  643. winner = 1;
  644. win_money();
  645. display();
  646. cout << "\n\nyou win!\n\n";
  647. }
  648. else if (player_total < dealer_total)
  649. {
  650. winner = 2;
  651. display();
  652. cout << "\n\nyou lose!\n\n";
  653. }
  654. else if (player_total == dealer_total)
  655. {
  656. winner = 3;
  657. money_total = money_total + bet_amount; //you dont lose anything for a tie.
  658. display();
  659. cout << "\n\ntie!\n\n";
  660. }
  661. }
  662. else
  663. {
  664. if (bust == 1)
  665. {
  666. winner = 2;
  667. display();
  668. cout << "\n\nyou lose!\n\n";
  669. }
  670. if (bust == 2)
  671. {
  672. winner = 1;
  673. win_money();
  674. display();
  675. cout << "\n\nyou win!\n\n";
  676. }
  677. }
  678.  
  679. }
  680.  
  681. //determines money win
  682. void game::win_money()
  683. {
  684. if (bust == 2)//player gets twice his bet back if the dealer busts
  685. {
  686. money_total = money_total + (3 * bet_amount);
  687. }
  688. else
  689. {
  690. money_total = money_total + (2 * bet_amount);
  691. }
  692.  
  693. }
  694.  
  695. //display header for record
  696. void game::display_header()
  697. {
  698.  
  699.  
  700. cout << " Player Bank"<<"\n";
  701. cout << "___________________________";
  702. cout << "\n";
  703.  
  704.  
  705. }
  706. //display name and money amount
  707. void game::display_record()
  708. {
  709.  
  710. cout << player_name << "\t"<<"\t"<< money_total;
  711.  
  712. cout <<"\n";
  713. }
  714.  
  715. int main()
  716. {
  717. //code to color text to blue
  718. HANDLE hConsole;
  719. int k = 11; // pick the colorattribute k you want
  720. hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  721.  
  722.  
  723. SetConsoleTextAttribute(hConsole, k);
  724.  
  725.  
  726. game player;
  727.  
  728. player.blackjack();
  729.  
  730.  
  731.  
  732.  
  733. ofstream tofile;
  734.  
  735. tofile.open("Blackjack.txt", ios::app); //write to record
  736.  
  737. if(tofile.fail())
  738. {
  739. cout << "Record failed to open";
  740. exit(1);
  741. }
  742.  
  743. if (player.money_total > 0)
  744. {
  745. tofile << player.player_name <<"\n"<< player.money_total<<"\n";
  746. cout<<"\n";
  747.  
  748. }
  749. tofile.close();
  750.  
  751.  
  752.  
  753.  
  754. ifstream infile; //read from record
  755.  
  756. infile.open("Blackjack.txt");
  757.  
  758. player.game::display_header();
  759.  
  760.  
  761. while(!infile.eof())
  762. {
  763. infile >> player.player_name;
  764. infile >> player.money_total;
  765.  
  766. if(infile.eof() == 0 ) //to remove duplicacy
  767. {
  768. player.game::display_record();
  769. }
  770. }
  771. infile.close();
  772.  
  773. return 0;
  774. }
Code tags added. -Narue
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: error checking of user input

 
0
  #7
Apr 17th, 2005
>I am having a hard time finding how to check a string array
What do you mean by "check" a string array? There are a lot of things you could mean, and that makes answering your question difficult. It's also not wise to post reams of code. Shrink your code down to a small example program that exhibits the problems you're having, otherwise nobody will waste their time helping you.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 13
Reputation: cap2361 is an unknown quantity at this point 
Solved Threads: 0
cap2361 cap2361 is offline Offline
Newbie Poster

Re: error checking of user input

 
0
  #8
Apr 17th, 2005
I usually do not post entire code. will heed your warning. I read up on isalpha and isdigit. I cannot find anything that lets me write a condition statement when using a string array. the string will accept both char and int. int isalpha(char) returns a nonzero number if the character is a letter, otherwise it retruns a zero. this is what my book says. the code sniplets you provided use reference passing and pointers. I would rather avoid both because anything I add to this code gives me an error. So far I haven't found any information as to checking user input for char. I am ok if it is one letter, but cannot do it with strings. I haven't found anything on the web either that checks for char string input, everything is for int.

thx for all your help and advice
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: error checking of user input

 
0
  #9
Apr 17th, 2005
Give me an example of strings that would pass your test and strings that would fail your test. Your wording is too vague for me to help much.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 13
Reputation: cap2361 is an unknown quantity at this point 
Solved Threads: 0
cap2361 cap2361 is offline Offline
Newbie Poster

Re: error checking of user input

 
0
  #10
Apr 17th, 2005
User is asked to enter name. char player_name[50]; User enters name (example) Charlie. the program gets no errors because charlie is all letters of the alphabet. next user comes along and when asked to enter name, enters cha3l43 or 343455. this is not acceptable and user needs to enter name made up of letters from alphabet. I am sorry that I am having trouble explaining this. I have never had to write condition statement for string input before.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC