I am trying to write a coin toss program that loops. However my while loop doesn't work. The program should continue until the user presses something other than 'y', but for some reason after the second pass it ends the program.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
char coinToss();
int main() {
char result;
char ch = 'y';
srand(time(0));
while (ch == 'y' || ch == 'Y') {
result = coinToss();
printf("The coin toss is %c", result);
printf("\nToss the coin again?");
scanf_s("%c", &ch, 1);
}
}
char coinToss() {
char result;
int coin;
coin = rand() % 2 + 1;
if (coin == 1) {
result = 'T';
}
else {
result = 'H';
}
return result;
}