c - Can't get loop to stop running -
i trying write program rolls 2 dice, calculates number of rolls each side of dice, calculates actual , expected percents asks user roll again. however, loop wont ask user if want continue or stop looping after 5 rolls. not allowed use arrays yet. beginner. :)
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> int main(void) { int i; int number; float expected_percent; int val2=1, val3=1,val4=1,val5=1,val6=1,val7=1; int val8=1,val9=1,val10=1,val11=1, val12=1 ; int val; int rolldice; int roll_1, roll_2; float perc2, perc3, perc4, perc5, perc6, perc7, perc8; float perc9, perc10, perc11, perc12; float exp2, exp3, exp4, exp5, exp6, exp7, exp8, exp9; float exp10, exp11, exp12; char ans, y, y, n, n; srand(time(null)); do{ printf("how many times want roll dice ? \n \n "); scanf("%d/n/n",&number); (i = 0; < number; i++) { roll_1 = rand() %6+1; roll_2 = rand() %6 + 1; rolldice = roll_1 + roll_2; val = rolldice; switch (val){ case 2: val2++; break; case 3: val3++; break; case 4: val4++; break; case 5: val5++; break; case 6: val6++; break; case 7: val7++; break; case 8: val8++; break; case 9: val9++; break; case 10: val10++; break; case 11: val11++; break; case 12: val12++; break; } } perc2 = (float)(100 *(float)val2/number); perc3 = (float)(100 *(float)val3/number); perc4 = (float)(100 *(float)val4/number); perc5 = (float)(100 *(float)val5/number); perc6 = (float)(100 *(float)val6/number); perc7 = (float)(100 *(float)val7/number); perc8 = (float)(100 *(float)val8/number); perc9 = (float)(100 *(float)val9/number); perc10 = (float)(100 *(float)val10/number); perc11 = (float)(100 *(float)val11/number); perc12 = (float)(100 *(float)val12/number); exp2 = (perc2 / number) * 36; exp3 = (perc3 / number) * 36; exp4 = (perc4 / number) * 36; exp5 = (perc5 / number) * 36; exp6 = (perc6 / number) * 36; exp7 = (perc7 / number) * 36; exp8 = (perc8 / number) * 36; exp9 = (perc9 / number) * 36; exp10 = (perc10 / number) * 36; exp11 = (perc11 / number) * 36; exp12 = (perc12 / number) * 36; printf("\n\nroll\ttotal\tsession\t expected\n\n"); printf("2\t%d \t%.2f%% \t %.2f%% \t \n",val2, perc2, exp2); printf("3\t%d \t%.2f%% \t %.2f%% \t \n",val3, perc3, exp3); printf("4\t%d \t%.2f%% \t %.2f%% \t \n",val4, perc4, exp4); printf("5\t%d \t%.2f%% \t %.2f%% \t \n",val5, perc5, exp5); printf("6\t%d \t%.2f%% \t %.2f%% \t \n",val6, perc6, exp6); printf("7\t%d \t%.2f%% \t %.2f%% \t \n",val7, perc7, exp7); printf("8\t%d \t%.2f%% \t %.2f%% \t \n",val8, perc8, exp8); printf("9\t%d \t%.2f%% \t %.2f%% \t \n",val9, perc9, exp9); printf("10\t%d \t%.2f%% \t %.2f%% \t \n",val10, perc10, exp10); printf("11\t%d \t%.2f%% \t %.2f%% \t \n",val11, perc11, exp11); printf("12\t%d \t%.2f%% \t %.2f%% \t \n",val12, perc12, exp12); } while (ans == y || y); printf("\n\nwould roll again? please answer y or n\n\n"); scanf("%c", &ans); return 0; }
do { /* ... */ } while (ans == y || y);
you should change into
do { /* ... */ } while (ans == 'y' || ans == 'y');
Comments
Post a Comment