0
#include <stdio.h>
#include <stdlib.h>
int twento(void);
int main(int argc, char *argv[])
{
  int number1,number2,sum1=0,sum2=0;
  do{
  printf("Player 1 you start to play:\n");
  number1=twento();
  sum1 = number1 + sum1;
  printf("Player1 Your sum is %d\n",sum1);
  printf("Player2 Your turn to play:\n");
  number2=twento();
  sum2=number2+sum2;
  printf("Player2 Your sum is: %d\n",sum2);
  if (sum1==21)printf("\nPlayer1 You win");
  else if(sum2==21)printf("Player2 You win");
  else if(sum1>21)printf("\n Player 1 You Lose");
  else if(sum1>21)printf("\n Player 2 You Lose");
}while(sum1<=21 || sum2<=21);

  system("PAUSE"); 
  return 0;
}
int twento(){
    srand(time(NULL));
    return rand()%9+1;
}

Can anyone tel me y this program does run until the sum is greater than 21 ?

The Question was ::
Qu.4 Write down a C program to play the game of 'twenty-one' between two players.
The game consists of buying numbers in the range 1 to 9. The numbers are added
until the sum equals to 21. The player to score 21 first wins the game.

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by foreshadowed
0

srand should be in main.

while(sum1 <= 21 || sum2 <= 21);

above will cause your loop to execute until both sum1 and sum2 are greater or equal to 21, which *probably* isn't what you want. try && instead. I'm thinking you'd likely just want < 21 as well as a test condition.

HTH

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.