I'm missing something obvious

This is a discussion on I'm missing something obvious within the C Programming forums, part of the General Programming Boards category; I'm trying to complete an excercise to learn unix system calls and one of the functions in the program was ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    31

    I'm missing something obvious

    I'm trying to complete an excercise to learn unix system calls and one of the functions in the program was giving me only partial output. So I wrote a test program that is almost identical to the one linked with the rest of the program that works fine. I've gone over this for a solid day. Anyway, here's the code to what works:
    Code:
    #include "hotel.h"
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define LNAME 22
    #define FNAME 19
    
    char buf[NLINES];
    
    void gname(int, int *);
    
    int
    main()
    {
        int index = 0;
    
        printf("Enter last name: ");
        gname(LNAME, &index);
        printf("Enter first  name: ");
        gname(FNAME, &index);
        printf("%s", buf);
    }
    
    gname(int i, int *index)
    {
        int j, count = *index;
        char c;
    
    
        if (i == LNAME)
            j = i - 2;
        else if (i == FNAME)
            j = (LNAME + FNAME) - 1;
    
        while ( (c = getchar()) != '\n' && c != EOF && count < j) {
            buf[count] = c;
            count++;
        }
    
        if (i == LNAME) {
            buf[count] = ',';
            buf[++count] = ' ';
            *index = ++count;
        }
    
        if (i == FNAME) {
            for (count; count < j; ++count)
                buf[count] = ' ';
            buf[count] = '\n';
        }
    }
    
    gname(int i, int *index)
    {
        int j, count = *index;
        char c;
    
    
        if (i == LNAME)
            j = i - 2;
        else if (i == FNAME)
            j = (LNAME + FNAME) - 1;
    
        while ( (c = getchar()) != '\n' && c != EOF && count < j) {
            buf[count] = c;
            count++;
        }
    
        if (i == LNAME) {
            buf[count] = ',';
            buf[++count] = ' ';
            *index = ++count;
        }
    
        if (i == FNAME) {
            for (count; count < j; ++count)
                buf[count] = ' ';
            buf[count] = '\n';
        }
    }
    This works just fine.
    it will output and print the following
    Enter last name: ted
    Enter first name: danson
    danson, ted

    The hotel.h file just contains two extern variables:
    extern int fd; /*a file descriptor for global use*/
    #define NLINES 41
    #define MAXROWS 10
    extern char buf[NLINES];

    Then I copy this file to the directory with the rest of the files, make a few modifacations and don't get what I expect.
    Code:
    #include "hotel.h"
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <stdbool.h>
    
    #define LNAME 22
    #define FNAME 19
    
    char buf[NLINES];
    int fd;
    
    void gname(int, int *);
    
    bool
    addguest(int *roomnum)
    {
        int room, index = 0;
        off_t offset;
    
        if (!findfree(&room))
            return false;
        else
            *roomnum = room;
    
        offset = (room - 1) * NLINES;
        if (lseek(fd, offset, SEEK_SET) < 0) {
      perror("problem with lseek in addguest");
            exit(-1);
        } 
    
        printf("Enter last name: ");
        gname(LNAME, &index);
        clearstdin();
        printf("Enter first  name: ");
        gname(FNAME, &index);
        printf("%s", buf);
    }
    
    void
    gname(int i, int *index)
    {
        int j, count = *index;
        char c;
    
    
        if (i == LNAME)
            j = i - 2;
        else if (i == FNAME)
            j = (LNAME + FNAME) - 1;
    
        while ( (c = getchar()) != '\n' && c != EOF && count < j) {
            buf[count] = c;
            count++;
        }
    
        if (i == LNAME) {
            buf[count] = ',';
            buf[++count] = ' ';
            *index = ++count;
        }
    
        if (i == FNAME) {
            for (count; count < j; ++count)
                buf[count] = ' ';
            buf[count] = '\n';
        }
    }
    this will prompt the same, with the use of the clearstdin function, otherwise it will output
    Enter last name: Enter first name:
    the clearstdin function is:
    Code:
    void
    clearstdin()
    {
        char c;
        while ((c = getchar()) != NULL && c != '\n' && c != EOF)
    }
    so with this function I get the proper prompts but the output is:
    , ted
    Maybe it's the clearstdin function that's causing the problem but if it is I don't see how.
    Thanks in advance.
    Last edited by crash88; 07-02-2006 at 12:18 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    Since getchar() returns an int, you may want to have int c; instead in clearstdin()
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    I don't suppose it occurred to you to try it without calling clearstdin();
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    31
    of course it occured to me. I explained that withought the use of clearstdin I get the output
    Enter last name: Enter first name:
    The both go on the same line so stdin obviously needs a douche.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    31
    I changed the char to an int and nothing changed. Thanks for the advice.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    Well lets see, the original worked fine without it.

    With it, you're missing half your input (the whole of the 2nd line)

    If you really want proof, try printing out each character that clearstdin() gets rid of, and see if it contains "danson".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    31
    too bad it wasn't stdin. the output was this when I tried writing each character to the screen
    crash@venus::~/programs/usp/ch2$ ./frontdesk file.txt


    1 getoccupier
    2 freeroom
    3 addguest
    4 findfree

    choice: 3
    Enter last name: danson
    10097110115111110Enter first name: ted
    Guest was added to room 1.
    Return to continue:


    1 getoccupier
    2 freeroom
    3 addguest
    4 findfree

    choice: 1
    What room: 1
    , ted
    is in room 1.
    Return to continue:

    even looking in file.txt it is the same as what's outputted to the screen.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by crash88
    too bad it wasn't stdin. the output was this when I tried writing each character to the screen
    crash@venus::~/programs/usp/ch2$ ./frontdesk file.txt


    1 getoccupier
    2 freeroom
    3 addguest
    4 findfree

    choice: 3
    Enter last name: danson
    10097110115111110Enter first name: ted
    Guest was added to room 1.
    Return to continue:


    1 getoccupier
    2 freeroom
    3 addguest
    4 findfree

    choice: 1
    What room: 1
    , ted
    is in room 1.
    Return to continue:

    even looking in file.txt it is the same as what's outputted to the screen.
    What the ........ is that? Oh, I know what it is, it's just what Salem said it was:
    100 = d
    97 = a
    110 = n
    115 = s
    111 = o
    110 = n

    You should probably actually learn to debug instead of just crying about everything "not working" and everyone being wrong when they're not.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 02:29 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 09:26 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 11:52 PM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21