Thread: Problem with Output

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    78

    Problem with Output

    Thank you for taking the time to help me! I am working on a program that keeps information about staff members, such as volunteers and employees. The problem I am having is that when I run the program (everything compiles fine), I enter a member, and then when I tell the program to display the members, the member list is not printed out. It prints the main menu prompt infinitely. Also, when I add a member and then compute the pay, the program displays "pay computed" then the main menu prompt, and repeats this pattern infinitely. Also, when I add a member and then search for the member, the program has a windows error and then terminates. So, I think something is wrong with my add function, or my StaffMemberParser function, which actually creates the member that is added to the list.

    I have attached most of my files so that if you want to, you can run it and see what's going on for yourself. The last file is attached in the next post. I am also putting the file I think is the problem, which is StaffMemberParser.h, below. Can someone help me figure out why its not doing what I want it to?

    Thank you very much in advance for any help you can provide!!

    Code:
    // StaffMemberParser.h
    
    #include <stdio.h>
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <stdexcept>
    #include "StaffMember.h"
    #include "FullTimeEmployee.h"
    #include "HourlyEmployee.h"
    #include "Volunteer.h"
       
    // protections 
    #ifndef StaffMemberParser_H
    #define StaffMemberParser_H
    
    using namespace std;
    
    class BadConversion : public std::runtime_error 
    {
     public:
       BadConversion(const std::string& s)
         : std::runtime_error(s)
         { }
    };
     
    inline double convertToDouble(const std::string& s)
    {
       std::istringstream i(s);
       double x;
       if (!(i >> x))
         throw BadConversion("convertToDouble(\"" + s + "\")");
       return x;
    } 
    
    inline int convertToInt(const std::string& s)
    {
       std::istringstream i(s);
       int x;
       if (!(i >> x))
         throw BadConversion("convertToInt(\"" + s + "\")");
       return x;
    } 
    
    class StaffMemberParser
    {
          public:
          static StaffMember * parseStringToMember(string lineToParse)
          {
                 const char *ptr = lineToParse.c_str();
                 char field [100];
                 int n, i = 0, hoursWorked;
                 string type, firstName, lastName, employeeId;
                 double rate, bonus;
                 StaffMember * newMember;
                 
                 while ( sscanf(ptr, "%31[^/]%n", field, &n) == 1 )
                 {
                      switch (i++)
                      {
                             case 0: type = field; break;
                             case 1: firstName = field; break;
                             case 2: lastName = field; break;
                             case 3: employeeId = field; break;
                             case 4:
                                  if ( type == "Volunteer" )
                                  {
                                       newMember = new Volunteer(firstName, lastName, employeeId);
                                       return newMember;
                                  }
                                  else if ( type == "HourlyEmployee" )
                                  {
                                       rate = convertToDouble(field);
                                  }
                                  else if ( type == "FullTimeEmployee" )
                                  {
                                       rate = convertToDouble(field);
                                  }
                                  break;
                             case 5:
                                  if ( type == "Volunteer" )
                                  {
                                       // nothing to do
                                  }
                                  else if ( type == "HourlyEmployee" )
                                  {
                                       hoursWorked = convertToInt(field);
                                       newMember = new HourlyEmployee(firstName, lastName, employeeId, rate, hoursWorked);
                                       return newMember;
                                  }
                                  else if ( type == "FullTimeEmployee" )
                                  {
                                       bonus = convertToDouble(field);
                                       newMember = new FullTimeEmployee(firstName, lastName, employeeId, rate, bonus);
                                       return newMember; 
                                  }
                                  break;
                             default: break;
                      }
                      ptr += n;
                      if ( *ptr != '/' )
                      {
                           break;  // didn't find an expected delimiter
                      }
                      while ( *ptr == '/' )      
                      { 
                           ++ptr;  // skip the delimiter     
                      }
                 }  
          }
    };
    
    #endif

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    Here is the last file.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If anything goes wrong with your parsestring function, then you return an invalid pointer and cheerfully put it in your vector. For instance, if I typed this
    Code:
    Volunteer/John/Smith/1
    I was so screwed, as the fifth go-round with sscanf failed (since there wasn't another field there to read), hence the loop stopped before the new Volunteer was constructed, hence nothing was returned. If I added a bogus field to the end, everything worked.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    Oh, okay, that makes sense. ...but how do I fix it? I tried moving the return statement to the fifth case, and commenting it out of the fourth case, but that didn't work either. How can I make it exit the cases if the type is a volunteer? Thank you for the help again, tabstop!

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So if case 4 is too late, then why move it to case 5? The last case that happens when you have a volunteer is case 3....

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    Oh...I thought you meant that it needed to be expanded so that it crossed all of the cases. But what you actually meant was that it doesn't even get to the fourth, because it can't due to what you are entering. I think I get it now. I'm sorry for being so slow about this stuff.

    Okay, let's see...I moved the creation of the new Volunteer and the return statement to case 3, and just put a comment in cases 4 and 5 for type == volunteer saying that there is nothing to do. But it still must not be right, because when I add a volunteer and then go to print the list, it prints the main menu prompt infinitely. And I see what you are saying about it working otherwise, but I don't know what to do about that.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It works here, or at least what I typed in worked here. What did you change.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    Quote Originally Posted by tabstop View Post
    It works here, or at least what I typed in worked here. What did you change.
    Here are my cases, which are the only part I changed of the StaffMemberParser.h file:

    Code:
                             case 0: type = field; break;
                             case 1: firstName = field; break;
                             case 2: lastName = field; break;
                             case 3: employeeId = field;
                                  if ( type == "Volunteer" ) 
                                  {
                                       newMember = new Volunteer(firstName, lastName, employeeId);
                                       return newMember;
                                  }
                                  break;
                             case 4:
                                  if ( type == "Volunteer" )
                                  {
                                       // nothing to do
                                  }
                                  else if ( type == "HourlyEmployee" )
                                  {
                                       rate = convertToDouble(field);
                                  }
                                  else if ( type == "FullTimeEmployee" )
                                  {
                                       rate = convertToDouble(field);
                                  }
                                  break;
                             case 5:
                                  if ( type == "Volunteer" )
                                  {
                                       // nothing to do
                                  }
                                  else if ( type == "HourlyEmployee" )
                                  {
                                       hoursWorked = convertToInt(field);
                                       newMember = new HourlyEmployee(firstName, lastName, employeeId, rate, hoursWorked);
                                       return newMember;
                                  }
                                  else if ( type == "FullTimeEmployee" )
                                  {
                                       bonus = convertToDouble(field);
                                       newMember = new FullTimeEmployee(firstName, lastName, employeeId, rate, bonus);
                                       return newMember; 
                                  }
                                  break;
                             default: break;
    Last edited by vileoxidation; 10-10-2009 at 10:24 PM.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have a strange notion of "indefinitely":
    Code:
    $ ./assign7
    Choice		Action
    ------		------
    A		Add Member
    C		Compute Pay
    D		Search for Member
    L		List Members
    Q		Quit
    ?		Display Help
    
    What action would you like to perform?
    A
    Please enter a member information to add:
    Volunteer/John/Smith/1
    What action would you like to perform?
    L
    
    Volunteer:
    
    First name:		John
    Last name:		Smith
    Member ID:		1
    Pay:			$0
    
    What action would you like to perform?
    Q

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    Yeah, but I don't get that. I get the screen shot in the attached picture...how strange.

    Maybe I'll try downloading my files from here and see if they run differently.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I can get that on Windows if I don't change your staffmemberparser to return early. (On BSD/Mac it just threw an exception.) If I do, well:

    (EDIT: Oh, and computePay doesn't seem to work right for people who actually get paid, but that's for later, maybe.)

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    Yeah, its really strange. I submitted my files to the online compiler we use for submission of assignments, and it worked fine. So its just stuck on my computer and won't run the new version. That is really odd. Maybe I need to restart to clear things out...

    And when I submit to the online compiler, it computes the rate correctly. It's just the bonus + rate for full time, and rate*hours for hourly, and for all of the test cases it shows up right, for me at least.

    Hmm. I don't know what is going on with my compiler and computer, but I think things are in a pretty good state for now. Thank you for the help, tabstop!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my output
    By Dogmasur in forum C Programming
    Replies: 17
    Last Post: 08-07-2008, 08:07 PM
  2. output from bank type problem
    By IzaakF in forum C Programming
    Replies: 2
    Last Post: 09-04-2002, 06:42 PM
  3. String Output Problem
    By Yin in forum C++ Programming
    Replies: 3
    Last Post: 03-14-2002, 07:36 AM
  4. Problem with output
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 12-11-2001, 08:32 PM