Thread: Beginner C Question Help

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    10

    Beginner C Question Help

    I'm currently reading C Programming for the Absolute Beginner and am stuck on one of the chapter questions. This is a really simple question but I am stuck.

    Problem:
    Build a number guessing game that uses input validation (isdigit() funciton) to verify that the user has entered a digit and not a non-digit (letter). Store a random number between 1 and 10 into a variable each time the program is run. Prompt the user to guess a number between 1 and 10 and alert the user if he was correct or not.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    main()
    {
    
        int iNumber = 0;
        int iAnswer = 0;
        srand(time());
    
        iAnswer = (rand() % 10) + 1;
    
        printf("Guessing Game\n");
        printf("\nGuess a number from 1 to 10: ");
        scanf("%d", &iNumber);
    
        if ( isdigit(iNumber) > 10 || < 1 )
            printf("Enter a number between 1 and 10");
    
            if (iNumber == iAnswer)
                printf("\nYou guessed right!\n");
    
            else
                printf("\nSorry, you guessed wrong\n");
                printf("The correct guess was %d\n", iAnswer);
    }
    I've already tried to run the program and debug it with no luck. I'm missing something from the text and obviously didn't apply that here.


    Any help will be greatly appreciated,

    -Peter

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by pmacdonald View Post
    I'm currently reading C Programming for the Absolute Beginner and am stuck on one of the chapter questions. This is a really simple question but I am stuck.

    Problem:
    Build a number guessing game that uses input validation (isdigit() funciton) to verify that the user has entered a digit and not a non-digit (letter). Store a random number between 1 and 10 into a variable each time the program is run. Prompt the user to guess a number between 1 and 10 and alert the user if he was correct or not.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    main()
    {
    
        int iNumber = 0;
        int iAnswer = 0;
        srand(time());
    
        iAnswer = (rand() % 10) + 1;
    
        printf("Guessing Game\n");
        printf("\nGuess a number from 1 to 10: ");
        scanf("%d", &iNumber);
    
        if ( isdigit(iNumber) > 10 || < 1 )
            printf("Enter a number between 1 and 10");
    
            if (iNumber == iAnswer)
                printf("\nYou guessed right!\n");
    
            else
                printf("\nSorry, you guessed wrong\n");
                printf("The correct guess was %d\n", iAnswer);
    }
    I've already tried to run the program and debug it with no luck. I'm missing something from the text and obviously didn't apply that here.


    Any help will be greatly appreciated,

    -Peter
    To check correctly do this
    Code:
    isdigit(iNumber) > 10 || isdigit(iNumber)< 1
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    isdigit() is for chars, not ints.

    > scanf("%d", &iNumber);
    You can't use isdigit here, scanf has already done that for you with the %d conversion.

    If you did this however
    Code:
    char ch;
    scanf("%c",&ch);
    if ( isdigit(ch) )
    Then that would be better usage of the function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> if ( isdigit(iNumber) > 10 || < 1 )

    Do be sure to look up the documentation on a function before you use it. Anyway, isdigit expects a *character* value and returns true or false (1 or 0). As it turns out you don't need the function at all here, just a straight comparison:

    Code:
    if ( iNumber > 10 || iNumber < 1 )
    Next, this statement:

    Code:
    printf("The correct guess was %d\n", iAnswer);
    If you want it to execute on the else condition, you'll need to enclose everything from the block in brackets, eg:

    Code:
    if ( iNumber > 10 || iNumber < 1 ) 
    {
            printf("Enter a number between 1 and 10");
    }
    else
    {
            if (iNumber == iAnswer)
            {
    		printf("\nYou guessed right!\n");
    	}
            else
            {
    		printf("\nSorry, you guessed wrong\n");
    		printf("The correct guess was %d\n", iAnswer);
    	}
    }
    EDIT: Ah, now I see isdigit is part of the requirements. In that case, declare a character and pass it to scanf with the %c format specifier.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    10
    Thanks for the quick replies and clarifications. I had a hunch that I was using the isdigit function wrong.

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    10
    A few more questions arose. How do I correctly call char cResponse to see if it is equal to the integer iAnswer?

    This code turned up many error in; srand, time, and rand.

    Any ideas?

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    main()
    {
    
        char cResponse = '\0';
        int iAnswer = 0;
        srand(time());
    
        iAnswer = (rand() % 10) + 1;
    
        printf("Guessing Game\n");
        printf("\nGuess a number from 1 to 10: ");
        scanf("%c", &cResponse);
    
        if isdigt(cResponse) > 10 || isdigit(cResponse) < 1
        {
            printf("Enter a number between 1 and 10");
        }
        else
        {
            if (cResposne == iAnswer)
            {
                printf("\nYou guessed right!\n");
            }
            else
            {
                printf("\nSorry, you guessed wrong\n");
                printf("The correct guess was %d\n", iAnswer);
            }
        }
    }

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Must must return int.
    Missing ( and ) around the if.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > if isdigt(cResponse) > 10 || isdigit(cResponse) < 1
    isdigit() is a TRUE/FALSE deal.

    isdigit('0') returns true (a non-zero result)
    isdigit('A') returns false (zero)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    10
    Please be more specific...around which if? I added them around the isdigit function and I still got the errors. I'm also not sure that I can compare the character to the integer so how would I do this yet still incorporate isdigit into the program?

    Errors:
    warning: return type default to 'int'
    In function 'main':
    warning: implicit declaration of function 'srand'
    warning: implicit declaration of function 'time'
    warning: implicit declaration of function 'rand'
    error: syntax error before "isdigit"

  10. #10
    Novice
    Join Date
    Jul 2009
    Posts
    568
    For starters, you're missing an include.

    #include <stdlib.h>


    That error is responsible for half your warnings.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    if isdigt(cResponse) > 10 || isdigit(cResponse) < 1
    ^ Missing ( and ).
    EVERY if must begin with ( and end with ).
    And as Salem points out, isdigit checks if the response is a digit or not. It doesn't check the range of a number, like if it's > 10.
    Isdigit is like asking "is this a digit?" and it responds yes or no.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Jul 2009
    Posts
    10
    msh - mmh, the book hasn't gone over that yet so there should be a way to make this program without incorporating it. I tried to add it but got the following errors:
    warning: return type defaults to 'int'

    In function 'main':
    warning: implicit declaration of function 'time'
    error: 'cResponse' undeclared (first sue in this function)
    error: (Each undeclared identifier is reported only once

    Elysia - I added the ( ) but still have errors.

    So how do I incorporate isdigit into this program without having it compare numbers. As Elysia pointed out the function if isdigit(cResponse) > 10 || isdigit(cResponse) < 1 wouldn't even serve its pupose...correct?

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Did you read Salems' post at all??

  14. #14
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by pmacdonald View Post
    msh - mmh, the book hasn't gone over that yet so there should be a way to make this program without incorporating it. I tried to add it but got the following errors:
    warning: return type defaults to 'int'

    In function 'main':
    warning: implicit declaration of function 'time'
    error: 'cResponse' undeclared (first sue in this function)
    error: (Each undeclared identifier is reported only once
    If the book has not covered stdlib.h yet you technically can't use srand() and rand() functions either.

    Quote Originally Posted by pmacdonald View Post
    So how do I incorporate isdigit into this program without having it compare numbers. As Elysia pointed out the function if isdigit(cResponse) > 10 || isdigit(cResponse) < 1 wouldn't even serve its pupose...correct?
    You loop for input until the user gives you something that looks like a digit.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by msh View Post
    You loop for input until the user gives you something that looks like a digit.
    No, you make some pseudo code for what you want to do and translate it into real C code, whether or not that includes isdigit.
    Beginners won't learn if you throw the spoon to them. They must learn to get it themselves.
    In other words, don't tell them how to logically do something! They must use their own logic to come to this conclusion!
    It's alright to explain functions do and introduce new functions to help with a specific task, but logic must never be handed out! This is a very critical part of programming and it is essential that every programmer out there knows it like the back of their hands!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner: Linked List question
    By WeatherMan in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2008, 07:16 AM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 06:51 AM
  3. beginner question
    By Barrot in forum C++ Programming
    Replies: 4
    Last Post: 08-19-2005, 02:17 PM
  4. Question About External Files (Beginner)
    By jamez05 in forum C Programming
    Replies: 0
    Last Post: 08-11-2005, 07:05 AM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 08:39 PM