![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 10
| Beginner C Question Help 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);
}
Any help will be greatly appreciated, -Peter |
| pmacdonald is offline | |
| | #2 | |
| DESTINY Join Date: Jul 2008 Location: in front of my computer
Posts: 656
| Quote:
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 | |
| | #3 |
| and the hat of vanishing 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) )
__________________ 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 | |
| | #4 |
| Guest 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 ) Code: printf("The correct guess was %d\n", iAnswer);
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);
}
}
|
| Sebastiani is offline | |
| | #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 | |
| | #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 | |
| | #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:
| |
| Elysia is offline | |
| | #8 |
| and the hat of vanishing 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 | |
| | #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 | |
| | #10 |
| 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 | |
| | #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:
| |
| Elysia is offline | |
| | #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 | |
| | #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:
| |
| Elysia is offline | |
| | #15 |
| and the Hat of Guessing 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |