Thread: Help With Rock Paper Scissors Game

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    21

    Help With Rock Paper Scissors Game

    I'm trying to create a rock paper scissors game and I having problems having one of the strings of array to appear.
    Any help is appreciated. When you hit option 2 it should atleast printf the word rock, paper, or scissors.
    Here's My Code

    Code:
    #include <stdlib.h>
    #include <time.h>
    
    
    int main(){
    char * array [3] = {"Rock", "Paper", "Scissors"};
    int randNum;
    int choice;
      srand((unsigned)(time(0))); 
    printf ("Welcome to Rock Paper Scissors\n");
    printf("Menu\n1 = Play Rock Paper Scissors\n2 = Quit\n");
    printf("Your Choice: ");
    scanf ("%d", &choice);
    switch(choice){
    case 1:
    randNum = rand() % 3;
    *array[randNum];
    printf("\n%s", array);
    
    
    system ("pause");
    case 2:
    break;
    }
    system ("Pause");
    }
    Last edited by anonymoususer; 11-30-2011 at 05:17 PM.

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Why would you expect case 2 to do anything when you immediately break from it?
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    21
    Im thinking of adding more options. Case 1 is what I what you to look at.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anonymoususer View Post
    I'm trying to create a rock paper scissors game and I having problems having one of the strings of array to appear.
    Referring to the code in message #1 ...

    Line 6 ... lose the *
    line 9 ... there's no need for the typecast.
    line 17 ... does nothing, delete it.
    line 18 ... should be
    Code:
    printf("\n%s", array[randnum]);
    line 21 ... it is massively annoying for operators to have to keep pressing enter to continue... ditch the pauses.

    line 26 ... main() returns an int... so return an int!
    Code:
    return 0;
    }
    Overall... your code indentation needs work... there's no excuse for lazy formatting, it just makes your code harder to read.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    21
    Ok
    I changed it based on what you said and I have the following errors
    Code:
    #include <stdlib.h>
    #include <time.h>
    
    
    int main(){
    char array [3] = {"Rock", "Paper", "Scissors"};
    int randNum;
    int choice;
      srand((unsigned)(time(0))); 
    printf ("Welcome to Rock Paper Scissors\n");
    printf("Menu\n1 = Play Rock Paper Scissors\n2 = Quit\n");
    printf("Your Choice: ");
    scanf ("%d", &choice);
    switch(choice){
    case 1:
    randNum= rand() % 3;
    
    
    printf("\n%s", array[randNum]);
    
    
    case 2:
    break;
    }
    return 0;
    system ("Pause");
    }
    
    Errors
    ImageShack® - Online Photo and Video Hosting
    Last edited by anonymoususer; 11-30-2011 at 09:12 PM.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Code:
    array = rand() % 3;
    The above line is certainly an error. You're attempting to change a pointer constant's value.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Tclausex
    The above line is certainly an error. You're attempting to change a pointer constant's value.
    Good catch, but more accurately, you are trying to assign to an array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    21
    You must of read it before I redited post #5 because i'm not assigning it to an array its being assigned to randNum and then Im passing randNum through the array. And the program stops running after this line of code
    Code:
    char array [3] = {"Rock", "Paper", "Scissors"};
    Here's the full code again
    Code:
    #include <stdlib.h>
    #include <time.h>
    
    
    int main(){
    char array [3] = {"Rock", "Paper", "Scissors"};
    int randNum;
    int choice;
      srand((unsigned)(time(0))); 
    printf ("Welcome to Rock Paper Scissors\n");
    printf("Menu\n1 = Play Rock Paper Scissors\n2 = Quit\n");
    printf("Your Choice: ");
    scanf ("%d", &choice);
    switch(choice){
    case 1:
    randNum = rand() % 3;
    
    
    printf("\n%s", array[randNum]);
    
    
    case 2:
    break;
    }
    return 0;
    system ("Pause");
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by anonymoususer
    And the program stops running after this line of code
    Code:
    char array [3] = {"Rock", "Paper", "Scissors"};
    You should be getting a warning from your compiler about that hinting what is the problem. Look carefully at the type of the array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    21
    I'm not at my computer at the moment so I can't test it but are you saying I shouldnt use char for the array? I don't think you can say "string array" what should it be if that's what your referring to.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anonymoususer View Post
    I'm not at my computer at the moment so I can't test it but are you saying I shouldnt use char for the array? I don't think you can say "string array" what should it be if that's what your referring to.
    I think I may have forgotten something in my earlier suggestions...
    Code:
    char array[3][] = ...
    Even though it's storing 3 strings, it is a two dimensional array.

  12. #12
    Registered User
    Join Date
    Oct 2011
    Posts
    21
    I got it working thanks!!
    Last edited by anonymoususer; 12-01-2011 at 12:10 AM.

  13. #13
    Registered User
    Join Date
    Oct 2011
    Posts
    21
    I do have one other problem. Im trying to put my code in a function but I'm getting errors
    Code:
    #include <stdlib.h>
    #include <time.h>
    
    
    
    
        char array[3][20] = {"Rock", "Paper", "Scissors"};
    int randNum;
    int randNum1;
    int i;
    int choice;
    int playAgain;
    int option;
    
    
    void game(char array[int randNum],char array[int randNum1]);
     
    int main(){
    
    
      srand((unsigned)(time(0))); 
    printf ("Welcome to Rock Paper Scissors\n");
    system ("pause");
    
    
    
    
    
    
    if(option == '1')  
    game(array[randNum],array[randNum1]);
    if(option == '2')
    printf("Goodbye");
    else {
    printf("\t\tInvalid Entry\n\t\tPlease Try Again");
    }
              
    
    
    
    
    return 0;
    system ("Pause");
    }
    
    
    void game(array[randNum],array[randNum1]){
         randNum = rand() % 3;
    randNum1 = rand() % 3;
    
    
    printf("\n%s %s %s Shoot:\n\n", array[0], array[1], array[2]);
     
     
    printf("\n\t\tAndy: %s\n", array[randNum]);
    printf("\t\tSpencer: %s\n\n\n", array[randNum1]);
    do{
    
    
    printf("Do you want to play again?\n1 = Yes\n2 = No\n    Your Choice: ");
    scanf("%d", &playAgain);
    
    
    if (playAgain != '1' && playAgain != '2')
            printf("Invalid Entry. Please Try Again.\n\n\t");
            system ("pause");
            
              
         }
    Errors:

    13 C:\Users\Andrew\Desktop\Untitled1.c syntax error before "int"
    37 C:\Users\Andrew\Desktop\Untitled1.c syntax error before '[' token
    37 C:\Users\Andrew\Desktop\Untitled1.c conflicting types for 'game'
    13 C:\Users\Andrew\Desktop\Untitled1.c previous declaration of 'game' was here
    37 C:\Users\Andrew\Desktop\Untitled1.c conflicting types for 'game'
    13 C:\Users\Andrew\Desktop\Untitled1.c previous declaration of 'game' was here
    C:\Users\Andrew\Desktop\Untitled1.c In function `game':
    56 C:\Users\Andrew\Desktop\Untitled1.c syntax error at end of input
    Last edited by anonymoususer; 12-01-2011 at 01:00 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rock, paper, scissors game
    By mrsmoss3791 in forum C++ Programming
    Replies: 12
    Last Post: 03-20-2011, 11:53 PM
  2. Replies: 1
    Last Post: 10-08-2010, 12:05 AM
  3. Replies: 8
    Last Post: 10-24-2007, 05:46 PM
  4. Rock Paper Scissors Game
    By tbca in forum C++ Programming
    Replies: 12
    Last Post: 07-09-2007, 12:16 PM
  5. Rock, Paper, Scissors game help
    By MillaTime in forum Game Programming
    Replies: 4
    Last Post: 09-08-2002, 05:55 PM