Thread: C Programming For The Absoloute Beginner Chapter 3 Challenge 2 Query

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    2

    C Programming For The Absoloute Beginner Chapter 3 Challenge 2 Query

    I've been working on this program for a good couple of weeks between my work hours.

    The challenge in the book was:
    Build a number guessing game that uses input validation (isdigit function) to verify that the user has entered a digit and not a non-digit (letter).

    The following is the best I could come up with.

    You'll notice that I ask the user to input their guess twice, once to assign to an integer and once to a character because I had no idea how to effectively convert the character to an integer.

    This is the first proper game I have made with C so please feel free to point out any rookie errors :>

    Any suggestions as to how to improve the code from here?

    Cheers.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main()
    
    {
        int iRandomNum = 0;
        srand(time(NULL));
        char cResponse = '\0';
        int dResponse = 0;
    
    iRandomNum = (rand()%10)+1;
    
        printf("iRandomNum= %d\n",iRandomNum);
        printf("please enter a digit\n");
    
        scanf("%c",&cResponse);
        printf("One more time?\n");
        scanf("%d",&dResponse);
        printf("cResponse= %c\n",cResponse);
    
    if (isdigit (cResponse)==0)
        printf("letter was inserted, please enter a number next time\n");
    else
        printf("iRandomNum= %d, this is a digit.\n",iRandomNum,", \n");
    
    
    if (dResponse == iRandomNum)
        printf(" well done!");
    
    else {
            printf("you lose\n");
            printf("You guessed cResponse= %c\n",cResponse);
            printf("You should have guessed %d",iRandomNum);}
    }

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Welcome to the forum. A couple of things for you:

    Read How to define main FAQ

    Did you not learn loops yet? Usually when you have input validation excersises it is part of a loop so that you keep asking until the input is correct.

    Take a look at the atoi() function. That should allow you to clean up your code a little.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Code:
    printf("iRandomNum= %d, this is a digit.\n",iRandomNum,", \n");
    What exactly are you trying to do here? printf works like this:

    printf([string to print, with %d and %s], [integer to replace %d], [string to replace %s])

    When you use it, you have a string as your third argument, but you don't have a "%s" anywhere in the first.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    2
    Those are just artefacts that didn't get picked up in debugging.
    Now that I have deleted them, it looks like this:
    printf("iRandomNum= %d, this is a digit.\n", iRandomNum);
    I use it to return the value f iRandomNum for debugging.

    Currently trying to integrate atoi in so that I don't need to ask the user to input their answer twice.
    Already made the program crash so that is proving fun to solve.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main(void)
    
    {
        int iRandomNum = 0;
        srand(time(NULL));
        char cResponse = '\0';
        int dResponse = 0;
    
    iRandomNum = (rand()%10)+1;
    
        printf("iRandomNum= %d\n",iRandomNum);
        printf("please enter a digit\n");
    
        scanf("%c",&cResponse);
        printf("cResponse= %c\n",cResponse);
    
    
    if (isdigit (cResponse)==0)
        printf("letter was inserted, please enter a number next time\n");
    else
        printf("iRandomNum= %d, this is a digit.\n", iRandomNum);
    
    dResponse = atoi(cResponse);
    printf("%d",dResponse);
    
    if (dResponse == iRandomNum)
        printf(" well done!");
    
    else {
            printf("you lose\n");
            printf("You guessed cResponse= %c\n",cResponse);
            printf("You should have guessed %d",iRandomNum);}
    }

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Dekuben View Post
    Currently trying to integrate atoi in so that I don't need to ask the user to input their answer twice.
    Already made the program crash so that is proving fun to solve.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main(void)
    
    {
        ....
    dResponse = atoi(cResponse);
    .....
    }
    Your compiler should be giving you errors with how you are using atoi(). It expects a C style string, not a single character. A couple of ways around this:

    1. You could just pass the address of cResponse to atoi with &cResponse.
    2. You could just do math to convert it to an integer: dResponse = cResponse - '0'.
    3. You could setup a C style string, that way your program would be able to handle numbers that were entered >9. Look at the example on the atoi() page I pointed you to.

    Also, as I mentioned before, did you not learn loops yet? Something like this for user input:
    Code:
    do
        ask user for a number
        store the number in a variable
    loop while user input is not a number
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I should read a bit from C Programming For the Absolut Beginner.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Dekuben View Post
    Currently trying to integrate atoi in so that I don't need to ask the user to input their answer twice.
    Already made the program crash so that is proving fun to solve.
    Like a hint?

    Look up scanf() in your C library documentation. It will almost certainly have more information than your textbook... pay special attention to it's return value.

    Now with that in mind... think about doing your inputs directly into an integer.

    A little testing and you might be surprised how easy it is...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner C query: unexpected behaviour in programme
    By Jimbo1981 in forum C Programming
    Replies: 5
    Last Post: 01-15-2011, 09:39 AM
  2. Beginners C Programming Challenge
    By UCnLA in forum C Programming
    Replies: 2
    Last Post: 03-18-2008, 12:15 PM
  3. Programming challenge
    By sybariticak47 in forum Windows Programming
    Replies: 1
    Last Post: 01-05-2006, 02:14 AM
  4. programming challenge
    By grohyt in forum C++ Programming
    Replies: 3
    Last Post: 09-19-2005, 07:07 PM
  5. C programming challenge
    By lost in C in forum C Programming
    Replies: 6
    Last Post: 03-11-2002, 01:27 AM