| | |
how to eliminate all keyboard inputs accept a certain range
![]() |
•
•
Join Date: Aug 2005
Posts: 3
Reputation:
Solved Threads: 0
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.
<< moderator edit: added [code][/code] tags >>
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <iostream.h> #include <string.h> #include <stdlib.h> #include <time.h> #include <conio.h> //================================================== main int main() { int numofplayers=0; char random_play_flag; char play_again; //char CARD_FACES[13] = {'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'}; //char CARD_SUITS[4] = {'S', 'H', 'C', 'D'}; char cards[52][25] = { "SA", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9","ST", "SJ", "SQ","SK", "HA", "H2", "H3", "H4", "H5", "H6", "H7" , "H8", "H9", "HT", "HJ", "HQ","HK", "CA", "C2", "C3", "C4", "C5", "C6", "C7" , "C8", "C9", "CT", "CJ", "CQ","CK", "DA", "D2", "D3", "D4", "D5", "D6", "D7" , "D8", "D9", "DT", "DJ", "DQ","DK" }; char players[52][25]; char deck[52][25]; char dealer[52][25]; char complete_hand[52][25]; play_again_label: cout << endl; cout << "Enter Number of Players in Game: "; cin >>numofplayers; if (numofplayers < 1 || numofplayers > 20) { cout << "Please try a number less than 20"; goto play_again_label; } start_again: cout << "Do you want Random Play (Y/N)"; cin >> random_play_flag ; if ( random_play_flag != 'y' && random_play_flag != 'Y' && random_play_flag != 'n' && random_play_flag != 'N') { cout << "Please type y or n"; cout << endl; goto start_again; } //shuffle deck srand((unsigned)time(0)); int gen_random=0; int random_integer; int lowest=0, highest=51; int range=(highest-lowest)+1; for (int d=0;d<52;d++){ pick_num: random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)); // printf("rand num=%d\n", random_integer); strcpy(deck[d],cards[random_integer]); int x=0; while( x < d){ if (!strcmp(deck[x], cards[random_integer])){ // printf("DUPS deck=%s random card=%s\n", deck[x], cards[random_integer]); goto pick_num; } x++; } } // for (int dd=0;dd<52;dd++){ // printf("----->Random card =%s\n",deck[dd]); // } if (random_play_flag == 'Y' || random_play_flag =='y') { for (int numcards=0;numcards<2;numcards++){ //2 sets of cards for(int index=0; index < numofplayers; index++){ if (numcards == 0){ strcpy(players[index],deck[index]); } else{ strcat(players[index]," "); strcat(players[index],deck[index+numofplayers]); // printf("IN card 2=%d %d %s\n",numcards,index,deck[index+numofplayers]); } } } } else if (random_play_flag == 'N' || random_play_flag =='n'){ // cout << "No random play" ; for (int l=0; l<numofplayers;l++){ printf("\n\nEnter Hand for Player %d = \n\n",l+1); cout << "Enter Card 1:\n"; cin >> players[l]; cout << "Enter Card 2:\n"; strcat(players[l]," "); cin >> deck[0]; strcat(players[l],deck[0]); } } //Dealer //Burn a card - start with (numplayers*2)+1 //Deal 3 cards strcpy(dealer[0],deck[(numofplayers*2)+1]); strcat(dealer[0]," "); strcat(dealer[0],deck[(numofplayers*2)+2]); strcat(dealer[0]," "); strcat(dealer[0],deck[(numofplayers*2)+3]); //1 dead card //deal card strcat(dealer[0]," "); strcat(dealer[0],deck[(numofplayers*2)+5]); //1 dead card //deal card strcat(dealer[0]," "); strcat(dealer[0],deck[(numofplayers*2)+7]); for (int z=0; z<numofplayers;z++){ printf("\n\nPlayer %d Hand\n", z+1); printf("%s\n\n",players[z]); } printf("\n\nDEALERS HAND=%s\n\n",dealer[0]); //Evaluate Hand for a flush for (int tt=0;tt<numofplayers;tt++){ int diamondflush=0; int spadesflush=0; int clubflush=0; int heartflush=0; strcpy(complete_hand[tt],dealer[0]); strcat(complete_hand[tt]," "); strcat(complete_hand[tt],players[tt]); printf("COMPLETE HAND FOR PLAYER %d = ",tt+1); for (int u=0;u<21;u++){ if (complete_hand[tt][u] == 'D') printf("%c",char(5)); else if (complete_hand[tt][u] == 'S') printf("%c",char(4)); else if (complete_hand[tt][u] == 'C') printf("%c",char(3)); else if (complete_hand[tt][u] == 'H') printf("%c",char(6)); else if (complete_hand[tt][u] == 'T') printf("%s","10"); else printf("%c",complete_hand[tt][u]); } printf("\n"); //printf("COMPLETE HAND FOR PLAYER %d = %s\n",tt+1, complete_hand[tt]); //printf("%c\n",complete_hand[0][1]); for (int r=0; r<21;r=r+3) { if (complete_hand[tt][r] == 'D') diamondflush++; if (complete_hand[tt][r] == 'S') spadesflush++; if (complete_hand[tt][r] == 'C') clubflush++; if (complete_hand[tt][r] == 'H') heartflush++; //printf("%c\n",char(5)); } if (diamondflush >=5) printf("You got a Flush\n"); if (spadesflush >=5) printf("You got a Flush\n"); if (clubflush >=5) printf("You got a Flush\n"); if (heartflush >=5) printf("You got a Flush\n"); int twos=0; int threes=0; int fours=0; int fives=0; int six=0; int seven=0; int eight=0; int nine=0; int ten=0; int ace=0; int king=0; int queen=0; int jack=0; char three_cards='N'; char two_cards='N'; for (int rr=1; rr<21;rr=rr+3) { if (complete_hand[tt][rr] == '2') twos++; if (complete_hand[tt][rr] == '3') threes++; if (complete_hand[tt][rr] == '4') fours++; if (complete_hand[tt][rr] == '5') fives++; if (complete_hand[tt][rr] == '6') six++; if (complete_hand[tt][rr] == '7') seven++; if (complete_hand[tt][rr] == '8') eight++; if (complete_hand[tt][rr] == '9') nine++; if (complete_hand[tt][rr] == 'T') ten++; if (complete_hand[tt][rr] == 'A') ace++; if (complete_hand[tt][rr] == 'K') king++; if (complete_hand[tt][rr] == 'Q') queen++; if (complete_hand[tt][rr] == 'J') jack++; //printf("number=%c\n",complete_hand[tt][rr]); } if (twos==4) printf("Four of a Kind 2\n"); else if (twos==3){ printf("Three of a Kind 2\n"); three_cards='Y'; } else if (twos==2){ printf("Pairs 2\n"); two_cards='Y'; } if (threes==4) printf("Four of a Kind 3\n"); else if (threes==3){ printf("Three of a Kind 3\n"); three_cards='Y'; } else if (threes==2){ printf("Pairs 3\n"); two_cards='Y'; } if (fours==4) printf("Four of a Kind 4\n"); else if (fours==3){ printf("Three of a Kind 4\n"); three_cards='Y'; } else if(fours==2){ printf("Pairs 4\n"); two_cards='Y'; } if (fives==4) printf("Four of a Kind 5\n"); else if(fives==3){ printf("Three of a Kind 5\n"); three_cards='Y'; } else if (fives==2){ printf("Pairs 5\n"); two_cards='Y'; } if (six==4) printf("Four of a Kind 6\n"); else if (six==3){ printf("Three of a Kind 6\n"); three_cards='Y'; } else if (six==2){ printf("Pairs 6\n"); two_cards='Y'; } if (seven==4) printf("Four of a Kind 7\n"); else if(seven==3){ printf("Three of a Kind 7\n"); three_cards='Y'; } else if(seven==2){ printf("Pairs 7\n"); two_cards='Y'; } if (eight==4) printf("Four of a Kind 8\n"); else if (eight==3){ printf("Three of a Kind 8\n"); three_cards='Y'; } else if (eight==2){ printf("Pairs 8\n"); two_cards='Y'; } if (nine==4) printf("Four of a Kind 9\n"); else if (nine==3){ printf("Three of a Kind 9\n"); three_cards='Y'; } else if (nine==2){ printf("Pairs 9\n"); two_cards='Y'; } if (ten==4) printf("Four of a Kind 10\n"); else if (ten==3){ printf("Three of a Kind 10\n"); three_cards='Y'; } else if (ten==2){ printf("Pairs 10\n"); two_cards='Y'; } if (ace==4) printf("Four of a Kind A\n"); else if (ace==3){ printf("Three of a Kind A\n"); three_cards='Y'; } else if (ace==2){ printf("Pairs A\n"); two_cards='Y'; } if (king==4) printf("Four of a Kind K\n"); else if (king==3){ printf("Three of a Kind K\n"); three_cards='Y'; } else if (king==2){ printf("Pairs Y\n"); two_cards='Y'; } if (queen==4) printf("Four of a Kind Q\n"); else if (queen==3){ printf("Three of a Kind Q\n"); three_cards='Y'; } else if (queen==2){ printf("Pairs Q\n"); two_cards='Y'; } if (jack==4) printf("Four of a Kind J\n"); else if (jack==3){ printf("Three of a Kind J\n"); three_cards='Y'; } else if(jack==2){ printf("Pairs J\n"); two_cards='Y'; } if (three_cards =='Y' && two_cards == 'Y') printf("Full House\n"); } cout << "Play again? "; cin >>play_again; if (play_again == 'Y' || play_again =='y') { system("CLS"); goto play_again_label; } return 0; }//end main
•
•
Join Date: Jul 2005
Posts: 164
Reputation:
Solved Threads: 5
In general you can do something like this although you should drop the deprecated headers for the std headers.
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).....
Although often I will make a validate function ifthe expression gets complicated and just call that function there instead.
C Syntax (Toggle Plain Text)
int input; while(!(cin>>input)) { cerr<<endl<<"enter an int wally!!!"<<endl; cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); } // if int entered, we get to here
It is also possible to validate input like this to a range of ints something like this (from a previous thread I participated in).....
C Syntax (Toggle Plain Text)
while (!(cin>>row>>seatnum) || row<1 || row>12 || seatnum<1 || seatnum>4) { cerr<<endl<<"error! re-enter"<<endl; cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); } AllocSeat(row,seatnum);
•
•
Join Date: Jun 2004
Posts: 173
Reputation:
Solved Threads: 9
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!
<< moderator edit: added [code][/code] tags and indenting >>
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!
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <iostream.h> #include <string.h> #include <stdlib.h> #include <time.h> #include <conio.h> //================================================== main int main() { int numofplayers=0; char strnumofplayers[3]; char random_play_flag; char play_again; //char CARD_FACES[13] = {'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'}; //char CARD_SUITS[4] = {'S', 'H', 'C', 'D'}; char cards[52][25] = { "SA", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9","ST", "SJ", "SQ","SK", "HA", "H2", "H3", "H4", "H5", "H6", "H7" , "H8", "H9", "HT", "HJ", "HQ","HK", "CA", "C2", "C3", "C4", "C5", "C6", "C7" , "C8", "C9", "CT", "CJ", "CQ","CK", "DA", "D2", "D3", "D4", "D5", "D6", "D7" , "D8", "D9", "DT", "DJ", "DQ","DK"}; char players[52][25]; char deck[52][25]; char dealer[52][25]; char complete_hand[52][25]; play_again_label: do { cout << endl; cout << "Enter Number of Players in Game: "; cin >> strnumofplayers; numofplayers = atoi(strnumofplayers); if ( numofplayers == 0 ) return 0; // quit the game! if ( numofplayers < 1 || numofplayers > 20 ) { cout << "Please try a number less than 20"; } } while ( numofplayers < 1 || numofplayers > 20 ); start_again: cout << "Do you want Random Play (Y/N)"; cin >> random_play_flag ; if ( random_play_flag != 'y' && random_play_flag != 'Y' && random_play_flag != 'n' && random_play_flag != 'N' ) { cout << "Please type y or n"; cout << endl; goto start_again; } //shuffle deck srand((unsigned)time(0)); int gen_random=0; int random_integer; int lowest=0, highest=51; int range=(highest-lowest)+1; for ( int d=0;d<52;d++ ) { pick_num: random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)); // printf("rand num=%d\n", random_integer); strcpy(deck[d],cards[random_integer]); int x=0; while ( x < d ) { if ( !strcmp(deck[x], cards[random_integer]) ) { // printf("DUPS deck=%s random card=%s\n", deck[x], cards[random_integer]); goto pick_num; } x++; } } // for (int dd=0;dd<52;dd++){ // printf("----->Random card =%s\n",deck[dd]); // } if ( random_play_flag == 'Y' || random_play_flag =='y' ) { for ( int numcards=0;numcards<2;numcards++ ) { //2 sets of cards for ( int index=0; index < numofplayers; index++ ) { if ( numcards == 0 ) { strcpy(players[index],deck[index]); } else { strcat(players[index]," "); strcat(players[index],deck[index+numofplayers]); // printf("IN card 2=%d %d %s\n",numcards,index,deck[index+numofplayers]); } } } } else if ( random_play_flag == 'N' || random_play_flag =='n' ) { // cout << "No random play" ; for ( int l=0; l<numofplayers;l++ ) { printf("\n\nEnter Hand for Player %d = \n\n",l+1); cout << "Enter Card 1:\n"; cin >> players[l]; cout << "Enter Card 2:\n"; strcat(players[l]," "); cin >> deck[0]; strcat(players[l],deck[0]); } } //Dealer //Burn a card - start with (numplayers*2)+1 //Deal 3 cards strcpy(dealer[0],deck[(numofplayers*2)+1]); strcat(dealer[0]," "); strcat(dealer[0],deck[(numofplayers*2)+2]); strcat(dealer[0]," "); strcat(dealer[0],deck[(numofplayers*2)+3]); //1 dead card //deal card strcat(dealer[0]," "); strcat(dealer[0],deck[(numofplayers*2)+5]); //1 dead card //deal card strcat(dealer[0]," "); strcat(dealer[0],deck[(numofplayers*2)+7]); for ( int z=0; z<numofplayers;z++ ) { printf("\n\nPlayer %d Hand\n", z+1); printf("%s\n\n",players[z]); } printf("\n\nDEALERS HAND=%s\n\n",dealer[0]); //Evaluate Hand for a flush for ( int tt=0;tt<numofplayers;tt++ ) { int diamondflush=0; int spadesflush=0; int clubflush=0; int heartflush=0; strcpy(complete_hand[tt],dealer[0]); strcat(complete_hand[tt]," "); strcat(complete_hand[tt],players[tt]); printf("COMPLETE HAND FOR PLAYER %d = ",tt+1); for ( int u=0;u<21;u++ ) { if ( complete_hand[tt][u] == 'D' ) printf("%c",char(5)); else if ( complete_hand[tt][u] == 'S' ) printf("%c",char(4)); else if ( complete_hand[tt][u] == 'C' ) printf("%c",char(3)); else if ( complete_hand[tt][u] == 'H' ) printf("%c",char(6)); else if ( complete_hand[tt][u] == 'T' ) printf("%s","10"); else printf("%c",complete_hand[tt][u]); } printf("\n"); //printf("COMPLETE HAND FOR PLAYER %d = %s\n",tt+1, complete_hand[tt]); //printf("%c\n",complete_hand[0][1]); for ( int r=0; r<21;r=r+3 ) { if ( complete_hand[tt][r] == 'D' ) diamondflush++; if ( complete_hand[tt][r] == 'S' ) spadesflush++; if ( complete_hand[tt][r] == 'C' ) clubflush++; if ( complete_hand[tt][r] == 'H' ) heartflush++; //printf("%c\n",char(5)); } if ( diamondflush >=5 ) printf("You got a Flush\n"); if ( spadesflush >=5 ) printf("You got a Flush\n"); if ( clubflush >=5 ) printf("You got a Flush\n"); if ( heartflush >=5 ) printf("You got a Flush\n"); int twos=0; int threes=0; int fours=0; int fives=0; int six=0; int seven=0; int eight=0; int nine=0; int ten=0; int ace=0; int king=0; int queen=0; int jack=0; char three_cards='N'; char two_cards='N'; for ( int rr=1; rr<21;rr=rr+3 ) { if ( complete_hand[tt][rr] == '2' ) twos++; if ( complete_hand[tt][rr] == '3' ) threes++; if ( complete_hand[tt][rr] == '4' ) fours++; if ( complete_hand[tt][rr] == '5' ) fives++; if ( complete_hand[tt][rr] == '6' ) six++; if ( complete_hand[tt][rr] == '7' ) seven++; if ( complete_hand[tt][rr] == '8' ) eight++; if ( complete_hand[tt][rr] == '9' ) nine++; if ( complete_hand[tt][rr] == 'T' ) ten++; if ( complete_hand[tt][rr] == 'A' ) ace++; if ( complete_hand[tt][rr] == 'K' ) king++; if ( complete_hand[tt][rr] == 'Q' ) queen++; if ( complete_hand[tt][rr] == 'J' ) jack++; //printf("number=%c\n",complete_hand[tt][rr]); } if ( twos==4 ) printf("Four of a Kind 2\n"); else if ( twos==3 ) { printf("Three of a Kind 2\n"); three_cards='Y'; } else if ( twos==2 ) { printf("Pairs 2\n"); two_cards='Y'; } if ( threes==4 ) printf("Four of a Kind 3\n"); else if ( threes==3 ) { printf("Three of a Kind 3\n"); three_cards='Y'; } else if ( threes==2 ) { printf("Pairs 3\n"); two_cards='Y'; } if ( fours==4 ) printf("Four of a Kind 4\n"); else if ( fours==3 ) { printf("Three of a Kind 4\n"); three_cards='Y'; } else if ( fours==2 ) { printf("Pairs 4\n"); two_cards='Y'; } if ( fives==4 ) printf("Four of a Kind 5\n"); else if ( fives==3 ) { printf("Three of a Kind 5\n"); three_cards='Y'; } else if ( fives==2 ) { printf("Pairs 5\n"); two_cards='Y'; } if ( six==4 ) printf("Four of a Kind 6\n"); else if ( six==3 ) { printf("Three of a Kind 6\n"); three_cards='Y'; } else if ( six==2 ) { printf("Pairs 6\n"); two_cards='Y'; } if ( seven==4 ) printf("Four of a Kind 7\n"); else if ( seven==3 ) { printf("Three of a Kind 7\n"); three_cards='Y'; } else if ( seven==2 ) { printf("Pairs 7\n"); two_cards='Y'; } if ( eight==4 ) printf("Four of a Kind 8\n"); else if ( eight==3 ) { printf("Three of a Kind 8\n"); three_cards='Y'; } else if ( eight==2 ) { printf("Pairs 8\n"); two_cards='Y'; } if ( nine==4 ) printf("Four of a Kind 9\n"); else if ( nine==3 ) { printf("Three of a Kind 9\n"); three_cards='Y'; } else if ( nine==2 ) { printf("Pairs 9\n"); two_cards='Y'; } if ( ten==4 ) printf("Four of a Kind 10\n"); else if ( ten==3 ) { printf("Three of a Kind 10\n"); three_cards='Y'; } else if ( ten==2 ) { printf("Pairs 10\n"); two_cards='Y'; } if ( ace==4 ) printf("Four of a Kind A\n"); else if ( ace==3 ) { printf("Three of a Kind A\n"); three_cards='Y'; } else if ( ace==2 ) { printf("Pairs A\n"); two_cards='Y'; } if ( king==4 ) printf("Four of a Kind K\n"); else if ( king==3 ) { printf("Three of a Kind K\n"); three_cards='Y'; } else if ( king==2 ) { printf("Pairs Y\n"); two_cards='Y'; } if ( queen==4 ) printf("Four of a Kind Q\n"); else if ( queen==3 ) { printf("Three of a Kind Q\n"); three_cards='Y'; } else if ( queen==2 ) { printf("Pairs Q\n"); two_cards='Y'; } if ( jack==4 ) printf("Four of a Kind J\n"); else if ( jack==3 ) { printf("Three of a Kind J\n"); three_cards='Y'; } else if ( jack==2 ) { printf("Pairs J\n"); two_cards='Y'; } if ( three_cards =='Y' && two_cards == 'Y' ) printf("Full House\n"); } cout << "Play again? "; cin >>play_again; if ( play_again == 'Y' || play_again =='y' ) { system("CLS"); goto play_again_label; } return 0; }//end main
•
•
Join Date: Aug 2005
Posts: 3
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Aug 2005
Posts: 3
Reputation:
Solved Threads: 0
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.
<< moderator edit: added code tags: [code][/code] >>
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <iostream.h> #include <string.h> #include <stdlib.h> #include <time.h> #include <conio.h> const int maxplayers=100; //================================================== main int main() { char num_players[3]; int numofplayers=0; char random_play_flag; char play_again; //char CARD_FACES[13] = {'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'}; //char CARD_SUITS[4] = {'S', 'H', 'C', 'D'}; char cards[52][25] = { "SA", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9","ST", "SJ", "SQ","SK", "HA", "H2", "H3", "H4", "H5", "H6", "H7" , "H8", "H9", "HT", "HJ", "HQ","HK", "CA", "C2", "C3", "C4", "C5", "C6", "C7" , "C8", "C9", "CT", "CJ", "CQ","CK", "DA", "D2", "D3", "D4", "D5", "D6", "D7" , "D8", "D9", "DT", "DJ", "DQ","DK" }; char players[52][25]; char deck[52][25]; char dealer[52][25]; char complete_hand[52][25]; /* srand(time(0)); // Initializes random 'seed'.*/ play_again_label: cout << "Enter Number of Players in Game: "; cin >>num_players; int slen = strlen(num_players); if (slen ==0 || slen >3){ printf("Try Again\n"); goto play_again_label; } for (int a=0;a<slen;a++){ if (num_players[a] < '0' || num_players[a] > '9') { printf("Try Again\n"); goto play_again_label; } numofplayers = atoi(num_players); } if (numofplayers < 1 || numofplayers >20){ printf("Try Again. With a number less than 20\n"); goto play_again_label; } cout << "Do you want Random Play (Y/N)"; cin >> random_play_flag ;
•
•
Join Date: Jul 2005
Posts: 164
Reputation:
Solved Threads: 5
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.
•
•
Join Date: Jun 2004
Posts: 173
Reputation:
Solved Threads: 9
•
•
•
•
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.
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.
![]() |
Similar Threads
- Diverting inputs (Java)
- diverting keyboard inputs (Java)
Other Threads in the C Forum
- Previous Thread: convert int back to char
- Next Thread: can anyone please brief me on argc and argv in C.
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o include incrementoperators input interest kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf open opensource owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling segmentationfault send sequential shape socket socketprograming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h





