Thread: C program for geting does not work

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    96

    C program for getint () does not work

    Hi,

    I used this program from the Kernighan and Ritchie C book, but it does not work for some reason. Codeblocks starts the CMD and then the CMD just freezes instead of printing the character. Can anyone tell me why this happens please?

    Code:
    void Testing ()
    {
                                                /* Local variables and initialization. */
        int x = 90;
    
        getint (&x);
    }
    
    int getint (int *pn)
    {
                                                /* Local variables and initialization. */
        int c, sign;
    
        while (isspace(c = getch ()))           // Skip the white space. The while is necessary so we
            ;                                   // can skip the white space and continue to the next character.
    
        if (!isdigit (c) && c != EOF && c != '+' && c != '-') {
            ungetch (c);                        // Its not a number.
            printf ("0\n");
            return 0;
        }
        sign = (c == '-') ? -1: 1;
        if (c == '+' || c == '-')
            c = getch ();
        for (*pn = 0; isdigit (c); c = getch ())
            *pn = 10 * *pn + (c - '0');
        *pn *= sign;                            // *pn = *pn * sign
        if (c != EOF)
            ungetch (c);
        printf ("%d\n", c);
        return c;
    }
    Last edited by ArakelTheDragon; 02-26-2020 at 03:56 PM.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Have you implemented getch() and ungetch()?


    Edit: the functions are also provided at the bottom of the first post in this thread: Complex declarations & ?error in Kernighan/Ritchie?
    Last edited by Hodor; 02-26-2020 at 04:15 PM.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Your printf() is wrong as well. Change to:

    Code:
        printf ("%d\n", *pn);
    
        return c;
    
    }
    The reason it's wrong is that *pn contains the converted integer, not the variable 'c'. Read closely what the function returns:

    Returns EOF for end of file, zero if the next input is not a number, and a positive value if the input contains a valid number
    I'm assuming that's not really what you want to print.

    Maybe remove the printf() from inside getint() and change Testing() to:

    Code:
    void Testing ()
    {
                                                /* Local variables and initialization. */
        int x = 90;
     
        if (getint (&x) > 0) {
            printf("%d\n", x);
        }
    
    }

  4. #4
    Registered User
    Join Date
    Dec 2016
    Posts
    96
    I changed it to this, it should be correct:

    Code:
    void Testing ()
    {
                                                /* Local variables and initialization. */
        int x = 90;
    
        printf ("%d.\n", getint (&x));
    }
    
    int getint (int *pn)
    {
                                                /* Local variables and initialization. */
        int c, sign;
    
        while (isspace(c = getch ()))           // Skip the white space. The while is necessary so we
            ;                                   // can skip the white space and continue to the next character.
    
        if (!isdigit (c) && c != EOF && c != '+' && c != '-') {
            ungetch (c);                        // Its not a number.
            return 0;
        }
        sign = (c == '-') ? -1: 1;
        if (c == '+' || c == '-')
            c = getch ();
        for (*pn = 0; isdigit (c); c = getch ())
            *pn = 10 * *pn + (c - '0');
        *pn *= sign;                            // *pn = *pn * sign
        if (c != EOF)
            ungetch (c);
        return c;
    }
    I also added conio.h for getch () and ungetch (). The function is suppose to get the integer from a number. Example: spaceA-90 is fed to the function and it returns the integer.

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    The K&R approach is pretty bad, but using conio.h is arguably worse. At least, as bad as it is, the K&R approach is portable. Try compiling your solution using conio.h using anything but Windows or turbo C and it will fail miserably. That said I think the K&R approach is dumb as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Geting back into coding v.ForgottenHowToDoIt
    By Orange Lozenge in forum General Discussions
    Replies: 0
    Last Post: 10-13-2018, 12:53 PM
  2. geting multiple output from a function
    By meher81 in forum C Programming
    Replies: 4
    Last Post: 04-09-2010, 11:03 AM
  3. Good hosting? For geting and sending positions?
    By azjherben in forum Networking/Device Communication
    Replies: 8
    Last Post: 08-11-2009, 07:27 AM
  4. geting ipv4 and ipv6 interfaces with ioclt
    By wwwnuwan in forum Networking/Device Communication
    Replies: 0
    Last Post: 04-21-2009, 12:38 AM
  5. linked list, geting there
    By anthonye in forum C++ Programming
    Replies: 6
    Last Post: 07-14-2003, 12:55 AM

Tags for this Thread