All pastes #382840 Raw Edit

Untitled

public text v1 · immutable
·published 2007-03-05 23:08 UTC
rendered paste body
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int inGame ();

int main()
{
	printf("I have a number between 1 and 1000\nCan you guess my number?\nPlease type your first guess\nType e to end.\n\n");

	while (inGame () == 1);

	printf("Ending Game.\n");

	return 0;
}

int inGame ()
{
	int in, num; // Variable creation
	char end; // Variable creation

	num = 1+(rand ( ) % 1000);  // create a 'random' number

	printf("Guess:  "); // Ask for a guess

	while (scanf("%d", &in) && in != num) // While they enter a digit and the input does not equal the number
	{
		if(in < num) // input is less than the number
			printf("Too low.  Try again:  ");
		else if(in > num) // input is greater than the number
			printf("Too high.  Try again:  ");
	}

	printf("Correct!\n"); // They won!

	while (scanf("%c", &end)) // Find the first entered character
	{
		if (strncmp(&end, "y", 1) == 0) // If the first character of end is "y" return 1
			return 1;
		else if (strncmp(&end, "n", 1) == 0) // If the first character of end is "n" return 0
			return 0;
		else
			printf("Would you like to play again? (y or n)  "); // If they didn't enter something valid ask again.
	}

	return 1;
}