Using the following code transorfm it to include fuctions bu
Using the following code transorfm it to include fuctions but to accomplisg the same task. ask the user if he wants to play. display and calculate his total after every hit and tell him if he bust or loses.
#include <stdio.h>
 #include <time.h>
 #include <stdlib.h>
 int main(void) {
 //have the user be asked if he wants to play.
    srand(time(NULL));
    int hitOrStand, play;
    printf(\"Do you want to play (1. Yes. 2. No.)\ Enter your choice: \");
    fflush(stdout);
    scanf(\"%d\", &play);
//if he says no display \"maybe next time\".
    if (play != 1) {
        printf(\"Maybe next time.\ \");
        fflush(stdout);
        return 0;
    }
    int total = 0;
    while (1) {
 // if player says yes proccede to give the player a random card from 1-11.
        int card = rand() % 11 + 1;
        total += card;
 //display his card.
        printf(\"Your card: %d\\tYour total: %d\ \", card, total);
        fflush(stdout);
        if (total == 21) {
            printf(\"You win.\ \");
            fflush(stdout);
            break;
        } else if (total > 21) {
            printf(\"You BUST.\ \");
            fflush(stdout);
            break;
        }
 //then ask player if he wants to Hit or Stand.
        printf(\"Do you want to Hit or Stand (1. Hit. 2. Stand.): \");
        fflush(stdout);
        scanf(\"%d\", &hitOrStand);
//if player Hits display his new number and his new total. continue until he gets 21 or BUST.
 //if player chooses to stand with a total less than 21 he loses.
        if (hitOrStand == 2) {
            printf(\"You Lose.\ \");
            fflush(stdout);
            break;
        }
    }
    printf(\"See you next time....\ \ \");
    fflush(stdout);
 }
Solution
PROGRAM CODE:
#include <stdio.h>
 #include <time.h>
 #include <stdlib.h>
//function for hit or stand
 int hitOrStand()
 {
    int hit;
    //then ask player if he wants to Hit or Stand.
 printf(\"Do you want to Hit or Stand (1. Hit. 2. Stand.): \");
 fflush(stdout);
 scanf(\"%d\", &hit);
 //if player Hits display his new number and his new total. continue until he gets 21 or BUST.
 //if player chooses to stand with a total less than 21 he loses.
 if (hit == 2) {
 printf(\"You Lose.\ \");
 fflush(stdout);
 return 2;
 }
 return 1;
 }
//function to validate the total and print message accordingly
 int validate(int total)
 {
    int val = 2;
    if (total == 21)
    {
 printf(\"You win.\ \");
 val = 1;
    }
 else if (total > 21)
 {
 printf(\"You BUST.\ \");
 val = 1;
 }
 fflush(stdout);
 return val;   
 }
 //start the game
 void playGame()
 {
    int play;
    printf(\"Do you want to play (1. Yes. 2. No.)\ Enter your choice: \");
 fflush(stdout);
 scanf(\"%d\", &play);
 //if he says no display \"maybe next time\".
 if (play != 1) {
 printf(\"Maybe next time.\ \");
 fflush(stdout);
 exit(0);
 }
 else
 {
    int total = 0;
 while (1) {
 // if player says yes proccede to give the player a random card from 1-11.
 int card = rand() % 11 + 1;
 total += card;
 //display his card.
 printf(\"Your card: %d\\tYour total: %d\ \", card, total);
 fflush(stdout);
 if(validate(total)==1)
         break;
    if(hitOrStand() == 2)
        break;
    }
 }
 }
//main function
 int main(void) {
 //have the user be asked if he wants to play.
 srand(time(NULL));
    playGame();
 printf(\"See you next time....\ \ \");
 fflush(stdout);
 }
OUTPUT:



