C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-11-2009, 01:13 PM   #16
msh
Novice
 
Join Date: Jul 2009
Posts: 32
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.
msh is offline   Reply With Quote
Old 07-11-2009, 01:36 PM   #17
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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!
__________________
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, 06:59 PM   #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);
        }
    }
}
EDIT: When I remove the header #include <time.h> the program runs BUT as soon as it runs a Microsoft Window pops up and says "Guessing Game.exe has stopped working".

Any ideas?

Last edited by pmacdonald; 07-11-2009 at 07:10 PM.
pmacdonald is offline   Reply With Quote
Old 07-11-2009, 07:22 PM   #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   Reply With Quote
Old 07-11-2009, 07:29 PM   #20
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by pmacdonald View Post
EDIT:

Alright so I was playing around with the program more and decided to start eliminating lines until the program worked.
So ... why would you do that? If your legs were a little sore, would you just cut bits off until you felt better?

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   Reply With Quote
Old 07-11-2009, 07:51 PM   #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   Reply With Quote
Old 07-11-2009, 08:03 PM   #22
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by pmacdonald View Post
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?
If you don't know why it wasn't working, then you didn't look it up.

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.
and you wouldn't have asked the question.
tabstop is offline   Reply With Quote
Old 07-11-2009, 08:04 PM   #23
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
What we're really wondering, is why this is difficult for you to figure out:
Quote:
error: too few arguments to funtion 'time'
Do you know that 'time' is in your program?
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   Reply With Quote
Old 07-11-2009, 08:11 PM   #24
Guest
 
Sebastiani's Avatar
 
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:
Header File

time.h

Category

Time and Date Routines

Syntax

#include <time.h>
time_t time(time_t *timer);

Description

Gets time of day.

time gives the current time, in seconds, elapsed since 00:00:00 GMT, January 1, 1970, and stores that value in the location pointed to by timer, provided that timer is not a NULL pointer.

Return Value

time returns the elapsed time in seconds.
Sebastiani is offline   Reply With Quote
Old 07-11-2009, 09:06 PM   #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   Reply With Quote
Old 07-11-2009, 09:29 PM   #26
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by pmacdonald View Post
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>.
I've never looked at that book, but whenever I run across a mistake like that I think, "Oh, maybe this was so I'd have to spend extra time thinking about that", which I actually hope there are not so many published writers with that attitude.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 07-11-2009, 11:58 PM   #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   Reply With Quote
Old 07-12-2009, 12:02 AM   #28
Guest
 
Sebastiani's Avatar
 
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   Reply With Quote
Old 07-12-2009, 02:49 AM   #29
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Quote:
Originally Posted by pmacdonald View Post
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>.
If that's true, you may consider a new book.
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:
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-24-2009, 08:25 AM   #30
Registered User
 
Join Date: Jul 2009
Posts: 6
Quote:
Originally Posted by Elysia View Post
If that's true, you may consider a new book.
If you keep finding yourself writing code that works in the book but not with your code, then it's time to switch.
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   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 06:31 AM.


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