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; Originally Posted by pmacdonald msh - mmh, the book hasn't gone over that yet so there should be a way ...

  1. #16
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    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.

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    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!
    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.

  3. #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.

  4. #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.

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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?

  6. #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?

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What we're really wondering, is why this is difficult for you to figure out:
    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.
    Hope is the first step on the road to disappointment.

  9. #24
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> 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:

    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.
    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;
    }

  10. #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>.

  11. #26
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #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).

  13. #28
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> 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.
    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;
    }

  14. #29
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    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.
    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.

  15. #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".

Page 2 of 3 FirstFirst 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