How do I get switch() to loop multiple times in C? -
How do I get switch() to loop multiple times in C? -
i have created fruit machine game. loop output several times before printing final output scored. simulate moving nature of real slot machine. when seek , loop switch() statements no output produced. how go doing this?
#include <stdio.h> #include <unistd.h> int main () { int firstreel, secondreel, thirdreel, loop; // generating 3 random numbers srand(time(null)); int rndone = rand () %4; int rndtwo = rand () %4; int rndthree = rand () %4; // assigning random numbers clearer var names firstreel = rndone; secondreel = rndtwo; thirdreel = rndthree; // switch statements each reel switch(firstreel){ case 0: printf("bell "); break; case 1: printf("cherry "); break; case 2: printf("orange "); break; case 3: printf("horseshoe "); break; } switch(secondreel){ case 0: printf("bell "); break; case 1: printf("cherry "); break; case 2: printf("orange "); break; case 3: printf("horseshoe "); break; } switch(thirdreel){ case 0: printf("bell\n"); break; case 1: printf("cherry\n"); break; case 2: printf("orange\n"); break; case 3: printf("horseshoe\n"); break; } // win/lose conditions if (firstreel == secondreel || firstreel == thirdreel || secondreel == thirdreel) printf("congratulations! win!\n"); else { printf("sorry, lose. play again? (y/n)\n"); } }
use sort of counter/ looping statement
int i=0; while(i< 10){ //your switch statements i++; }
as improve programming practice please include default case/scenario when switch input doesn't satisfy of cases.. helps in keeping code structured , avoids confusion showing other values have been taken care of. ex:
default: printf("invalid value entered"); break;
c
Comments
Post a Comment