Join 149,473 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,897 people online right now. Registration is fast and FREE... Join Now!
I have gotten to this point with my Program for Rock Paper, Scissors but when I run it it will not play and only continue to give me options if usable keys for the game. I am stumped. Could someone please point me in the right direction. I have posted the program below:
outcome compare(p_r_s player_chioce, p_r_s machine_choice); void prn_final_status(int win_cnt, int lose_cnt); void prn_game_status(int win_cnt, int lose_cnt, int tie_cnt);
void prn_help(void); void prn_instructions(void); void report(outcome result, int *win_cnt_ptr, int *lose_cnt_ptr, int *tie_cnt_ptr); p_r_s selection_by_machine(void); p_r_s selection_by_player(void);
void prn_instructions(void) { printf("\n%s\n", "PAPER, ROCK, SCISSORS:\n" "\n" "In this game\n" "\n" " p is for \"paper\"\n" " r is for \"rock\"\n" " s is for \"scissors\"\n" "\n" "Both the player and the machine will chose one\n" "of p, r, or s. If the two choices are the same,\n" "then the game is a tie. Otherwise:\n" "\n" " \"paper covers the rock\" (a win for paper)\n" " \"rock breaks the scissors\" (a win for rock)\n" " \"scissors cut the paper\" (a win for scissors)\n" "\n" " g for game status (print number of wins)\n" " h for help (print short instructions)\n" " i for instructions (print these instructions)\n" " q for quit (quit the game)\n" "\n" "This game is played repeatedly until q is entered.\n" "\n" "Good luck!\n"); }
void prn_help(void) { printf("\n%s\n", "The following characters can be used for input:\n" " p for paper\n" " r for rock\n" " s for scissors\n" " g print the game status\n" " h help, print this list\n" " i reprint the instructions\n" " q qiut this game\n"); }
if(player_choice == machine_choice) return tie; switch (player_choice){ case paper: result = (machine_choice == rock) ? win : lose; break; case rock: result = (machine_choice == scissors) ? win : lose; break; case scissors: result = (machine_choice == paper) ? win : lose; break; default: printf("PROGRAMMER ERROR: Unexpected choice!\n\n"); exit(1); } return result; }
void report(outcome result, int *win_cnt_ptr, int *lose_cnt_ptr, int *tie_cnt_ptr)
{ switch (result) { p_r_s player_choice, machine_choice; case win: ++*win_cnt_ptr; if (player_choice == paper) printf("%35sYou chose paper and I chose rock. You win.\n", ""); else if (player_choice == rock) printf("%35sYou chose rock and I chose scissors. You win.\n", ""); else if (player_choice == scissors) printf("%35sYou chose scissors and I chose paper. You win.\n", ""); break; case lose: ++*lose_cnt_ptr; if (player_choice == paper) printf("%35sYou chose paper and I chose scissors. You lose.\n", ""); else if (player_choice == rock) printf("%35sYou chose rock and I chose paper. You lose.\n", ""); else if (player_choice == scissors) printf("%35sYou chose scissors and I chose rock. You lose.\n", ""); break; case tie: ++*tie_cnt_ptr; if (player_choice == paper) printf("%35sYou chose paper and I chose paper. We tie.\n", ""); else if (player_choice == rock) printf("%35sYou chose rock and I chose rock. We tie.\n", ""); else if (player_choice == scissors) printf("%35sYou chose scissors and I chose scissors. We tie.\n", ""); break; default: printf("PROGRAMMER ERROR: Unexpected result!\n\n"); exit(1);
OK, for whatever the reason, the computer is reading in the linefeed character when you hit enter.
you can use the getc() function and pass stdin as the argument. char c = getc(stdin);
You might have to use getc() twice to move past the newline character.
In am really a begginer so when I get to my text I will look up your suggestion. Unfortunately this text is horrible an explains very little or I would have an easier time. If you have any further suggestions I would greatly appreciate it.
I have gotten to this point with my Program for Rock Paper, Scissors but when I run it it will not play and only continue to give me options if usable keys for the game. I am stumped. Could someone please point me in the right direction. I have posted the program below:
outcome compare(p_r_s player_chioce, p_r_s machine_choice); void prn_final_status(int win_cnt, int lose_cnt); void prn_game_status(int win_cnt, int lose_cnt, int tie_cnt);
void prn_help(void); void prn_instructions(void); void report(outcome result, int *win_cnt_ptr, int *lose_cnt_ptr, int *tie_cnt_ptr); p_r_s selection_by_machine(void); p_r_s selection_by_player(void);
void prn_instructions(void) { printf("\n%s\n", "PAPER, ROCK, SCISSORS:\n" "\n" "In this game\n" "\n" " p is for \"paper\"\n" " r is for \"rock\"\n" " s is for \"scissors\"\n" "\n" "Both the player and the machine will chose one\n" "of p, r, or s. If the two choices are the same,\n" "then the game is a tie. Otherwise:\n" "\n" " \"paper covers the rock\" (a win for paper)\n" " \"rock breaks the scissors\" (a win for rock)\n" " \"scissors cut the paper\" (a win for scissors)\n" "\n" " g for game status (print number of wins)\n" " h for help (print short instructions)\n" " i for instructions (print these instructions)\n" " q for quit (quit the game)\n" "\n" "This game is played repeatedly until q is entered.\n" "\n" "Good luck!\n"); }
void prn_help(void) { printf("\n%s\n", "The following characters can be used for input:\n" " p for paper\n" " r for rock\n" " s for scissors\n" " g print the game status\n" " h help, print this list\n" " i reprint the instructions\n" " q qiut this game\n"); }
if(player_choice == machine_choice) return tie; switch (player_choice){ case paper: result = (machine_choice == rock) ? win : lose; break; case rock: result = (machine_choice == scissors) ? win : lose; break; case scissors: result = (machine_choice == paper) ? win : lose; break; default: printf("PROGRAMMER ERROR: Unexpected choice!\n\n"); exit(1); } return result; }
void report(outcome result, int *win_cnt_ptr, int *lose_cnt_ptr, int *tie_cnt_ptr)
{ switch (result) { p_r_s player_choice, machine_choice; case win: ++*win_cnt_ptr; if (player_choice == paper) printf("%35sYou chose paper and I chose rock. You win.\n", ""); else if (player_choice == rock) printf("%35sYou chose rock and I chose scissors. You win.\n", ""); else if (player_choice == scissors) printf("%35sYou chose scissors and I chose paper. You win.\n", ""); break; case lose: ++*lose_cnt_ptr; if (player_choice == paper) printf("%35sYou chose paper and I chose scissors. You lose.\n", ""); else if (player_choice == rock) printf("%35sYou chose rock and I chose paper. You lose.\n", ""); else if (player_choice == scissors) printf("%35sYou chose scissors and I chose rock. You lose.\n", ""); break; case tie: ++*tie_cnt_ptr; if (player_choice == paper) printf("%35sYou chose paper and I chose paper. We tie.\n", ""); else if (player_choice == rock) printf("%35sYou chose rock and I chose rock. We tie.\n", ""); else if (player_choice == scissors) printf("%35sYou chose scissors and I chose scissors. We tie.\n", ""); break; default: printf("PROGRAMMER ERROR: Unexpected result!\n\n"); exit(1);
} }
Thank you for any guidance you can throw my way.
*mod edit - added code tags
To the best of my knowledge here is the corrected program it works well. I commented some of the major changes. For the rest, compare the two programs.
/* u misspelled choice in player_choice for the */ /* outcome function*/
outcome compare(p_r_s player_choice, p_r_s machine_choice); void report(outcome result, p_r_s player_choice, int *win_cnt_ptr, int *lose_cnt_ptr, int *tie_cnt_ptr); void prn_help(void); void prn_final_status(int win_cnt, int lose_cnt); void prn_game_status(int win_cnt, int lose_cnt, int tie_cnt);
int main(void) { int win_cnt = 0, lose_cnt = 0, tie_cnt = 0; outcome result; p_r_s player_choice, machine_choice;
srand(time(NULL)); prn_instructions();
do { player_choice = selection_by_player(); machine_choice = selection_by_machine();
switch (player_choice){ case paper: case rock: case scissors: result = compare(player_choice, machine_choice); report(result, player_choice, &win_cnt, &lose_cnt, &tie_cnt); break; case game: prn_game_status(win_cnt, lose_cnt, tie_cnt); break; case instructions: prn_instructions(); break; case help: prn_help(); break; case quit: break; default: printf("PROGRAMMER ERROR: Cannot get to here!\n\n"); exit(1); }
} while (player_choice != quit);
prn_game_status(win_cnt, lose_cnt, tie_cnt); prn_final_status(win_cnt, lose_cnt); printf("Type any letter then hit the enter key\n"); scanf("%");
return 0; } void prn_instructions(void) { printf("PAPER, ROCK, SCISSORS:\n" "\n" "In this game\n" "\n" " p is for \"paper\"\n" " r is for \"rock\"\n" " s is for \"scissors\"\n" "\n" "Both the player and the machine will chose one\n" "of p, r, or s. If the two choices are the same,\n" "then the game is a tie. Otherwise:\n" "\n" " \"paper covers the rock\" (a win for paper)\n" " \"rock breaks the scissors\" (a win for rock)\n" " \"scissors cut the paper\" (a win for scissors)\n" "\n" " g for game status (print number of wins)\n" " h for help (print short instructions)\n" " i for instructions (print these instructions)\n" " q for quit (quit the game)\n" "\n" "This game is played repeatedly until q is entered.\n" "\n" "Good luck!\n"); }
/* \t corrects some problems in reading the char c repeated times*/
scanf("\t%c", &c);
switch ( c ) { case 'p': player_choice = paper; break; case 'r': player_choice = rock; break; case 's': player_choice = scissors; break; case 'g': player_choice = game; break; case 'i': player_choice = instructions; break; /* Here u forgot the case for quit */ case 'q': player_choice = quit; break; default: player_choice = help; break; }
return player_choice; }
void prn_final_status(int win_cnt, int lose_cnt) { if(win_cnt > lose_cnt) printf("CONGRATULATIONS - You won!\n\n"); else if(win_cnt == lose_cnt) printf(" A DRAW - You Tied!\n\n"); else printf("SORRY - You lost!\n\n"); }
void prn_help(void) { printf("The following characters can be used for input:\n" " p for paper\n" " r for rock\n" " s for scissors\n" " g print the game status\n" " h help, print this list\n" " i reprint the instructions\n" " q quit this game\n"); }
if(player_choice == machine_choice) result = tie; switch (player_choice){ case paper: result = (machine_choice == rock) ? win : lose; break; case rock: result = (machine_choice == scissors) ? win : lose; break; case scissors: result = (machine_choice == paper) ? win : lose; break; default: printf("PROGRAMMER ERROR: Unexpected choice!\n\n"); exit(1); } return result; }
void report(outcome result, p_r_s player_choice, int *win_cnt_ptr, int *lose_cnt_ptr, int *tie_cnt_ptr)
{ switch (result)
{ /* p_r_s player_choice, machine_choice; */ /* don't redefine player and machine choice they will */ /* reset to garbage*/ case win: ++*win_cnt_ptr; if (player_choice == paper) printf("You chose paper and I chose rock. You win.\n"); else if (player_choice == rock) printf("You chose rock and I chose scissors. You win.\n"); else if (player_choice == scissors) printf("You chose scissors and I chose paper. You win.\n"); break; case lose: ++*lose_cnt_ptr; if (player_choice == paper) printf("You chose paper and I chose scissors. You lose.\n"); else if (player_choice == rock) printf("You chose rock and I chose paper. You lose.\n"); else if (player_choice == scissors) printf("You chose scissors and I chose rock. You lose.\n"); break; case tie: ++*tie_cnt_ptr; if (player_choice == paper) printf("You chose paper and I chose paper. We tie.\n"); else if (player_choice == rock) printf("You chose rock and I chose rock. We tie.\n"); else if (player_choice == scissors) printf("You chose scissors and I chose scissors. We tie.\n"); break; default: printf("PROGRAMMER ERROR: Unexpected result!\n\n"); exit(1); } }
/* you misspelled choice in player_choice for the */ /* outcome function */
outcome compare(p_r_s player_choice, p_r_s machine_choice); void report(outcome result, p_r_s player_choice, int *win_cnt_ptr, int *lose_cnt_ptr, int *tie_cnt_ptr); void prn_help(void); void prn_final_status(int win_cnt, int lose_cnt); void prn_game_status(int win_cnt, int lose_cnt, int tie_cnt);
int main(void) { int win_cnt = 0, lose_cnt = 0, tie_cnt = 0; outcome result; p_r_s player_choice, machine_choice;
srand(time(NULL)); prn_instructions();
/* I did a different loop compare the two*/ do { player_choice = selection_by_player(); machine_choice = selection_by_machine();
switch (player_choice){ case paper: case rock: case scissors: result = compare(player_choice, machine_choice); report(result, player_choice, &win_cnt, &lose_cnt, &tie_cnt); break; case game: prn_game_status(win_cnt, lose_cnt, tie_cnt); break; case instructions: prn_instructions(); break; case help: prn_help(); break; case quit: break; default: printf("PROGRAMMER ERROR: Cannot get to here!\n\n"); exit(1); }
/* the following holds the output if using the gnu compiler
printf("Type any letter then hit the enter key\n"); scanf("%");
return 0; } void prn_instructions(void) { printf("PAPER, ROCK, SCISSORS:\n" "\n" "In this game\n" "\n" " p is for \"paper\"\n" " r is for \"rock\"\n" " s is for \"scissors\"\n" "\n" "Both the player and the machine will chose one\n" "of p, r, or s. If the two choices are the same,\n" "then the game is a tie. Otherwise:\n" "\n" " \"paper covers the rock\" (a win for paper)\n" " \"rock breaks the scissors\" (a win for rock)\n" " \"scissors cut the paper\" (a win for scissors)\n" "\n" " g for game status (print number of wins)\n" " h for help (print short instructions)\n" " i for instructions (print these instructions)\n" " q for quit (quit the game)\n" "\n" "This game is played repeatedly until q is entered.\n" "\n" "Good luck!\n"); }
printf("Input p, r, or s: "); /* \t solves some problems with reading the char c after the */ /* first time in the loop */ scanf("\t%c", &c);
switch ( c ) { case 'p': player_choice = paper; break; case 'r': player_choice = rock; break; case 's': player_choice = scissors; break; case 'g': player_choice = game; break; case 'i': player_choice = instructions; break; /* I think you forgot the case for quit here*/ case 'q': player_choice = quit; break; default: player_choice = help; break; }
return player_choice; }
void prn_final_status(int win_cnt, int lose_cnt) { if(win_cnt > lose_cnt) printf("CONGRATULATIONS - You won!\n\n"); else if(win_cnt == lose_cnt) printf(" A DRAW - You Tied!\n\n"); else printf("SORRY - You lost!\n\n"); }
void prn_help(void) { printf("The following characters can be used for input:\n" " p for paper\n" " r for rock\n" " s for scissors\n" " g print the game status\n" " h help, print this list\n" " i reprint the instructions\n" " q quit this game\n"); }
if(player_choice == machine_choice) result = tie; switch (player_choice){ case paper: result = (machine_choice == rock) ? win : lose; break; case rock: result = (machine_choice == scissors) ? win : lose; break; case scissors: result = (machine_choice == paper) ? win : lose; break; default: printf("PROGRAMMER ERROR: Unexpected choice!\n\n"); exit(1); } return result; }
void report(outcome result, p_r_s player_choice, int *win_cnt_ptr, int *lose_cnt_ptr, int *tie_cnt_ptr)
{
/*no need the redefine player and machine choice here */ /* if you do you will put garbage in those variables.*/ /* you need p_r_s player_choice not just outcome result */ /* in the function. */
switch (result)
{
case win: ++*win_cnt_ptr; if (player_choice == paper) printf("You chose paper and I chose rock. You win.\n"); else if (player_choice == rock) printf("You chose rock and I chose scissors. You win.\n"); else if (player_choice == scissors) printf("You chose scissors and I chose paper. You win.\n"); break; case lose: ++*lose_cnt_ptr; if (player_choice == paper) printf("You chose paper and I chose scissors. You lose.\n"); else if (player_choice == rock) printf("You chose rock and I chose paper. You lose.\n"); else if (player_choice == scissors) printf("You chose scissors and I chose rock. You lose.\n"); break; case tie: ++*tie_cnt_ptr; if (player_choice == paper) printf("You chose paper and I chose paper. We tie.\n"); else if (player_choice == rock) printf("You chose rock and I chose rock. We tie.\n"); else if (player_choice == scissors) printf("You chose scissors and I chose scissors. We tie.\n"); break; default: printf("PROGRAMMER ERROR: Unexpected result!\n\n"); exit(1); } }