![]() |
| | #16 | |
| Novice Join Date: Jul 2009
Posts: 32
| Quote:
You loop for input until the user gives you something that looks like a digit. | |
| msh is offline | |
| | #17 | ||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Quote:
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!
__________________ 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 | |
| | #18 |
| Registered User Join Date: Jul 2009
Posts: 10
| I actually appreciate you guys not giving me the direct answer. But I've run into another error and I just don't see what I'm doing wrong. I tried to simplify the program without using isdigit (because I don't even see why it's necessary in this program) but I'm still getting an error...but much less errors. error: too few arguments to funtion 'time' Code: #include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int iResponse = 0;
int iAnswer = 0;
srand(time());
iAnswer = (rand() % 10) + 1;
printf("Guessing Game\n");
printf("\nGuess a number from 1 to 10: ");
scanf("%d", &iResponse);
if (iResponse < 1 || iResponse > 10)
{
printf("Enter a number between 1 and 10");
}
else
{
if (iResponse == iAnswer)
{
printf("\nYou guessed right!\n");
}
else
{
printf("\nSorry, you guessed wrong\n");
printf("The correct guess was %d\n", iAnswer);
}
}
}
Any ideas? Last edited by pmacdonald; 07-11-2009 at 07:10 PM. |
| pmacdonald is offline | |
| | #19 |
| Registered User Join Date: Jul 2009
Posts: 10
| EDIT: Alright so I was playing around with the program more and decided to start eliminating lines until the program worked. When I took off the following line: srand(time()); the program ran. But as you guys know now the answer isn't really random. It should still produce different numbers from 1 to 10 because of the following: iAnswer = (rand() % 10) + 1; but it doesn't even do that...each time I run the program iAnswer = 2. Any help would be greatly appreciated. |
| pmacdonald is offline | |
| | #20 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
If you don't know what srand does, why is it in your program? And why didn't you look up what srand did before you cut it out? | |
| tabstop is offline | |
| | #21 |
| Registered User Join Date: Jul 2009
Posts: 10
| I did look it up...and I know what it does. But for some reason it wasn't working in the program. Why don't you tell me why? |
| pmacdonald is offline | |
| | #22 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
Because, if you had looked it up, you would have seen this: Code: time_t time(time_t *tloc);
The time() function returns the value of time in seconds since 0 hours, 0
minutes, 0 seconds, January 1, 1970, Coordinated Universal Time, without
including leap seconds. If an error occurs, time() returns the value
(time_t)-1.
The return value is also stored in *tloc, provided that tloc is non-null.
| |
| tabstop is offline | |
| | #23 | |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| What we're really wondering, is why this is difficult for you to figure out: Quote:
Do you know what an 'argument' is? Do you know what 'too few' means? Which part is confusing? Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? | |
| quzah is offline | |
| | #24 | |
| Guest Join Date: Aug 2001
Posts: 4,923
| >> I did look it up...and I know what it does. But for some reason it wasn't working in the program. Why don't you tell me why? One rule of programming is to be sure to read the documentation on any function you plan to use. You'll need to know what arguments it takes, the constraints on the arguments, and what it returns. Had you looked up the docs on time you would have found that it takes a pointer to a time_t. Thus the error "too few arguments to function 'time'". Here's what my docs say: Quote:
| |
| Sebastiani is offline | |
| | #25 |
| Registered User Join Date: Jul 2009
Posts: 10
| What I don't understand is why in this book do they not give you all the correct information before asking you a question where you need said information. Going off the examples in the book the program I wrote should've worked...they didn't use any other header except #include <stdio.h> and #include <ctype.h>. |
| pmacdonald is offline | |
| | #26 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is offline | |
| | #27 |
| Registered User Join Date: Jul 2009
Posts: 10
| I'm going to start over with this program and start with writing out the pseudo code. I'll get back to you guys with my new results tomorrow (well I guess that would be later today). |
| pmacdonald is offline | |
| | #28 |
| Guest Join Date: Aug 2001
Posts: 4,923
| >> I'm going to start over with this program and start with writing out the pseudo code. Always a good idea. It usually helps clarify your ideas, anyway. Good luck. |
| Sebastiani is offline | |
| | #29 | ||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Quote:
If you keep finding yourself writing code that works in the book but not with your code, then it's time to switch.
__________________ 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 | |
| | #30 |
| Registered User Join Date: Jul 2009
Posts: 6
| Sorry for digging up an old thread, but I had the same problem as the OP. This book indeed sucks, I wasted a day on the isDigit, rand and time functions! Switching to "C for Dummies". |
| Nimbuz 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 |