Thread: Beginner C Question Help

  1. #1
    Registered User
    Join Date
    Jun 2011
    Location
    Melbourne Australia
    Posts
    3

    Beginner C Question Help

    Quote Originally Posted by Nimbuz View Post
    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

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    @Susan M:

    Has the book covered loops?
    If yes, I think a do/while loop would be needed.
    If not, I would skip this problem till the book covers loops.

    Tim S.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Location
    Melbourne Australia
    Posts
    3
    Quote Originally Posted by stahta01 View Post
    @Susan M:

    Has the book covered loops?
    If yes, I think a do/while loop would be needed.
    If not, I would skip this problem till the book covers loops.

    Tim S.
    Actually Tim when I first came across the problem I thought that a do/while loop would be suitable here. However Chapter 4, which I'm about to start, covers do/while loops. Therefore I thought that being a Chapter 3 problem it should only cover the topics learnt. However, I'm starting to wonder if this book is logical :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ beginner Question
    By zyberb in forum C++ Programming
    Replies: 4
    Last Post: 03-27-2011, 01:03 PM
  2. Beginner question - please help
    By mo34 in forum C++ Programming
    Replies: 2
    Last Post: 09-11-2008, 02:34 PM
  3. Another beginner question
    By jcafaro10 in forum C++ Programming
    Replies: 40
    Last Post: 07-31-2007, 04:06 PM
  4. a beginner question
    By xpflyer2002 in forum C Programming
    Replies: 6
    Last Post: 03-07-2004, 05:24 PM
  5. beginner question
    By davidnj732 in forum C Programming
    Replies: 2
    Last Post: 02-17-2003, 09:45 PM