Originally Posted by
Nimbuz
Sorry for digging up an old thread, but I had the same problem as the OP. This book indeed sucks, I wasted a day on the isDigit, rand and time functions! Switching to "C for Dummies".
Well I'm certainly digging up an old thread too lol. I have just started this book myself and am frustratingly working on the same problem. I found this thread after extensive searching for information and ideas. I have gone over and over this problem so much that I wonder if it's possible using only the lessons learned from the first three chapters. The book's website has a download available for fixing errors found inthe book but I have still come across other small errors just in the first three chapters. I would be very keen to hear what you thought of "C for Dummies" Nimbuz - assuming that after two years you will be reading this.
I have mine sort of working but I don't feel that it is truly correct - if the user has entered a character the program says to "please enter a digit" but still continues on with the rest of the program.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
main() //Guess a number from 1 to 10
{
int iRandomNum = 0;
//int iResponse = 0;
char cResponse = '\0';
srand(time(NULL));
iRandomNum = (rand() % 10) + 1;
printf("\nGuess a number from 1 to 10: ");
//scanf("%d", &iResponse);
scanf("%c", &cResponse);
if (isdigit(cResponse) == 0) {
// check to see if user has entered a digit
printf("\nYou need to enter a digit please. \n");
}
if (cResponse == iRandomNum) {
// if user input = random number
printf("\nYou are correct!\n");
} else
printf("\nBummer dude\n");
printf("\nThe correct number was %d\n", iRandomNum);
}
Would appreciate any advice if anyone is reading this after so long