mercredi 23 novembre 2016

C while loop printing same statement multiple times and not working correclty

I am new to programming, and I looked at other forums and tried what they said. It did not work. The first problem is that after the user presses a choice, it is suppose to ask again if they want to continue, this is for now until I add more. However, when I actually have the computer generating 1, it does not print tie when the user presses 'r'. Why is this not working like I want? It prints, after one round, do you want to try again 3 times. Thanks in advance. Here is the code.

int main() {
    printf("Welcome to rock paper scissors!\n");
    int keep_playing=5;
    srand(time(NULL));
    int player_score=0;
    int computer_score=0;
    while (keep_playing==5) {
        printf("Press 'y' to play or 'n' to quit:\n");
        char playornot;
        char playerchoice;
        int computerchoice;
        computerchoice=(rand()%3)+1;
        printf("%d\n",computerchoice);
        scanf("%c",&playornot);
        if (playornot=='y') {
            printf("Type in 'r' for rock, 'p' for paper or 's' for scissors:");
            scanf("%c\n",&playerchoice);
            if ((computerchoice==1)&&(playerchoice=='r')){
                printf("Tie!\n");
            }
        } else if (playornot=='n') {
            keep_playing=4;
        }
    }
    return 0;
}




Aucun commentaire:

Enregistrer un commentaire