Thread: Help me fix this function

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    46

    Help me fix this function

    my program keeps hanging up when I try to fill this array of objects of an ADT I created. maybe you guys can dig through this code a little and help me:


    the function: the point is to dig through an input file and fill an array of type TimeCard. I open the file in my main program, but that's not where the problem is. It seems too long and complicated for some reason

    Code:
    void TimeCardList::fill (ifstream& inf)
    {
      const int NUM_EMPLOYEES = 20;
      const int MAX_ID = 5;
      const int MAX_TIME = 3;
      int n, index;
      char id[MAX_ID];
      char time[MAX_TIME];
      char next;
      
      inf.get(next);
    
       for (int i = 0; i < NUM_EMPLOYEES; i++)
       {
         index = 0;
         while (next != ' ')
         {
            if ((isdigit(next)) && (index < MAX_ID))
            {
              id[index] = next;
              index++;
            }
            inf.get(next);
         }
          id[index] = '\0';
          n = atoi (id);
    
          cards[i].set_id(n);
    
          inf.get(next);
          inf.get(next);
    
          //two more while loops with about the same code
          
    
         do               //do loop gets a new line once I've read in
         {
           inf.get(next);
         }while (next != '\n');
    the class: there are two classes, one is for a single time card that has variables for the employee's ID number and hour and minute variables for how long he's worked. the definition looks like this:

    Code:
    class TimeCard
    {
      public:
        TimeCard();
        void set_id(int d);
        void set_hrs(int h);
        void set_min(int m);
        int get_id();
        int get_hrs();
        int get_min();
        void print (ostream& outf);
      private:
        int id, hrs, min;
    };
    here's the other class, it's designed to hold an array of TimeCard objects:

    Code:
    class TimeCardList
    {
      public:
        void fill (ifstream& inf);
        void sort();
        TimeCard search();
        TimeCard cards[100];
    };
    it's not finished, the fill function is the only one I'm working on right now

    the input file looks like this:

    Code:
    1010    10:10
    4011    11:40
    1205    05:20
    I get no compiler or runtime errors, just when I run the program nothing happens and I have to exit it. I don't even know what it's doing while it's hanging there. I know the file is opening because I tested it with a cout.

    help me please
    Last edited by Strait; 01-22-2005 at 03:07 PM.

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I'm not exactly sure what this "get_line()" function is? Maybe I'm just stupid but I didn't know it was part of the std::ifstream class (getline() is, though). I even looked in my reference book (The C++ Standard Library) but it wasn't listed in the index.

    And I would assume get_line changes the read position, and you don't reset the read position before you try to do more reading later.

    I also think you are over-running your array bounds, since you are trying to store 4 characters in the char array 'id' plus the null-terminator.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Taking a shot in the dark here, but I'm looking at this part of your code...
    Code:
    while (next != ' ')
    {
        if ((isdigit(next)) && (index < 3))
        {
           id[index] = next;
           index++;
        }
        inf.get(next);
    }
    Your sentinel is the space character. Does the input file use spaces to separate fields? Are they perhaps Tabs? Should your while loop perhaps be while ( ! isspace (next ) ) so it'll check for all whitespace chars, including tabs etc...
    Last edited by Scribbler; 01-21-2005 at 08:23 PM.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I'm not exactly sure what this "get_line()" function is?
    Echo JaWiB, how that code could compile is beyond me.

  5. #5
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Quote Originally Posted by swoopy
    >I'm not exactly sure what this "get_line()" function is?
    Echo JaWiB, how that code could compile is beyond me.
    The context in which it's used, I would venture a guess (and probably not be too far off) and say it's a user defined member function of TimeCardList.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Scribbler
    The context in which it's used, I would venture a guess (and probably not be too far off) and say it's a user defined member function of TimeCardList.
    But inf is declared as an ifstream:
    >void TimeCardList::fill (ifstream& inf)

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    46
    sorry, that get_line was a typo, the statement is correct. I only have a UNIX compiler so I have to type out code when I want to post it lol

    JaWiB: I agree, I was overrunning the array boundary so I changed it, but you'd think that would give me a run time error or a core dump or something

    Scribbler: the input file uses no tabs, I made it myself. there are exactly 4 spaces between the id number and the hour. I'm thinking I'm just taking too many spaces and it runs out of digits to read too quickly, but you'd also think that would give me a runtime error or a core dump or it would at least get through the function call without freezing

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    46
    okay, I broke it down into steps to redo this function and I got fill working for one id, but I can't get the rest of the array to fill. I don't think it'll be much help unless I post most of my code (over 500 lines so far) so if I need some more help I'll repost. thanks
    Last edited by Strait; 01-22-2005 at 03:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  5. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM