We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,394 Members — Technology Publication meets Social Media

# Guessing Game

By clarence_cool03 on May 15th, 2009 8:06 am

{-='Guessing Game using Dev-C LanGuaGe...=-}

``````#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Function Declarations
void getNum(int gNum, int randNum);
void guessNum(int gNum, int randNum);
void displaySorry(int randNum);
void displaySuccessful(void);

int main(void)
{
//  Local Declarations
int gNum;
int randNum;

//  Statements
srand(time(NULL));
randNum = rand() % 20 + 1;
getNum(gNum, randNum);

system("pause");
return 0;
}// main

/* ================================= getNum ================================= */
void getNum(int gNum, int randNum)
{
//  Statements
printf("I am thinking of a number between 1 and 20.\n");
printf("Can you guess what it is? ");
scanf("%d", &gNum);

if(gNum == randNum)
{
displaySuccessful();
}// if
else
guessNum(gNum, randNum);
}// getNum

/* ================================ guessNum ================================= */
void guessNum(int gNum, int randNum)
{
//  Local Declarations
int count;

//  Statements
for(count = 0; count < 4; count++)
{
if(gNum > randNum)
{
printf("Your guess is high. Try Again: ");
scanf("%d", &gNum);
}// if

else if(gNum < randNum)
{
printf("Your guess is low. Try Again: ");
scanf("%d", &gNum);
}// else if
}// for

if(gNum == randNum)
{
displaySuccessful();
} // if
else
{
displaySorry(randNum);
}// else
}// guessNum

/* ============================= displaySorry ================================ */
void displaySorry(int randNum)
{
//  Statements
printf("\nSorry! The number was %d.\n", randNum);
printf("You should have gotten it by now.\n");
printf("Better luck next time.\n");
printf("Thank You!");
}// displaySorry

/* ========================== displaySuccessful ============================== */
void displaySuccessful()
{
//  Statements
printf("\nCongratulations! You did it!\n");
printf("Thank You!");
}// displaySucceful``````
Post: Markdown Syntax: Formatting Help

You