Beginner C Question Help

This is a discussion on Beginner C Question Help within the C Programming forums, part of the General Programming Boards category; I'm currently reading C Programming for the Absolute Beginner and am stuck on one of the chapter questions. This is ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    msh
    msh is offline
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    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
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    >>warning: return type defaults to 'int'
    Main must return int. I posted a link earlier (red text). Read it. Very informative.

    >>warning: implicit declaration of function 'time'
    "Implicit declaration" means you used a function without first declaring it. That means, in this case, that you didn't include the correct header. You must include time.h.

    As for the rest...
    You should really think about the logic of your program.
    In psuedo code, you want to do:

    Check if answer is a digit.
    Check if answer is in range 0 > x < 10.

    How do you do this?
    Can you answer those questions?
    Think about it. You should be able to at least do the second.
    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.

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

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, your book can't have mentioned rand without doing stdlib.

    Food for thought: If isdigit returns true/false, why do you need to compare it with anything to make a true/false decision?

Page 1 of 3 123 LastLast
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, 05: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, 07:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21