Problem with Rock Scissor Paper program in C - c

I have a problem with my program. I want it to execute a loop that makes the result of the game and increments the score. But for some reason it doesn't make the exiting loop or shows the correct score. Below is the code. What the problem could there be.?
Is the there something wrong with the function calls. I want to execute ask the player if he wants to continue and in the case he wants to play to shwo the new score.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void game_exit(void);
void game_playing(void);
void game_choice_menu();
int algorithm(int *player_choice, int *computer_choice);
int main()
{
system("clear");
printf("\n|==============================|\n| ROCK-SCISSORS-PAPER |\n|==============================|\n");
printf("\n1.Start Playing!\n2.Exit!\n\nWhat you wanna do: (pick 1 or 2): ");
int input;
scanf("%d",&input);
switch (input)
{ case 1:
game_playing();
game_exit();
break;
case 2:
game_exit();
break;
default:
printf("WRONG INPUT!! TRY AGAIN!!!");
game_exit();
break;
}
}
void game_choice_menu(void)
{
static int player,computer;
player =0;
computer = 0;
system("clear");
printf("PLAYER : %d COMPUTER: %d \n", player, computer);
printf("===============================");
printf("\nRock, Scissors or Paper? (1 for Rock, 2 for Scissors and 3 for Paper) ");
int answer;
do{
scanf("%d",&answer);} while (answer<=1 && answer>=3);
int computer_choice = rand()% 3;
int result;
result = algorithm(&answer,&computer_choice);
if (result==0) {
player++;
//system("pause 3");
printf("\n YOU WON!!!\n");}
else {
//system("pause 3");
printf("\n YOU LOSE!!!\n");
computer++;}
}
int algorithm(int *player_choice, int *computer_choice)
{
if ( *player_choice < *computer_choice)
{
if ( (*computer_choice - *player_choice)==2)
return 1;
else if ((*computer_choice - *player_choice)==1)
return 0;
}
else if (*player_choice > *computer_choice)
{
if ((*player_choice - *computer_choice)==2)
return 1;
if ((*player_choice - *computer_choice)==1)
return 0;
}
}
void game_playing(void)
{
for (;;)
{
game_choice_menu();
//printf("\n Want to play again? (Y for yes, N for no)");
//char reply;
//scanf("%c",&reply);
//if (reply == 'y' || reply == 'Y')
// continue;
//else if (reply == 'n' || reply == 'N')
// break;
}
}
void game_exit(void)
{
system("clear");
printf("THANK YOU FOR PLAYING!!!");
printf("\n");
}

Related

C code stops after if statement inside while loop

I am trying to create a program that assigns the user one of three questions at random. I figured out that part however I want to make it so that after they correctly answer their assigned question that they are asked if they want to take another one.
int main(void) {
srand(time(0));
int luckyNumber;
int quizNumber;
int score;
int totalScore;
int averageScore;
int attempts;
char answer1;
char answer2;
char answer3;
char opt1;
char loop = 'y';
printf("Welcome to the quiz program!\n");
while(loop == 'y') {
printf("Please enter a lucky number between 1 and 9: ");
scanf("%d", &luckyNumber);
quizNumber = rand() % 3 + 1;
printf("Your quiz number is %d\n", quizNumber);
if (quizNumber == quizNumber) {
while (score != 5) {
printf("Quiz 1\n");
printf("A. True\n");
printf("B. False\n");
printf("Enter your answer: ");
scanf(" %c", &answer1);
if (answer1 == 'B' || answer1 == 'b') {
printf("Correct!\n");
score = score + 5;
break;
}
else {
printf("Incorrect!, Try again.\n");
printf("\n");
}
break;
}
break;
}
printf("Would you like to go again y/n?");
scanf("%c", &loop);
if (loop != 'y')
loop = 'n';
}
return 0;
}
The problem I am having is after I compile the code, it stops when the correct answer is input rather than going on to ask if the user would like to do another quiz.

I don't understand why a part of my code isn't connecting to the rest

I have an assignment for a coding class where I have to create a code for the rock, paper scissors game, with different functions, calling rock 1 and paper 2 and scissors 3.
I also have to create a menu, then have the person chose, save that, then have the computer generate a random number and then print what they played, what the computer played and who won.
However, the last part isn't connecting, and I don't understand where I went wrong, so I'm really really lost and I would really appreciate any help.
For reference here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float response, choice;
int playerInput() {
printf(" For rock press 1\n ");
printf("For paper press 2\n ");
printf("For scissors press 3\n ");
printf("What would you like to play? ");
scanf("%f", &response);
return response;
}
int computerChoice() {
int lower = 1, upper = 3, count = 1;
srand(time(0));
printf("The random number that: ");
for (int i = 0; i < count; i++) {
int choice = (rand() % (upper - lower + 1)) + lower;
printf("the computer chose is %d ", choice);
}
return choice;
}
void displayWinner() {
if (response == choice) {
printf("You tied, lets play again");
}
if (choice == 1 && response == 2) {
printf("you won");
}
if (choice == 1 && response == 3) {
printf("computer won");
}
if (choice == 2 && response == 1) {
printf("computer won");
}
if (choice == 2 && response == 3) {
printf("you won");
}
if (choice == 3 && response == 1) {
printf("you won");
}
if (choice == 3 && response == 2) {
printf("computer won");
}
}
int main(void) {
int pinput, cinput, dinput;
pinput = playerInput();
cinput = computerChoice();
displayWinner();
return 0;
}
int choice =
But why declare a local with the same name as a global. I suggest that's your first mistake.
On fixing it you'll find the next line prints garbage because it's now printing a %d from a float (that got widened to a double).

When I call a function I get: Process returned -1073741819

I am writing a program to print a fee invoice of a student. When I call my function getPrefix() my program essentially crashes and says Process returned -1073741819; the program compiles and everything.
The problem is occuring in my deleteCourse() function. I call the function getPrefix() to print out the name associated with the specific crn and the program crashes.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LOAD 12
#define creditHour 120.25
// prototype functions above main
void printMenu();
void addCourse(int [], int crnValid[]);
void courseList ();
int deleteCourse (int [], int crnValid[]);
char* getPrefix (int);
void printInvoice (int [], int);
int main()
{ // start main
int studentId ;
int option = 1;
int crn[MAX_LOAD] = {0}; // used to keep track of courses being taken
int crnValid[MAX_LOAD] = {4587,4599,8997,9696,7895,9658,4287,9599,8927,7696,7890,9008} ; // used to check if crns are valid
printf("Welcome! Enter the student's ID number:\n");
scanf("%d", &studentId);
while (option != 0){
printMenu();
scanf("%d", &option);
switch(option){
case 1:
addCourse (crn, crnValid);
break;
case 2:
deleteCourse (crn, crnValid);
break;
case 3:
printInvoice (crn, studentId) ;
break;
case 0:
break;
default:
printf("Please enter a valid option: \n");
} ;
}
return 0;
} // end main
// define functions below main
// ------------------------------------------------------------------------------------
void printMenu(){
printf("\n---------------------------------\n") ;
printf("Choose from the following options:\n");
printf("1- Add a course for the student\n");
printf("2- Drop a course for the student\n");
printf("3- Print the fee invoice\n");
printf("0- Exit program\n");
printf("---------------------------------\n") ;
printf("Enter your selection: ");
}
// --------------------------------------------------------------------------------------
void addCourse(int crn[], int crnValid[]){
char choice ;
printf("Would you like to print the list of courses? (y/n)");
scanf(" %c", &choice) ;
if (choice == 'y'){
courseList();
}
int i; // loop incrementer
int success = 0;
int valid;
for (i = 1; i < MAX_LOAD; i++){
if (crn[i] == 0){
printf("Enter the course number to add: ");
scanf("%d", &crn[i]);
success = 1; // success = 1 when course has not been taken
break ;
}
}
if (success == 0){
printf("Course can't be added, you have attained the max number of courses!\n");
}
else {
printf("Course added, you have added course %d\n", crn[i]) ;
}
}
// --------------------------------------------------------------------------------------
void courseList ()
{
printf("CRN\tCOURSE\t\tCREDIT HOURS\n");
printf("4587\tMAT 236\t\t4\n");
printf("4599\tCOP 220\t\t3\n");
printf("8997\tGOL 124\t\t1\n");
printf("9696\tCOP 100\t\t3\n");
printf("7895\tMNT 125\t\t2\n");
printf("9658\tOPT 120\t\t3\n");
printf("4287\tMAT 836\t\t4\n");
printf("9599\tCOP 220\t\t3\n");
printf("8927\tGOM 124\t\t3\n");
printf("7696\tCOT 100\t\t4\n");
printf("7890\tMOT 125\t\t3\n");
printf("9008\tOPT 520\t\t5\n");
}
// -------------------------------------------------------------------------------------
int deleteCourse(int crn[], int crnValid[])
{
int i;
int success = 0;
int crnToDelete;
printf("Enter the course number to delete: ");
scanf("%d", &crnToDelete);
char prefix[20] ;
for (i=1; i < MAX_LOAD; i++){
if ( crn[i] == crnToDelete )
{
crn[i] = 0; // deletes course from array crn
success = 1;
break;
}
}
if (success == 0){
printf("The student isn't taking %d/%s", crnToDelete, getPrefix(crnToDelete)) ;
}
else {
printf("Course deleted!\n");
}
return crnToDelete ;
}
//--------------------------------------------------------------------------------------
char* getPrefix (int crnToDelete){
switch(crnToDelete){
case 4587:
return "MAT 236";
case 4599:
return "COP 220";
case 8997:
return "GOL 124";
case 9696:
return "COP 100";
case 4580:
return "MAT 230";
case 4581:
return "MAT 231";
case 4582:
return "MAT 232";
case 4583:
return "MAT 233";
case 3587:
return "MAT 256";
case 4519:
return "COP 420";
case 6997:
return "GOL 127";
case 9494:
return "COP 101";
}
}
// --------------------------------------------------------------------------------------
void printInvoice (int crn [], int studentId)
{
printf("\n\tVALENCIA COMMUNITY COLLEGE\n");
printf("\tORLANDO FL 10101\n");
printf("\t----------------------------\n");
printf("\n\tFee Invoice Prepared for Student V%d\n", studentId);
printf("\n\t1 Credit Hour = $ 120.25\n");
printf("\n\tCRN\tCR_PREFIX\tCR_HOURS");
int i;
for (i=1; i < MAX_LOAD; i++)
{
if (crn[i] != 0){ // if a course is in spot i, print course
printf("\n\t%d\n", crn[i]) ;
}
}
printf("\n\t\tHealth and Id fees:\t $ 35.00");
printf("\n\t-----------------------------------------");
printf("\n\t\tTotal Payments:") ;
}

