#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0

    Question An activity that i cant solve


    This code runs, but i can't have the second switch statement to work and i can't input anything for player 2...this is a rock-paper-scissor program using switch/case





    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<iostream>
    #include<string.h>
    main()
    {
    char x,y;
    printf("This is a Rock-Paper-Scissor game\nPlease enter \n[R] for Rock\n[P] for Paper\n[S] for Scissor");
    printf("\nEnter letter for Player 1: ");
    scanf("%c",&x);
    printf("\nEnter letter for Player 2: ");
    scanf("%c",&y);
    switch(x)
    {

    case 'r':case 'R':
    switch(y)
    {
    case 'r':case 'R':
    printf("Draw");break;
    case 'p':case 'P':
    printf("Player 2 wins");break;
    case 's':case 'S':
    printf("Player 1 wins");break;
    default:
    printf("Invalid input");break;
    }break;
    case 'p':case 'P':
    switch(y)
    {
    case 'p':case 'P':
    printf("Draw");break;
    case 's':case 'S':
    printf("Player 2 wins");break;
    case 'r':case 'R':
    printf("Player 1 wins");break;
    default:
    printf("Invalid input");break;
    }break;
    case 's':case 'S':
    switch(y)
    {
    case 's':case 'S':
    printf("Draw");break;
    case 'r':case 'R':
    printf("Player 2 wins");break;
    case 'p':case 'P':
    printf("Player 1 wins");break;
    default:
    printf("Invalid input");break;
    }break;
    default:
    printf("INVALID INPUT");
    }
    getch();
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,175
    Rep Power
    2222
    [QUOTE=archael07]Rule One: Use code tags! If you don't, which you didn't, then your code loses all its formatting and becomes an unreadable mess. If we can't read it, we won't bother and you won't get any help.

    Here's what your code looks like when you use code tags:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<iostream>
    #include<string.h>
    main()
    {
          char x,y;
          printf("This is a Rock-Paper-Scissor game\nPlease enter \n[R] for Rock\n[P] for Paper\n[S] for Scissor");
          printf("\nEnter letter for Player 1: ");
          scanf("%c",&x);
          printf("\nEnter letter for Player 2: ");
          scanf("%c",&y);
          switch(x)
              {
                              
                        case 'r':case 'R': 
                                 switch(y)
                                 {
                                 case 'r':case 'R':
                                 printf("Draw");break;                     
                                 case 'p':case 'P':
                                 printf("Player 2 wins");break;
                                 case 's':case 'S':
                                 printf("Player 1 wins");break;
                                 default:
                                 printf("Invalid input");break;
                                 }break;
                        case 'p':case 'P':        
                        switch(y)
                                 {
                                 case 'p':case 'P':
                                 printf("Draw");break;                     
                                 case 's':case 'S':
                                 printf("Player 2 wins");break;
                                 case 'r':case 'R':
                                 printf("Player 1 wins");break;
                                 default:
                                 printf("Invalid input");break;
                                 }break;
                        case 's':case 'S':
                                 switch(y)
                                 {
                                 case 's':case 'S':
                                 printf("Draw");break;                     
                                 case 'r':case 'R':
                                 printf("Player 2 wins");break;
                                 case 'p':case 'P':
                                 printf("Player 1 wins");break;
                                 default:
                                 printf("Invalid input");break;
                                 }break;      
                        default:
                                 printf("INVALID INPUT");              
              }
              getch();
    }
    Rule Two: Use proper indenting! I've seen a lot worse, but your style still makes your code hard to read, what with that excessive indenting where it's not needed and lack of indenting where it is needed. And hiding close-braces is never a good idea.

    Though I didn't have to try to read your code to spot the problem:
    scanf("%c",&y);
    I'm sure there must be an entry for this in the "commonly asked questions" thread, but the problem is that you are telling scanf to read in the next character no matter what kind of character it is. That means that the ENTER placed in the input buffer by the previous scanf is what got placed into y.

    ENTER is one of several characters that are called white space, some of the others being space and tab. What you want the program to do is to read in the next non-white-space character, meaning that you want scanf to ignore all intervening white space. Here is how you do that:
    scanf(" %c",&y);
    Please note the space added before the %. That is what tells scanf to ignore all white space that precedes the character that you actually want it to read.

    I can't speak for any of the rest of your barely readable code, except for the main function header. By not declaring what datatype it returns, you're forcing it to default to int, but then you completely fail to return that int that you had promised it. Keep your promises! Declaring it as void isn't even right, even though you can get away with doing it wrong.

    Declare main as:
    int main()
    and make the last line in the main() function:
    return 0;
    That tells the operating system that your program ran successfully. If it had not for some reason (eg, wrong number of arguments passed in, unable to open a file, division by zero attempted), then you would return a non-zero value to indicate failure. Other programs can make use of that information, as can shell scripts, so returning it is a requirement for all well-behaved programs.

    Comments on this post

    • archael07 agrees
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0
    THANKS!!! the space before the % works!!

    though my bad for the format i was half-asleep when i'm doing that hahha and i did not know about the code tags
    here is what it looks like know, and working!!!

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<iostream>
    #include<string.h>
    int main()
    {
          char x,y;
          printf("This is a Rock-Paper-Scissor game\nPlease enter \n[R] for Rock\n[P] for Paper\n[S] for Scissor");
          printf("\nEnter letter for Player 1: ");
          scanf("%c",&x);
          printf("\nEnter letter for Player 2: ");
          scanf(" %c",&y);
          switch(x)
              {                          
                        case 'r':case 'R': 
                                 switch(y)
                                 {
                                 case 'r':case 'R':
                                 printf("\nDraw");break;                     
                                 case 'p':case 'P':
                                 printf("\nPlayer 2 wins");break;
                                 case 's':case 'S':
                                 printf("\nPlayer 1 wins");break;
                                 default:
                                 printf("\nInvalid input");break;
                                 }
                                 break;
                        case 'p':case 'P':        
                        switch(y)
                                 {
                                 case 'p':case 'P':
                                 printf("\nDraw");break;                     
                                 case 's':case 'S':
                                 printf("\nPlayer 2 wins");break;
                                 case 'r':case 'R':
                                 printf("\nPlayer 1 wins");break;
                                 default:
                                 printf("\nInvalid input");break;
                                 }
                                 break;
                        case 's':case 'S':
                                 switch(y)
                                 {
                                 case 's':case 'S':
                                 printf("\nDraw");break;                     
                                 case 'r':case 'R':
                                 printf("\nPlayer 2 wins");break;
                                 case 'p':case 'P':
                                 printf("\nPlayer 1 wins");break;
                                 default:
                                 printf("\nInvalid input");break;
                                 }
                                 break;      
                        default:
                                 printf("\nINVALID INPUT");              
              }
              getch();
              return 0;
    }
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,113
    Rep Power
    1803
    Your problem can be solved thus:

    Code:
    char input_char()
    {
        char x = getchar();
        while( x != '\n' && getchar() != '\n' )
        {
            // empty - flushing the line input
        }
        return x ;
    }
    and then replace your scanf() calls with input_char() calls.

    There is also a much simpler algorithm to determine the winner:

    Given a function to convert an input of r, p or s to 0,1 or 2 respectively, for example thus:
    Code:
    int get_rps_index( char ch )
    {
        const char* rps = "rps" ;
        char* p = strchr( rps, tolower(ch) ) ;
        return p != 0 ? p - rps : -1 ;
    }
    You can then determine the winner thus:

    Code:
    int xi = get_rps_index( x ) ;
    int yi = get_rps_index( y ) ;
    
    if( yi > 0 && yi > 0 )
    {
        if( yi > xi )
        {
            yi += 3 ;
        }
    
        switch( yi - xi )
        {
            case 0:
                // Draw
            break ;
    
            case 1:
                // Player 2 (y) wins
            break ;
    
            case 2:
                // Player 1 (x) wins
            break ;
        }
    }
    else
    {
        // invalid input
    }
    Last edited by clifford; November 28th, 2012 at 04:32 PM.
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,113
    Rep Power
    1803
    Originally Posted by archael07
    THANKS!!! the space before the % works!!
    Only until the user mis-keys and enters: r#<enter> or similar - the program will not wait for player two input which will take the # and be invalid input. Equally if you typed rp<enter>, it would not wait for new input but this time player 2 will win.

    You might find that input method acceptable, but it would normally be better to ensure that the program always stops and waits for input for player 2 and disregards any input after the first character entered - the method I suggested does that.
    Last edited by clifford; November 28th, 2012 at 04:29 PM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0
    yours is way cooler than mine!! unfortunately we are not that far in the lesson in school yet, so am a little blurry on what you said or how to do what you said...if you would be so kind as to explain further?? esp the one where it will only accept one input as required. We are going to create a program where user must input 1 character only,,have tried arrays but am messing up the code..hehehe MANY THANKS!!!
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,175
    Rep Power
    2222
    Rather, Clifford made you aware of a far greater problem and concern. An older, more curmudgeonly member, DaWei, would put it thus: "I could easily make your program run out to the weeds and puke all over its shoes."

    Right now you are learning the bare essentials of how to get input from the user and to process it and to provide output. Simple tasks, extremely simple tasks. Not really what you will need to do in the real world.

    In the real world, your software needs to be able to handle any kind of input from the user and to keep running reliably no matter what happens. That's called being "robust". That involves input validation. That means that your program should be expected to receive any kind of input from the user, validate it, and if it is not correct to either ask the user to input it correctly or allow him to exit gracefully.

    Clifford was trying to introduce you to that extremely important idea. Your first professional project outside of school will be totally unlike what you had been doing in school. School is only getting you ready to learn what you will really be doing. Don't let this discourage you! It is fun and exciting and there's so much more to learn!

IMN logo majestic logo threadwatch logo seochat tools logo