I was trying to build the game of rock paper scissors in C. When I made it for playing one single time, it was working properly. But when I tried to make it run for multiple number of times using for loop, it failed to scan the value given by the user. It is running the code correctly when it is the eventh turn, i.e , 2nd,4th,6th and so on. But it fails to take the input when it is odd turn. The code is given below:
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
int main()
{
int n;
printf("Enter the number of times you want to play rock paper scissors: ");
scanf("%d", &n);
for(int i=0;i<n;i++){
char c,p;
//creating random rps generator
int num;
srand(time(0));
num=rand()%3+1;
if(num==1){
c='r';
}
else if(num==2){
c='p';
}
else{
c='s';
}
//Taking input from the user
printf("Enter rock(r),paper(p) or scissors(s) : ");
scanf("%c",&p);
//Main code
if(c==p){
printf("It's a draw.\n");
}
else if((c =='r' && p =='p') || (c =='p' && p =='s') || (c =='s' && p =='r')){
printf("You win.\n");
}
else if((c =='p' && p =='r') || (c =='s' && p =='p') || (c =='r' && p =='s')){
printf("You lose.\n");
}
}
return 0;
}