# Challenge #2 Extended and Completed!!

 Emir ARITURK · February 11, 2015 #include#include#include#includeint diceTotal(){                int diceSum ;        srand(time(0));            diceSum =  (1 + rand()%6 ) +  (1 + rand()%6 ) + (1 + rand()%6 );                return diceSum;        }int main(){            char guess; //Whether the sum is greater or less (Greater : G,g ; Less : L,l)         char decision; //Whether the player wants to continue or stop playing (Yes : Y,y ; No: N,n)        int PrevRoll; //Rolling the dice first          int NewRoll; //Rolling the dice to compare current and previous results          int startingCash = 400; // 400\$ starting cash               printf("\n   Welcome !! Here we go!!");                   GAME_START : diceTotal(); //Rolling the dice        PrevRoll = diceTotal();        printf("\n\n  The first sum of three dice is : %d \n", PrevRoll );      COMPARE : puts("\n  How about the next number?");        printf("  Would it come out greater(G,g) or less(L,l) than %d or equal(E,e) to %d ? : ",PrevRoll,PrevRoll);          scanf(" %c", &decision); //User enters G , L or E for greater,less or equal            diceTotal(); //Rolling the dice again          NewRoll = diceTotal();           printf("\nThe next sum of three dice is : %d \n", NewRoll );                  if (NewRoll > PrevRoll){                          if ( decision == 'G' || decision == 'g' ){                puts("Congrats! Right guess, you've won 100\$ !! ");                 startingCash += 100;                  printf("Current cash is : %d\$. ", startingCash);                                  printf("\n\n  Do you want to play again ? (Y/N) : ");                 scanf(" %c", &decision);                               switch(decision){                    case 'Y' : case 'y' :                      goto GAME_START;                                         case 'N' : case'n' :                          goto  END_GAME;                                    default:                         printf(" Wrong decision..");                            goto RIGHT_DECISION;                               }//end of switch             }//end of inner if                          else if (decision == 'L' || decision == 'l' || decision == 'E' || decision == 'e'){              printf("Turns out you've guessed it wrong..You lost 120\$.."); startingCash-=120; printf("Current cash is : %d\$. ", startingCash);                     if (startingCash <= 0)                   {                   puts("\n\n   ---------- You're out of money! Turns out today is not your day.. ----------");                   goto END_GAME;                }                    printf("\n\n Do you want to play again ? (Y/N) : ");                   scanf(" %c", &decision);                               switch(decision){                    case 'Y' : case 'y' :                      goto COMPARE;                                         case 'N' : case'n' :                           goto  END_GAME;                                    default:                         printf(" Wrong decision.. ");                            goto RIGHT_DECISION;                             } //end of switch            } //end of inner else if        } //end of outer if                                 if (NewRoll < PrevRoll){                          if (decision == 'L' || decision == 'l'){                puts("Congrats! Right guess, you've won 100\$ !! ");                 startingCash += 100;                  printf("Current cash is : %d\$. ", startingCash);                                  printf("\n\n Do you want to play again ? (Y/N) : ");                 scanf(" %c", &decision);                               switch(decision){                    case 'Y' : case 'y' :                      goto GAME_START;                                         case 'N' : case'n' :                           goto  END_GAME;                                    default:                         printf(" Wrong decision..");                            goto RIGHT_DECISION;                               }//end of switch             }//end of inner if                          else if ( decision == 'G' || decision == 'g'|| decision == 'E' || decision == 'e' ){              printf("Turns out you've guessed it wrong..You lost 120\$.."); startingCash-=120; printf("Current cash is : %d\$. ", startingCash);                   if (startingCash <= 0)                 {                   puts("\n\n   ---------- You're out of money! Turns out today is not your day.. ----------");                    goto END_GAME;                }                  printf("\n\n Do you want to play again ? (Y/N) : ");                   scanf(" %c", &decision);                               switch(decision){                                    case 'Y' : case 'y' :                       goto COMPARE;                                         case 'N' : case'n' :                          goto  END_GAME;                                    default:                         printf(" Wrong decision.. ");                            goto RIGHT_DECISION;                             } //end of switch            } //end of inner else if         } //end of outer if                            if (NewRoll == PrevRoll){                          if (decision == 'E' || decision == 'e'){                 puts("Congrats! Right guess, you've won 130\$ !! ");                 startingCash += 130;                  printf("Current cash is : %d. ", startingCash);                                 printf("\n\n Do you want to play again ? (Y/N) : ");                 scanf(" %c", &decision);                               switch(decision){                    case 'Y' : case 'y' :                      goto GAME_START;                                         case 'N' : case'n' :                           goto  END_GAME;                                    default:                         printf(" Wrong decision..");                            goto RIGHT_DECISION;                               }//end of switch             }//end of inner if                                         else if ( decision == 'G' || decision == 'g'|| decision == 'L' || decision == 'l' ){                   printf("Turns out you've guessed it wrong..You lost 120\$.."); startingCash-=120; printf("Current cash is : %d\n", startingCash);                                          if (startingCash <= 0)                  {                  puts("\n\n   ---------- You're out of money! Turns out today is not your day.. ----------");                   goto END_GAME;                }                                       printf("\n\n Do you want to play again ? (Y/N) : ");                   scanf(" %c", &decision);                                   switch(decision){                    case 'Y' : case 'y' :                      goto COMPARE;                                         case 'N' : case'n' :                          goto  END_GAME;                                    default:                         printf(" Wrong decision.. ");                            goto RIGHT_DECISION;                                 } //end of switch               } //end of inner else if            } //end of outer if                      RIGHT_DECISION : printf("\n  Do you want to play again ? (Y/N) : ");                  scanf(" %c", &decision);                                 switch(decision){                      case 'Y' : case 'y' :                       goto GAME_START;                                           case 'N' : case'n' :                           goto  END_GAME;                                     default:                         printf(" Wrong decision..");                            goto RIGHT_DECISION;                                  }//end of switch                         END_GAME : printf("\n\t\t **********  Thanks for joining!  **********\n");             return 0; // Lovin' you babe.. }

## Replies

- page 1

 c student · February 11, 2015 ohh boyyy...did you ever do some form of (visual) basic programming in a point of your life? Emir ARITURK · February 12, 2015 Nope, never. Why did you ask that? c student · February 12, 2015 what do you know about goto? Emir ARITURK · February 12, 2015 I can't understand what you mean. Are we on an exam or smth like that?   but anyway, here is what I know about goto : http://en.wikipedia.org/wiki/Goto . And the work is right above. If I didn't have any idea about goto, believe me all those above would be quite stupid c student · February 12, 2015 i guessing you skipped over this: http://en.wikipedia.org/wiki/Spaghetti_code Emir ARITURK · February 12, 2015 I know what the spaghetti code is and it occurs when goto's used a few times or a lot. But does it mean that goto is never used? The reason why I used 3 goto terms in this code is, the related pieces of codes that doing similar tasks, had to stand far from each other, they couldn't stand adjacent I mean. This is the reason. But thanks for reminding again for this goto&spaghetti thing c student · February 12, 2015 goto is used for more extreme cases where it would provide a much cleaner solution than any other method, meaning it should be avoided where possible.  if you wanted to use common sections of code, it would be much better to use functions instead of bulking everything inside main. Emir ARITURK · February 12, 2015 This's been really good to know, starting from now I'm gonna build these sort of codes that way, thnx a lot again!! c student · February 12, 2015 that's good to hear.  good luck on future projects. Emir ARITURK · February 12, 2015 Thanks a lot c student!!
• 1
• 2

## C

129,866 followers