-1
#include <stdio.h>
#include <stdlib.h> //for random()
#include <time.h>

void RockPaperScissors(char usrVal);

main(){

char input;
int i;
        for (i = 0; i < 3; ++i){
            printf("\nEnter r, p, or s for rock paper scissors: ");
            input = getchar();
            RockPaperScissors(input);
    }
    printf("\n\n\nTHE GAME IS OVER\n\n");
}




void RockPaperScissors(char usrVal){

    const int rock = 0;
    const int paper = 1;
    const int scissors = 2;

    srand(time(NULL));
    int comVal = rand() % 2 + 0;

    int usrScore = 0;
    int comScore = 0;
    int tie = 0;

    switch (usrVal){
            case 'r':
                    if (comVal == paper)
                            printf("\nYOU LOSE!!!\n");
                            ++comScore;
                            break;
                    if (comVal == scissors)
                            printf("\nYOU WIN!!!\n");
                            ++usrScore;
                            break;
            case 'p':
                    if (comVal == rock)
                            printf("\nYOU WIN!!!\n");
                            ++usrScore;  
                            break;
                    if (comVal == scissors)
                            printf("\nYOU LOSE!!!\n");
                            ++comScore;
                            break;
            case 's':
                     if (comVal == rock)
                            printf("\nYOU LOSE!!!\n");
                            ++comScore;
                            break;
                    if (comVal == scissors)
                            printf("\nYOU WIN!!!\n");
                            ++usrScore;
                            break;
            default:
                    printf("\nTIE\n");
                    ++tie;
                     printf("\ncomVal: %d  usrVal: %c\n", comVal, usrVal);


           }

    printf("\nscore is user: %d   computer: %d  Tie: %d \n\n\n\n", usrScore, comScore, tie);
}


 // ISSUES: need way to store score count.. need to fix rando()..sometimes printf() is skipped in switch statement
  //Need to fix the for loop (it does not ask for input every time)

I have been stuck on this problem for hours. I have looked through multiple forums and have found nothing that really explains my issue. the program will go to one iteration of the loop and then skip user input. but then after that it will ask for an input.


Enter r, p, or s for rock paper scissors: r

score is user: 0 computer: 1 Tie: 0


Enter r, p, or s for rock paper scissors: TIE

comVal: 0 usrVal:

score is user: 0 computer: 0 Tie: 1


Enter r, p, or s for rock paper scissors: r

score is user: 0 computer: 1 Tie: 0

THE GAME IS OVER

jamms69
  • 1
  • 1
  • 6

1 Answers1

0

Make input = getchar(); to scanf("\n%c", &input);. Your problem is un-managed \n (new line).\n is also considered as a character.So the enter you press is used for next getchar().

Also make main() to int main() (main should have some return type usally int) and Improve your code indentation .(don't forget to add return 0; if your are using int main())

Modified code :-

#include <stdio.h>
#include <stdlib.h> //for random()
#include <time.h>

void RockPaperScissors(char usrVal);

int main() // make it int main()
{

        char input;
        int i;
        for (i = 0; i < 3; ++i)
        {
                printf("\nEnter r, p, or s for rock paper scissors: ");
                scanf("\n%c", &input);  // not input = getchar();
                RockPaperScissors(input);
        }
        printf("\n\n\nTHE GAME IS OVER\n\n");
        return 0;
}

void RockPaperScissors(char usrVal)
{

        const int rock = 0;
        const int paper = 1;
        const int scissors = 2;

        srand(time(NULL));
        int comVal = rand() % 2 + 0;

        int usrScore = 0;
        int comScore = 0;
        int tie = 0;

        switch (usrVal)
        {
        case 'r':
                if (comVal == paper)
                        printf("\nYOU LOSE!!!\n");
                ++comScore;
                break;
                if (comVal == scissors)
                        printf("\nYOU WIN!!!\n");
                ++usrScore;
                break;
        case 'p':
                if (comVal == rock)
                        printf("\nYOU WIN!!!\n");
                ++usrScore;
                break;
                if (comVal == scissors)
                        printf("\nYOU LOSE!!!\n");
                ++comScore;
                break;
        case 's':
                if (comVal == rock)
                        printf("\nYOU LOSE!!!\n");
                ++comScore;
                break;
                if (comVal == scissors)
                        printf("\nYOU WIN!!!\n");
                ++usrScore;
                break;
        default:
                printf("\nTIE\n");
                ++tie;
                printf("\ncomVal: %d  usrVal: %c\n", comVal, usrVal);
        }

        printf("\nscore is user: %d   computer: %d  Tie: %d \n\n\n\n", usrScore, comScore, tie);
}
anoopknr
  • 3,177
  • 2
  • 23
  • 33