0 Members and 3 Guests are viewing this topic.
#include <stdio.h>#include <stdlib.h>#include <time.h>#include <string.h>int main();int main() { char options[][8] = {"rocks", "papers", "scissors"}; //Possible options srand( time(NULL) ); //For random integer /* Define variables needed */ int user_choice_index; char user_choice[8]; int cpu_choice_index; char cpu_choice[8]; int i; while (1) { /* Get user choice and index */ fgets(user_choice, sizeof user_choice, stdin); for (i=0; i<3; i++) { if ( user_choice == options[i] ) { user_choice_index = i; //Index of user's choice break; } } /* Get computer choice */ cpu_choice_index = ( rand() %3 ); //Random number [0,3] cpu_choice = options[cpu_choice_index]; /* Check who wins */ if ( strcmp(cpu_choice, user_choice) == 0 ) { printf("Tie\n"); } else if ( (cpu_choice_index + 1) % 3 == user_choice_index ) { printf("You won"); } else { printf("You lose"); } } return 0;}
rock_papers_scissors.c:33:16: error: incompatible types when assigning to type ‘char[8]’ from type ‘char *’
cpu_choice = options[cpu_choice_index];
char options[][8] = {"rocks", "papers", "scissors"}; //Possible options
#include <stdio.h>#include <stdlib.h>#include <time.h>#include <string.h>int main();int main() { char options[][8] = {"rocks", "paper", "scissors"}; //Possible options srand( time(NULL) ); //For random integer /* Define variables needed */ int user_choice_index; char user_choice[8]; int cpu_choice_index; char cpu_choice[8]; int i; while (1) { /* Get user choice and index */ fgets(user_choice, sizeof user_choice, stdin); for (i=0; i<3; i++) { if ( user_choice == options[i] ) { user_choice_index = i; //Index of user's choice break; } } /* Get computer choice */ cpu_choice_index = ( rand() %3 ); //Random number [0,3] strcpy(cpu_choice, options[cpu_choice_index]); printf("%s", cpu_choice); /* Check who wins */ if ( strcmp(cpu_choice, user_choice) == 0 ) { printf("Tie\n"); } else if ( (cpu_choice_index + 1) % 3 == user_choice_index ) { printf("You won\n"); } else { printf("You lose\n"); } } return 0;}
scissorsscissors�@You wonrocksYou lose
*cpu_choice = options[cpu_choice_index];
ChangeCode: [Select]cpu_choice = options[cpu_choice_index];to Code: [Select]*cpu_choice = options[cpu_choice_index];
scissors�@You won
char options[][9] = {"rocks", "paper", "scissors"}; //Possible options srand( time(NULL) ); //For random integer /* Define variables needed */ int user_choice_index; char user_choice[9]; int cpu_choice_index; char cpu_choice[9];
By making the char array able to hold 9 chars to make the string 0 terminated.
#include <stdio.h>#include <stdlib.h>#include <time.h>#include <string.h>int main();int main() { char options[][9] = {"rocks", "paper", "scissors"}; //Possible options srand( time(NULL) ); //For random integer /* Define variables needed */ int user_choice_index; char user_choice[9]; int cpu_choice_index; char cpu_choice[9]; int i; while (1) { /* Get user choice and index */ fgets(user_choice, sizeof user_choice, stdin); for (i=0; i<3; i++) { if ( user_choice == options[i] ) { user_choice_index = i; //Index of user's choice break; } } /* Get computer choice */ cpu_choice_index = ( rand() %3 ); //Random number [0,3] strcpy(cpu_choice, options[cpu_choice_index]); printf("%s\n", cpu_choice); /* Check who wins */ if ( strcmp(cpu_choice, user_choice) == 0 ) { printf("Tie\n"); continue; } else if ( (cpu_choice_index + 1) % 3 == user_choice_index ) { printf("You won\n"); continue; } else { printf("You lose\n"); continue; } } return 0;}
#include <stdio.h>#include <stdlib.h>#include <time.h>#include <string.h>int main();int main() { char options[][9] = {"rocks", "paper", "scissors"}; //Possible options srand( time(NULL) ); //For random integer /* Define variables needed */ int user_choice_index; char user_choice[9]; int cpu_choice_index; int i; while (1) { /* Get user choice and index */ fgets(user_choice, sizeof user_choice, stdin); for (i=0; i<3; i++) { if ( strcmp(options[i], user_choice) == 0 ) { user_choice_index = i; //Index of user's choice break; } } /* Get computer choice */ cpu_choice_index = ( rand() %3 ); //Random number [0,3] printf("%s\n", options[cpu_choice_index]); /* Check who wins */ if ( user_choice_index == cpu_choice_index ) { printf("Tie\n"); } else if ( (cpu_choice_index + 1) % 3 == user_choice_index ) { printf("You won\n"); } else { printf("You lose\n"); } } return 0;}
scissorsscissorsTierocksYou lose
"A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str."That might be your problem. If it is, you'll need to make user_choice a 10-byte array: 8 for printing characters, one for the newline, and another for the null. That'll throw of your comparisons with strcmp, though, so you could also add a newline to each element of options. Then the printf("%s\n"... line won't need that \n.
Ashbad: it's the same instance if and only if it's at the same address, as far as I can tell. Note that small integers actually do act like C ints, so 5 is 5 returns True, just like 5 == 5 in C returns whatever's C's value for true is.
class A: passprint repr(A())