Im getting Id returned 1 exit status [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am also having the "undefined reference to ERPSGame()" error as well as the "Id returned 1 exit status". Did I declare something wrong? I am using Orwell Dev C++.
What seems to be the problem? Is it my compiler ?
Check out the whole code:
/* GameBox BETA v0.2 */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
void ERPS();
void Menu();
void BJack();
void Log();
int ERPSGame();
void ERPSInstruct();
int main() {
char Choice;
system("cls");
printf("\t\t Welcome to Mark and Leroy's GameBox BETA v0.2 \n\n\n");
printf("--------------------------------------------------------------------------------\n");
printf("\t _________________________________________________________\n");
printf("\t ||_____________________________________________________|| \n");
printf("\t || \t\t|| \n");
printf("\t || \t\t|| \n");
printf("\t || \t\t|| \n");
printf("\t ||\t Please press 'A' to continue to the menu\t||\n");
printf("\t ||\t \t||\n");
printf("\t ||\t Otherwise, press 'F' to exit the program\t||\n");
printf("\t ||_____________________________________________________|| \n");
printf("\t ||_____________________________________________________||\n");
printf("--------------------------------------------------------------------------------\n\n");
/* Drawing Starts Here */
printf("\t\t ========================================\n");
printf("\t\t || || \n");
printf("\t\t || ||=============== || \n");
printf("\t\t || || || \n");
printf("\t\t || || ======= || \n");
printf("\t\t || || Gamebox || || \n");
printf("\t\t || || BETA v0.2 || || \n");
printf("\t\t || ||==============|| || \n");
printf("\t\t || || \n");
printf("\t\t || ||==== ||====|| == == || \n");
printf("\t\t || || || || || == == || \n");
printf("\t\t || ||===|| || || == || \n");
printf("\t\t || ||___|| ||====|| == == || \n");
printf("\t\t || == == || \n");
printf("\t\t || Artwork by: Mark Sanchez || \n");
printf("\t\t ========================================\n");
/* Drawing Ends Here */
scanf("%c", &Choice);
switch (Choice) {
case'A':case'a':{
Menu();
break;
}
case'F':case'f':{
exit(0);
}
default: {
main();
}
}
return 0;
}
void Menu() {
char Choice1;
system("cls");
printf("\t\t Welcome to Mark and Leroy's GameBox BETA v0.2 \n\n\n");
printf("--------------------------------------------------------------------------------\n\n");
printf("\t\t Press 'R' to play the Enhanced Rock-Paper-Scissors game\n\n\n");
printf("\t\t Press 'B'to play Blackjack\n\n\n");
printf("\t\t Press 'C' to view the changelog\n\n\n");
printf("--------------------------------------------------------------------------------\n\n");
scanf("%c", &Choice1);
switch(Choice1) {
case'R':case'r': {
ERPS();
break;
}
case'B':case'b': {
BJack();
break;
}
case'C':case'c': {
Log();
break;
}
default: {
Menu();
}
}
}
void ERPS() {
system("cls");
char Instruct1;
char Instruct2;
/* Menu of ERPS */
printf("\t\t\t Welcome to ENHANCED Rock Paper Scissors\n\n\n");
printf("\t\t\t Press A to start the game\n\n");
printf("\t\t\t Press S to view the instructions\n\n");
printf("\t\t\t Press 'P' to return to the main menu\n");
scanf("%c", &Instruct1);
scanf("%c", &Instruct2);
switch(Instruct2) {
case'A':case'a': {
ERPSGame();
break;
}
case'S':case's': {
ERPSInstruct();
break;
}
case'P':case'p': {
Menu();
break;
}
default:{
ERPS();
}
}
}
int rand_i(int n)
{
int rand_max = RAND_MAX - (RAND_MAX % n);
int ret;
while ((ret = rand()) >= rand_max);
return ret/(rand_max / n);
}
int weighed_rand(int *tbl, int len)
{
int i, sum, r;
for (i = 0, sum = 0; i < len; sum += tbl[i++]);
if (!sum) return rand_i(len);
r = rand_i(sum) + 1;
for (i = 0; i < len && (r -= tbl[i]) > 0; i++);
return i;
}
int ERPSGame(int argc, const char *argv[]) /* THIS IS THE ERPSGame */
{
char umove[10], cmove[10], line[255];
int user, comp;
int tbl[]={0,0,0};
int tbllen=3;
printf("Hello, Welcome to rock-paper-scissors\nBy The Elite Noob\n");
mainloop:
while(1)
{ // infinite loop :)
printf("\n\nPlease type in 1 for Rock, 2 For Paper, 3 for Scissors, 4 to quit\n");
srand(time(NULL));
comp = (weighed_rand(tbl, tbllen) + 1) % 3;
fgets(line, sizeof(line), stdin);
while(sscanf(line, "%d", &user) != 1) //1 match of defined specifier on input line
{
printf("You have not entered an integer.\n");
fgets(line, sizeof(line), stdin);
}
if( (user > 4) || (user < 1) )
{
printf("Please enter a valid number!\n");
continue;
}
switch (comp)
{
case 1 :
strcpy(cmove, "Rock");
break;
case 2 :
strcpy(cmove, "Paper");
break;
case 3 :
strcpy(cmove, "Scissors");
break;
default :
printf("Computer Error, set comp=1\n");
comp=1;
strcpy(cmove, "Rock");
break;
}
switch (user)
{
case 1 :
strcpy(umove, "Rock");
break;
case 2 :
strcpy(umove, "Paper");
break;
case 3 :
strcpy(umove, "Scissors");
break;
case 4 :
printf("Goodbye! Thanks for playing!\n");
return 0;
default :
printf("Error, user number not between 1-4 exiting...");
goto mainloop;
}
if( (user+1)%3 == comp )
{
printf("Comp Played: %s\nYou Played: %s\nSorry, You Lost!\n", cmove, umove);
}
else if(comp == user)
{
printf("Comp Played: %s\nYou Played: %s\nYou Tied :p\n", cmove, umove);
}
else
{
printf("Comp Played: %s\nYou Played: %s\nYay, You Won!\n", cmove, umove);
}
tbl[user-1]++;
}
}
void ERPSInstruct() {
system("cls");
char Instruct3;
printf("\t\t\t Instructions of the Game \n\n");
printf("\t\t\t There will be 5 choices to choose from\n");
printf("\t\t\t 1. Rock\n");
printf("\t\t\t 2. Paper\n");
printf("\t\t\t 3. Scissors\n");
printf("\t\t\t 4. Lizard\n");
printf("\t\t\t 5. Spock\n\n\n\n");
printf("\t\t This is the mechanism of the choices: \n\n");
printf("\t\t Scissor cuts paper. \n\n");
printf("\t\t Paper covers rock. \n\n");
printf("\t\t Rock crushes lizard. \n\n");
printf("\t\t Lizard poisons spock. \n\n");
printf("\t\t Spock smashes scissors. \n\n");
printf("\t\t Scissors decapitate lizard. \n\n");
printf("\t\t Lizard eats paper. \n\n");
printf("\t\t Paper disproves spock. \n\n");
printf("\t\t Spock vaporizes rock. \n\n");
printf("\t\t Rock crushes scissors. \n\n");
printf("\t\t\t Press 'P' to return to the menu\n");
scanf("%c", &Instruct3);
switch(Instruct3) {
case'P':case'p': {
Menu();
break;
}
default: {
ERPSInstruct();
}
}
}
void BJack() {
system("cls");
char Instruct2;
printf("BLACKJack \n\n\n");
printf("\t\t\t Press 'P' to return to the menu\n");
scanf("%c", &Instruct2);
switch(Instruct2) {
case'P':case'p': {
Menu();
break;
}
default: {
BJack();
break;
}
}
getch();
}
void Log() {
system("cls");
char Instruct3;
printf("CHANGELOG: \n\n\n");
printf(" 0.1 -- Fixed a bug wherein the program forces\n to do 'default' upon choosing a game\n\n\n");
printf("-----------------------------------------------\n\n");
printf(" 0.2 -- Fixed display in CMD: Added a few dividers \nas well as correcting a few grammatical mistakes\n");
printf(" Added awesome graphics \n\n\n");
printf("-----------------------------------------------\n\n");
printf("\t\t\t Press 'P' to return to the menu\n");
scanf("%c", &Instruct3);
switch(Instruct3) {
case'P':case'p': {
Menu();
break;
}
default: {
Log();
break;
}
}
}
Is it my way of declaring ERPSGame? I used int.
Function declaration is not matched with function definition.
Declaration:
int ERPSGame();
Definition
int ERPSGame(int argc, const char *argv[])
In Dev-C++, under Tools -> Compiler Options, put "-Wall" in the "...commands when calling compiler" box. It will be helpful for you to get warnings and do effective coding and saves your time too.
When you take in instruct1 and then use instruct2 in the switch...
scanf("%c", &Instruct1);
scanf("%c", &Instruct2);
switch(Instruct2) {
case'A':case'a': {
ERPSGame();
This could be giving you an odd value given the fact that users response is in instruct1. Potentially.

Is there any way to avoid using exit(0)?

I wanted to practice a little with separating logic into functions and using basic recursion in a primitive "Guess my number game", just as a sort of way to see where I'm at in my C programming.
Here's the code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
char playAgain()
{
printf("\nDo you wanna play again? ");
char resp = 0;
while (TRUE) {
scanf("%c", &resp);
if (resp == 'y') break;
if (resp == 'n') break;
}
return resp;
}
void play()
{
srand((int)time(NULL));
int num = rand() % 10 + 1;
int guess = 0;
int flag = 0;
int attempts = 0;
printf("\nGuess the number: \n");
while (TRUE) {
scanf("%d", &guess);
if (num > guess)
{
printf("Too low! ");
attempts++;
}
else if (num < guess)
{
printf("Too high! ");
attempts++;
}
else if (num == guess)
{
attempts++;
printf("You won! You did it in %d attempts", attempts);
char yo = playAgain();
if (yo == 'y') play();
else if (yo == 'n') exit(0);
else {
printf("Error!");
exit(1);
}
}
}
}
int main()
{
return play();
}
Everything works but I've only managed to make it quit when the user says 'n', by using exit(0) and I've heard it's bad practice. But having spent a couple of hours just noodling around and trying all the other ways I could think of (using flags, for example), I just couldn't make it work. It works for 'y', but as soon as I enter 'n', it doesn't quit and just calls the playAgain() function one more time, or freezes altogether (nothing happens).
I feel ashamed for not being able to solve this, but I'm out of ideas. Is there any other way, other than exit(0), to make it jump out of play() straight to return 0 in main()? The problem seems to be that I have an infinite game loop, so when the answer is 'n', I need to break out of the loop AND out of the play() function, and that has proven to be problematic. I remember being able to do this easily when I had no functions, but just all the relevant code in main(), but the whole point of this is to use functions. For example, I can't figure out how to make use of a return type of play(), so that it knows when to quit.
P.S. As always, seeing how often people get accused of this, I assure you this is not homework because I am, and always have been, doing programming strictly as a hobby. There's no professor I answer to, apart from you :-) And trust me that I did try to figure out a solution, but nothing seems to work. It's a question of good vs. bad design, so it's especially important for my self-teaching. exit(0) seems to be a hackish solution and serves no educational purpose.
P.P.S. By the way, I'm using xCode, and the program runs in its output window. Probably not, but maybe that's the reason quitting with 'n' doesn't work?
SUMMARY: The code works fine except when the answer is 'n', in which case it just asks "Do you wanna play?" again, or doesn't do anything at all.
Make your play function return a result:- int play( void ) {
then instead of exit(1) and exit(0); use return 1; and return 0;
at the end of play put return 0;
then in main
{
return play();
}
instead of recursion, just make a loop
int play( void)
{
srand((int)time(NULL));
int num;
int guess;
int flag;
while(1)
{
num = rand() % 10 + 1;
guess = 0;
flag = 0;
printf("\nGuess the number %d: \n", num);
while (1) {
scanf("%d", &guess);
if (num > guess)
{
printf("Too low! ");
}
else if (num < guess)
{
printf("Too high! ");
}
else if (num == guess)
{
printf("You won!");
char yo = playAgain();
if (yo == 'y') break;
else if (yo == 'n') return 0;
else {
printf("Error!");
return 1;
}
}
}
}
}
and if you want more of a game engine type approach
enum {
GUESS,
GAMEOVER,
QUIT,
} GAME_STATES;
typedef struct
{
enum GAME_STATES state;
int num;
int game_over;
} guessing_game_t;
void init_game(guessing_game_t* g)
{
g->state = GUESS;
g->game_over = 0;
g->num = rand() % 10 + 1;
}
void evaluate_guess(guessing_game_t* g, int guess)
{
if(g->num == guess)
{
printf("You won!");
g->state = GAMEOVER;
}
else if (g->num > guess)
{
printf("Too low! ");
}
else if (g->num < guess)
{
printf("Too high! ");
}
}
int input_guess( void )
{
int guess;
scanf("%d", &guess);
return guess;
}
void play_again( guessing_game_t* g)
{
char resp = 0;
printf("\nDo you wanna play again? ");
scanf("%c", &resp);
if (resp == 'y') init_game(g);
if (resp == 'n') g->state = QUIT;
}
void play( guessing_game_t* g)
{
while(g->state != QUIT)
{
switch(g->state)
{
case GUESS:
evaluate_guess(g, input_guess());
break;
case GAMEOVER:
play_again(g);
break;
}
}
}
int main()
{
guessing_game_t game;
init_game(&game);
play(&game);
}
You can put a flag before going on with the loop
Change the while(true) to while (continue_play), continue_play is the name of flag for an example
Then,
if (yo == 'n') exit(0);
can be change to
if (yo == 'n') continue_play=false;
and do the similar for the exit(1) case
continue_play then will be a flag to exit from the function.
You have one more thing that the program will end: the logic of your code is made like that u can also use break; instead of exit(0) and exit(1) because you use while loop and you will break it and the program will end.
Since you may recursive call you play() function, I think exit it with exit(0) is a proper choose.
Acutall, I do not think using recursion to implement play() is a good idea at all, two infinite loops are enough. You could implemet it like this:
void play(void)
{
srand((int)time(NULL));
int num;
int guess = 0;
int attempts = 0;
int try_again = 1;
while (try_again) {
num = rand() % 10 + 1;
printf("\nGuess the number: \n");
while (TRUE) {
scanf("%d", &guess);
if (num > guess)
{
printf("Too low! ");
attempts++;
}
else if (num < guess)
{
printf("Too high! ");
attempts++;
}
else if (num == guess)
{
attempts++;
printf("You won! You did it in %d attempts", attempts);
char yo = playAgain();
if (yo == 'n') try_again = 0;
break;
}
}
}
}

Resources