| | |
error checking of user input
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2005
Posts: 13
Reputation:
Solved Threads: 0
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);
}
}
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);
}
}
>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:
Though it's a little awkward using C-style strings. If you were using std::strings then it would be much easier:
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:
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:
C++ Syntax (Toggle Plain Text)
#include <algorithm> #include <cctype> #include <cstring> #include <functional> #include <iostream> using namespace std; int alpha ( int c ) { return isalpha ( c ); } void getname ( char s[], int n ) { while ( cin.getline ( s, n ) && *find_if ( s, s + strlen ( s ), not1 ( ptr_fun ( alpha ) ) ) != '\0' ) { cout<<"No digits allowed. Try again: "; } }
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; void getname ( string& s ) { while ( getline ( cin, s ) && s.find_first_of ( "0123456789" ) != string::npos ) { cout<<"No digits allowed. Try again: "; } }
C++ Syntax (Toggle Plain Text)
#include <cctype> #include <iostream> using namespace std; void getname ( char s[], int n ) { while ( cin.getline ( s, n ) ) { char *p = s; while ( *p != '\0' ) { if ( !isalpha ( *p ) ) { cout<<"No digits allowed. Try again: "; break; } ++p; } if ( *p == '\0' ) break; } }
I'm here to prove you wrong.
>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:
You've replaced one broken construct that doesn't do what you want with another broken construct that doesn't do what you want.
'\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:
C++ Syntax (Toggle Plain Text)
cin.getline(player_name, 50);
I'm here to prove you wrong.
•
•
Join Date: Feb 2005
Posts: 13
Reputation:
Solved Threads: 0
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.
Code tags added. -Narue
C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <stdlib.h> #include <time.h> #include <fstream.h> #include <string.h> #include <windows.h> // WinApi header #include <ctype.h> class game { public: game(); //default constuctor void blackjack(); protected: void shuffle(); //prototype for shuffle prog. void hit(); //deals one card at a time void first_deal(); //deals 2 cards to each player void display(); //displays cards, score, and money void player_hit_stay(); //player chooses to hit or stay void dealer_hit_stay(); //automated dealer hit/stay, hit if total <= 16 void player_hand_totaler(); //sums cards in player's hand void dealer_hand_totaler(); //sums cards in dealer's hand void ace_change(); //if total is > 12 then changes aces (11's) to 1's. reruns totaler everytime it changes an ace void bust_check(); //checks if either player busts void reshuffle(); //reruns shuffle if current card is 52 void clear_screen(); //couts 20 clear lines void clear_hands(); // clears arrays for player and dealer hands, resets bust, winner, hand totals, and bet amount void winner_check(); //checks for busts or (if no bust) greater hand total void get_name(); //cin's the person's name void intro(); //displays program picture and author info void bet(); //allows for players to enter a bet. checks for too high bets and negative bets. void win_money(); //if player wins, adds 2*bet to total, 3*bet if dealer busts int deck[52], player_hand[15], dealer_hand[15]; //sets array for deck, and for each player's hand int current_card, player_total, dealer_total; //current card # (in deck), and player/dealer hand total int player_card_number, dealer_card_number; //card # in player/dealers's hand int player; //sets 1 for player and 2 for dealer, affects turn int bust; //if total is >21, sets bust=1 for player and =2 for dealer. int winner; //set by winner_check, 1 for player, 2 for dealer, 3 for tie int bet_amount; //amount the player bets. entered in bet function char player_hit; //player enters y or n if they want to hit public: void display_header(); //file display void display_record(); //display name and money from record int money_total; //total money the player currently has char player_name[50]; //player can enter their name, up to 49 characters long }; game::game() //default consturctor { int i=0; for(i=0; i<=51; i++) //initialzes deck { deck[i]=0; } for(i=0; i<=14; i++) //initializes player/dealer hand arrays { player_hand[i]=0; dealer_hand[i]=0; } current_card = 0; player_total = 0; dealer_total = 0; player_card_number = 0; dealer_card_number = 0; player = 1; player_hit = ' '; bust = 0; winner = 0; money_total = 100; } void game::intro() { cout << " ------------- -------------\n" << " | ----- J| | A|\n" << " | '<' | | A |\n" << " | - | | A A |\n" << " | / \\ | | A A |\n" << " | +-( J )-+ | | AAAAAAA |\n" << " | \\ / | | A A |\n" << " | | | | | A A |\n" << " |J - - | |A |\n" << " ------------- -------------\n" << " BLACKJACK 21!\n"; cout << "\n"; get_name(); } 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 digit!" << "\n""\n"; clear_screen(); cout << "Enter your name? "; cin.getline(player_name, 50); } } //player make bet void game::bet() { cout << "\n\nyou have $" << money_total; cout << "\nplease only bet in whole dollar amounts."; cout << "\nhow much do want to you bet? "; cin >> bet_amount; //checks bet amount to be in range of money while ((bet_amount > money_total)||(bet_amount < 0)) { //if bet is over money in players bank if (bet_amount > money_total) { cout << "\nyou bet more money than you have. no, you can't bet your car keys. fool.\n\n"; clear_screen(); cout << "\n\nyou have $" << money_total; cout << "\nplease only bet in whole dollar amounts."; cout << "\nhow much do want to you bet? "; cin >> bet_amount; } //can't bet a negative amount else if (bet_amount < 0) { cout << "\nyou can't bet negative money!\n\n"; clear_screen(); cout << "\n\nyou have $" << money_total; cout << "\nplease only bet in whole dollar amounts."; cout << "\nhow much do want to you bet? "; cin >> bet_amount; } } money_total = money_total - bet_amount; } void game::blackjack() //"main" part of program { intro(); char play_again = 'y'; shuffle(); do { bet(); clear_screen(); first_deal(); player_hit_stay(); if (bust == 1) { clear_screen(); cout << "you bust!\n\n"; //winner = 2; } else if (bust == 0) { dealer_hit_stay(); if (bust == 2) { clear_screen(); cout << "dealer busts!\n\n"; //winner = 1; } else if (bust == 0) { clear_screen(); cout << "Dealer stays.\n\n"; } } winner_check();//here winner_check will be called if (money_total > 0) { cout << "\n\nanother round? (y for yes, n for no) "; cin >> play_again; while ((play_again != 'y')&&(play_again != 'Y')&&(play_again != 'n')&&(play_again != 'N')) { cout << "\n\nyou did not enter a y or n. please try again: "; cin >> play_again; } clear_hands(); clear_screen(); } } //keep playing game while ((money_total > 0)&&((play_again == 'y')||(play_again == 'Y'))); clear_screen(); //to save player name and money win to record // if((money_total > 0) && (play_again = 'n')&&(play_again = 'N')) //{ // cout << "Save your name and winnings (Y for yes and N for n)"; // cin >> play_again; //if((play_again = 'y')&&(play_again = 'Y')) // { // ofstream tofile; //save_name(); //} //} if (money_total <= 0) { cout << "\n\nsorry, you ran out of money. goodbye!\n"; } clear_screen(); cout << "\n"; } void game::shuffle() { 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}; //sets the unshuffled deck. int picked = 0; //number variable, used in function srand(time(0)); for(int x=0; x<=51; x++) { picked = (1 + rand() % 52) ; while (unshuffled[picked -1] == 0) //tests if card picked was already picked (set to 0). if so, chooses another. { picked = (1 + rand() % 52) ; } deck[x] = unshuffled[picked - 1]; //sets card in deck used to card in unshuffled deck. unshuffled[picked -1] = 0; //"deletes" card in unshuffled deck so it cannot be used again. current_card=0; } } void game::hit() { reshuffle(); if (player==1) { player_hand[player_card_number]=deck[current_card]; player_card_number++; player_hand_totaler(); } if (player==2) { dealer_hand[dealer_card_number]=deck[current_card]; dealer_card_number++; dealer_hand_totaler(); } current_card++; ace_change(); bust_check(); } void game::first_deal() { player=1; hit(); hit(); player++; hit(); hit(); } void game::display() { int c=0; if (winner == 0) { cout << "Dealer:\n"; } else { cout << "Dealer: (total) " << dealer_total << "\n"; } for (c=0; c<=14; c++)//draws top of card { if (dealer_hand[c] != 0) { cout << " +---+ "; } } cout << endl; for (c=0; c<=14; c++)//draws top of card { if (dealer_hand[c] != 0) { cout << " | | "; } } cout << endl; for (c=0; c<=14; c++) { //displays a J, Q, or K instead of card values 12, 13, and 14 //J-K is 12-14 so that the Ace can be 11 automatically if (dealer_hand[c] != 0) { cout << " | "; if ((winner == 0)&&(c == 0)) { cout << "? "; } else if (dealer_hand[c]==10) { cout << "10"; } else if ((dealer_hand[c]==11)||(dealer_hand[c]==1)) { cout << "A "; } else if (dealer_hand[c]==12) { cout << "J "; } else if (dealer_hand[c]==13) { cout << "Q "; } else if (dealer_hand[c]==14) { cout << "K "; } else { cout << dealer_hand[c] << " "; } cout << "| "; } } cout << endl; for (c=0; c<=14; c++)//draws middle of card { if (dealer_hand[c] != 0) { cout << " | | "; } } cout << endl; for (c=0; c<=14; c++)//draws bottom of card { if (dealer_hand[c] != 0) { cout << " +---+ "; } } //displays players name, total of hand and money cout << "\n\n" << player_name << ": (total) " << player_total << ", total money: $" << money_total << "\n"; for (c=0; c<=14; c++)//draws top of card { if (player_hand[c] != 0) { cout << " +---+ "; } } cout << endl; for (c=0; c<=14; c++)//draws top of card { if (player_hand[c] != 0) { cout << " | | "; } } cout << endl; for (c=0; c<=14; c++) { //displays a J, Q, or K instead of card values 12, 13, and 14 //J-K is 12-14 so that the Ace can be 11 automatically if (player_hand[c] != 0) { cout << " | "; if (player_hand[c]==10) { cout << "10"; } else if ((player_hand[c]==11)||(player_hand[c]==1)) { cout << "A "; } else if (player_hand[c]==12) { cout << "J "; } else if (player_hand[c]==13) { cout << "Q "; } else if (player_hand[c]==14) { cout << "K "; } else { cout << player_hand[c] << " "; } cout << "| "; } } cout << endl; for (c=0; c<=14; c++)//draws middle of card { if (player_hand[c] != 0) { cout << " | | "; } } cout << endl; for (c=0; c<=14; c++)//draws bottom of card { if (player_hand[c] != 0) { cout << " +---+ "; } } } void game::player_hit_stay() { player=1; player_hit = 'y'; while (((player_hit == 'y')||(player_hit == 'Y'))&&(bust == 0)) { clear_screen(); display(); cout << "\n\nwould you like to hit? (y for yes or n for no) "; cin >> player_hit; while ((player_hit != 'y')&&(player_hit != 'Y')&&(player_hit != 'n')&&(player_hit != 'N')) { cout << "\n\nyou did not enter a y or n. please try again: "; cin >> player_hit; } if ((player_hit == 'y')||(player_hit == 'Y')) { hit(); } } } void game::dealer_hit_stay() { player = 2; while ((dealer_total < 17)&&(bust != 2)) { if (bust == 0) { hit(); clear_screen(); cout << "\nDealer hits!\n"; display(); } } } //converts k,q,j to value of 10 for player hand void game::player_hand_totaler() { int c=0; player_total=0; for(c=0; c<=14; c++) //totals player hand { //this if statement adds 10 for J, Q, and K instead of 12, 13, and 14 //J-K is 12-14 so that the Ace can be 11 automatically if ((player_hand[c]==12)||(player_hand[c]==13)||(player_hand[c]==14)) { player_total=player_total+10; } else // adds current card { player_total=player_total+player_hand[c]; } } //return; } //converts k,q,j to value of 10 for dealer hand void game::dealer_hand_totaler() { int c=0; dealer_total=0; for(c=0; c<=14; c++) //totals player hand { //this if statement adds 10 for J, Q, and K instead of 12, 13, and 14 //J-K is 12-14 so that the Ace can be 11 automatically if ((dealer_hand[c]==12)||(dealer_hand[c]==13)||(dealer_hand[c]==14)) { dealer_total=dealer_total+10; } else //else adds current card { dealer_total=dealer_total+dealer_hand[c]; } } } //clears the screen void game::clear_screen() { system("cls"); } //clears cards for new hand void game::clear_hands() { int c=0; player_card_number = 0; dealer_card_number = 0; player_total = 0; dealer_total = 0; bust = 0; winner = 0; bet_amount = 0; for (c=0; c<=14; c++) { player_hand[c] = 0; } for (c=0; c<=14; c++) { dealer_hand[c] = 0; } } //checks for ace to be 1 or 11 void game::ace_change() { int c=0; if ((player_total >= 22)&&(player_total != 21)) { for (c=0; c <=14; c++) { if (player_hand[c]==11) { player_hand[c]=1; player_hand_totaler(); } } } if ((dealer_total >= 22)&&(dealer_total != 21)) { for (c=0; c <=14; c++) { if (dealer_hand[c]==11) { dealer_hand[c]=1; dealer_hand_totaler(); } } } player_hand_totaler(); dealer_hand_totaler(); } //reshuffle cards for new hand void game::reshuffle() { if (current_card == 52) { shuffle(); current_card=0; } } //checks for bust void game::bust_check() { if (player_total >= 22) { bust = 1; } if (dealer_total >= 22) { bust = 2; } } //in this blackjack game, your $ is already subtracted. therefore, //when you lose, no money is subtracted because your bet has already been subtracted. void game::winner_check() { if (bust == 0) { if (player_total > dealer_total) { winner = 1; win_money(); display(); cout << "\n\nyou win!\n\n"; } else if (player_total < dealer_total) { winner = 2; display(); cout << "\n\nyou lose!\n\n"; } else if (player_total == dealer_total) { winner = 3; money_total = money_total + bet_amount; //you dont lose anything for a tie. display(); cout << "\n\ntie!\n\n"; } } else { if (bust == 1) { winner = 2; display(); cout << "\n\nyou lose!\n\n"; } if (bust == 2) { winner = 1; win_money(); display(); cout << "\n\nyou win!\n\n"; } } } //determines money win void game::win_money() { if (bust == 2)//player gets twice his bet back if the dealer busts { money_total = money_total + (3 * bet_amount); } else { money_total = money_total + (2 * bet_amount); } } //display header for record void game::display_header() { cout << " Player Bank"<<"\n"; cout << "___________________________"; cout << "\n"; } //display name and money amount void game::display_record() { cout << player_name << "\t"<<"\t"<< money_total; cout <<"\n"; } int main() { //code to color text to blue HANDLE hConsole; int k = 11; // pick the colorattribute k you want hConsole = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hConsole, k); game player; player.blackjack(); ofstream tofile; tofile.open("Blackjack.txt", ios::app); //write to record if(tofile.fail()) { cout << "Record failed to open"; exit(1); } if (player.money_total > 0) { tofile << player.player_name <<"\n"<< player.money_total<<"\n"; cout<<"\n"; } tofile.close(); ifstream infile; //read from record infile.open("Blackjack.txt"); player.game::display_header(); while(!infile.eof()) { infile >> player.player_name; infile >> player.money_total; if(infile.eof() == 0 ) //to remove duplicacy { player.game::display_record(); } } infile.close(); return 0; }
>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.
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.
•
•
Join Date: Feb 2005
Posts: 13
Reputation:
Solved Threads: 0
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
thx for all your help and advice
•
•
Join Date: Feb 2005
Posts: 13
Reputation:
Solved Threads: 0
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.
![]() |
Similar Threads
- Help with 1.pointers and 2.error checking (C++)
- Error Checking for user input (Java)
- Need Help With Error Checking User Input (C)
Other Threads in the C++ Forum
- Previous Thread: Array troubles?
- Next Thread: Dynamic Disaster
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector visualstudio win32 windows winsock word wordfrequency wxwidgets






