It was from a Analog magazine in a series called "Learning to C".
I feel like I am almost there, but evidently, the included osbind.h is not part of the linux includes.
I need a function that returns a random number and that is where I am stuck.
The code for the C file, guess_number.c, is below.
And this is my first attempt at any C compiling so if I appear dumb in this it is because I am.
Code: Select all
#include <stdio.h>
#include <osbind.h>
#define TRUE 1
#define FALSE 0
main()
{
int num, guess, win, turns, play;
play = TRUE;
while (play) {
turns=0; win = FALSE;
num = get_num();
while (!win) {
++turns;
guess = get_guess();
win = check_guess(num, guess);
}
printf("It took you %d turns.\n\n");
play = play_again();
}
}
int get_num()
{
int n;
n = (int) random();
n = abs(n) % 99 + 1;
return(n);
}
int get_guess()
{
int g;
g = 0;
while (g<1 || g>100) {
printf("Enter a number from 1 to 100: ");
scanf("%d", &g);
printf("\n\n");
}
return(g);
}
int check_guess(num, guess)
int num, guess;
{
int wn=FALSE;
if (guess < num)
printf("Too low\n\n");
else if (guess > num)
printf("Too high\n\n");
else {
printf("You guessed it!\n");
wn = TRUE;
}
return(wn);
}
int play_again()
{
int ch, p;
p = -1;
ch = getchar();
while ( (p!=TRUE) && (p!=FALSE) ) {
printf("Play again? ");
if ( (ch=getchar()) == 'y' || ch == 'Y')
p = TRUE;
else if (ch == 'n' || ch == 'N')
p = FALSE;
}
printf("\n\n");
return(p);
}