C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-10-2009, 10:16 PM   #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
pmacdonald is offline   Reply With Quote
Old 07-10-2009, 10:35 PM   #2
DESTINY
 
BEN10's Avatar
 
Join Date: Jul 2008
Location: in front of my computer
Posts: 656
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.......

for( ; ; )
printf("If you can't make it good, at least make it look good");

PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
BEN10 is offline   Reply With Quote
Old 07-10-2009, 10:50 PM   #3
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 07-10-2009, 10:50 PM   #4
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,922
>> 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.
Sebastiani is offline   Reply With Quote
Old 07-11-2009, 12:41 AM   #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.
pmacdonald is offline   Reply With Quote
Old 07-11-2009, 09:49 AM   #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);
        }
    }
}
pmacdonald is offline   Reply With Quote
Old 07-11-2009, 10:33 AM   #7
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Must must return int.
Missing ( and ) around the if.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-11-2009, 12:12 PM   #8
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> 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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 07-11-2009, 12:15 PM   #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"
pmacdonald is offline   Reply With Quote
Old 07-11-2009, 12:24 PM   #10
msh
Novice
 
Join Date: Jul 2009
Posts: 32
For starters, you're missing an include.

#include <stdlib.h>


That error is responsible for half your warnings.
msh is offline   Reply With Quote
Old 07-11-2009, 12:43 PM   #11
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-11-2009, 01:02 PM   #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?
pmacdonald is offline   Reply With Quote
Old 07-11-2009, 01:08 PM   #13
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
>>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.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-11-2009, 01:11 PM   #14
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
Did you read Salems' post at all??
itCbitC is offline   Reply With Quote
Old 07-11-2009, 01:11 PM   #15
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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?
tabstop is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:53 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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