It is good that you made some effort at indentation, but you really need to be consistent. For example:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int main(void)
{
srand(time(NULL));
int cardone, cardtwo, cardthree, cardfour, total1, total2;
char user_reply;
char keep_playing;
keep_playing = 'y';
user_reply = 'y';
while ((keep_playing) == 'y' || (keep_playing == 'y'))
{
printf("Would you like to play the game of 42? [y or n]");
scanf("%c", &keep_playing);
if (keep_playing == 'n')
{
return 0;
}
else
total1 = 0;
total2 = 0;
while (getchar() != '\n');
printf("\nYour score is 0 press any button to continue");
while ((user_reply == 'y') || (user_reply == 'y'))
{
if (total1 > 42)
{
printf("\nBusted!");
goto label;
}
while (getchar() != '\n');
cardone = (rand() % 21 + 1);
cardtwo = (rand() % 21 + 1);
printf("\nWould you like another number? [y or n]");
scanf("%c", &user_reply);
if ((user_reply == 'y') || (user_reply == 'y'))
{
total1 = (total1 + (cardone + cardtwo));
printf("\nYour score is %d", total1);
}
else if ((user_reply == 'n') || (user_reply == 'n'))
{
printf("\nNow it is computers go.");
while ((total2 <= 32))
{
cardthree = (rand() % 21 + 1);
cardfour = (rand() % 21 + 1);
total2 = total2 + (cardthree + cardfour);
printf("\nComputers score is %d", total2);
if (total2 > 42)
{
printf("Computer is bust");
}
}
}
}
{
label:
printf("\nResults");
printf("\nYour score is %d", total1);
printf("\nComputers score is %d", total2);
if (total1 > total2 && total1 <= 42 || total2 > 42)
{
printf("\nCongratulations! You won!\n");
}
else
printf("\nUnlucky, Computer wins!\n");
}
}
return 0;
}
Anyway, I suggest that you split up your rather long main function into smaller functions that do one thing and do it well. For example, I suggest that you write a function to ask the "Would you like to play the game of 42?" question and return the answer in lowercase:
Code:
char checkIfKeepPlaying(void);
This way, you can write your loop as:
Code:
while (checkIfKeepPlaying() == 'y')
totally eliminating the "if (keep_playing == 'n')" check.
You do not need to use goto and a label: a break will suffice. It is also unnecessary to introduce an extra code block, though you could consider moving that piece of code into a separate function.