Hello, and thank you for taking the time to help me with this! 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 members, and then when I tell the program to display the members, nothing prints out. It just prompts for the next input. Is this an error in my main function, or in my StaffMemberParser function, which is called from the main function and used to add a member to the list? I am including both my main function, called Assignment7.cpp, and the StaffMemberParser.h file which I think could be the problem.

Can someone help me figure out why its not doing what I want it to?

Here is my code:

// Assignment #: 7
//         Name: 
// EmailAddress:
//  Description: It displays a menu of choices
//               (add volunteers, full time employees, or hourly employees,
//               compute their pay, search a member, list members,
//               quit, display menu) to a user.
//               Then it performs the chosen task. It will keep asking a user 
//               to enter the next choice until the choice of 'Q' (Quit) is
//               entered.

#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
#include "StaffMember.h"
#include "StaffMemberParser.h"

using namespace std;

void printMenu();

int main()
{
	char input1;
	string inputInfo;
	bool operation;

	vector<StaffMember *> memberList;


	printMenu();     // print out menu


	do
	{
		cout << "What action would you like to perform?" << endl;
		cin >> input1;

		switch (input1)
		{
		case 'A':   //Add Member
			cout << "Please enter a member information to add:\n";
			cin >> inputInfo;
			StaffMemberParser::parseStringToMember(inputInfo); 
			break;
		case 'C':   //Compute Pay
		    for (int j=0; j < memberList.size(); j++)
			{
                 memberList.at(j)->computePay();
		    }
            cout << "pay computed\n";
			break;
		case 'D':   //Search for Member
			cout << "Please enter a memberID to search:\n";
			cin >> inputInfo;
			operation = false;
			
            for (int j=0; j < memberList.size(); j++)
			{
			     if (inputInfo == memberList.at(j)->getMemberId())
			     {
                       operation = true;
                 }
                 else
                 {
                       operation = false;
                 }
		    }
			if (operation == true)
				cout << "member found\n";
			else
				cout << "member not found\n";
			break;
		case 'L':   //List Members
             if (sizeof(memberList) == 0)
             {
                 cout << "no member\n" << endl;
             }
             else 
             {
                 for (int j=0; j < memberList.size(); j++)
			     {
				     memberList.at(j)->printInfo();
		         }   
             }
			 break;
		case 'Q':   //Quit
			for (int j=0; j < memberList.size(); j++)
			{
				delete memberList.at(j);
			}
			break;
		case '?':   //Display Menu
			printMenu();
			break;
		default:
			cout << "Unknown action\n";
			break;
		}

	} while (input1 != 'Q' && input1 != 'q'); // stop the loop when Q is read

}
// 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

Since you haven't provided the printInfo function, we can't see whether anything is wrong with it. My first advice would be to just have printInfo () display "Hello World" and return. If you don't get that output, then it's not even being called and that narrows down your problem. If it does display that, then you either have a bad printInfo () function or you have bad data. If looking at your printInfo () function doesn't make it obvious which it is, then skip the vectors and all that stuff, create a simple hard-code object in main, then call printInfo () on that object and see if it prints correctly. That should narrow it down more.

But we can't run what you've posted.

Since you haven't provided the printInfo function, we can't see whether anything is wrong with it. My first advice would be to just have printInfo () display "Hello World" and return. If you don't get that output, then it's not even being called and that narrows down your problem. If it does display that, then you either have a bad printInfo () function or you have bad data. If looking at your printInfo () function doesn't make it obvious which it is, then skip the vectors and all that stuff, create a simple hard-code object in main, then call printInfo () on that object and see if it prints correctly. That should narrow it down more.

But we can't run what you've posted.

Well, the printInfo function varies from child class to child class (the Volunteer, hourlyEmployee, and fullTimeEmployee files are all children of the staffMember class), but I can post all of those files if that is what is needed to figure out the problem. Here are the other four files:

// StaffMember.h
// Assignment #: 7
// Name:
// Email Address: 

// protections 
#ifndef StaffMember_H
#define StaffMember_H

using namespace std;

class StaffMember
{
      // protected variables
      protected:
        string firstName;
        string lastName;
        string memberId;
        double pay;
                
      // public functions
      public: 
        StaffMember(string &string1, string &string2, string &string3)
        {
               firstName = string1;
               lastName = string2;
               memberId = string3;
               pay = 0.0;
        }
        
        string getMemberId()
        {      return memberId;   }
        
        virtual void computePay() = 0;
        
        virtual void printInfo()
        {       cout << "\nFirst name:\t\t" << firstName << "\nLast name:\t\t" << lastName << "\nMember ID:\t\t" << memberId << "\nPay:\t\t\t$" << pay << "\n" << endl;      }
};

#endif
// FullTimeEmployee.h
// Assignment #: 7
// Name:
// Email Address:

// protections 
#ifndef FullTimeEmployee_H
#define FullTimeEmployee_H

using namespace std;

class FullTimeEmployee: public StaffMember
{
      // private variables
      private:
        double rate;
        double bonus;
                
      // public functions
      public:               
        FullTimeEmployee(string string1, string string2, string string3, double double1, double double2): StaffMember(string1, string2, string3)
        {
               rate = double1;
               bonus = double2;
        }
        
        virtual void computePay()
        {
               pay = rate + bonus; 
        }
        
        virtual void printInfo()
        {       
                cout << "\nFull Time Employee:" << endl;     
                StaffMember::printInfo();
        }               
};

#endif
// HourlyEmployee.h
// Assignment #: 7
// Name: 
// Email Address: 
         
// protections 
#ifndef HourlyEmployee_H
#define HourlyEmployee_H

using namespace std;

class HourlyEmployee: public StaffMember
{
      // private variables
      private:
        double rate;
        int hoursWorked;
                
      // public functions
      public: 
        HourlyEmployee(string string1, string string2, string string3, double double1, int int1): StaffMember(string1, string2, string3)
        {
               // StaffMember(string1, string2, string3);
               rate = double1;
               hoursWorked = int1;
        }
        
        virtual void computePay()
        {
               pay = rate*hoursWorked; 
        }
        
        virtual void printInfo()
        {
                cout << "\nHourly Employee:" << endl;     
                StaffMember::printInfo();                 
        }
};

#endif
// Volunteer.h
// Assignment #: 7
// Name: 
// Email Address: 

// protections 
#ifndef Volunteer_H
#define Volunteer_H

using namespace std;

class Volunteer: public StaffMember
{
      // public functions
      public: 
         Volunteer(string string1, string string2, string string3): StaffMember(string1, string2, string3)
         {
               // StaffMember(string1, string2, string3);
         }
         
         virtual void computePay()
         {
               double pay = 0.0;
         }
         
        virtual void printInfo()
        {
                cout << "\nVolunteer:" << endl;     
                StaffMember::printInfo();                 
        }
};

#endif

Take a look at the little code snippet I wrote below and you will see the freopen("CON", "w", stdout); which outputs data to a screen so try inplementing it into your code...

#include <fstream>
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
freopen("CON", "w", stdout);
}

Adding to my other post, I did some testing, and I think it is not a problem with my printInfo functions. I think it is a problem with my add member case, because when I search for a member that I have previously entered, it says the member cannot be found. So I think it is not adding to the list. But I am unsure what to do about that.

I see a function that returns type StaffMember*.

static StaffMember * parseStringToMember(string lineToParse)

I see a line that calls this function in line 45 of your driver program, so it may as well be a void function. I also see no object being added to memberList in lines 42 to 46. Make sure there's actually something in memberList.


You need to stick some debugging statements and/or breakpoints in there to see how far you get. Make sure you actually get a call to printInfo. Make sure memberList isn't empty.

Edited 7 Years Ago by VernonDozier: n/a

Adding to my other post, I did some testing, and I think it is not a problem with my printInfo functions. I think it is a problem with my add member case, because when I search for a member that I have previously entered, it says the member cannot be found. So I think it is not adding to the list. But I am unsure what to do about that.

See my last post. You need to do something with the return value of the function you call that creates and returns the new staff member.

StaffMember * newStaffMember = StaffMemberParser::parseStringToMember(inputInfo);
// now add newStaffMember to memberList.

I see a function that returns type StaffMember*.

static StaffMember * parseStringToMember(string lineToParse)

I see a line that calls this function in line 45 of your driver program, so it may as well be a void function. I also see no object being added to memberList in lines 42 to 46. Make sure there's actually something in memberList.


You need to stick some debugging statements and/or breakpoints in there to see how far you get. Make sure you actually get a call to printInfo. Make sure memberList isn't empty.

Gah. I'm really going to show my inexperience here. This is only my second C++ program, so please bear with me!

Okay, using the code in your second post, I am still confused. How do I add the member to the memberList? I am totally unsure about how to do this part of it - as I said, here my inexperience really shows.

Also, I don't know much about debugging. I use Dev-C++. Is there a good tutorial on debugging within Dev-C++ I could read? I know I should be using this feature, but I just haven't learned how yet.

Edit: I put that line in my code, and now I am getting an error:

48 Assignment7.cpp jump to case label
45 Assignment7.cpp crosses initialization of `StaffMember*newStaffMember'

Edited 7 Years Ago by vileoxidation: n/a

This is your SECOND C++ program and you're using classes, vectors, polymorphism, switch statements, etc.!!! What happened to "Hello World" as the first and "Hello <your name here>" as the second? For a second program, you're doing fantastic. But this should not be your second program. This should be your 30th program.

That said, I don't have a good Dev C++ debugging program handy, but google provides some good links. You need to build a project and then select "Debug". You can then stick some breakpoints in there, look at local variable values, etc. But again, I don't have a good tutorial handy, but there are lots of them.

Here's a small skeleton you can work off of. You add to a vector using push_back. You access a vector's size using the function of that name. You access an element using the at function or the [] operator. i don't know exactly where you put the statement that got you the jump error. But look at the skeleton and see if it helps.

#include <iostream>
#include <string>
#include <vector>
using namespace std;


class Student
{
   public:
          string firstName;
          string lastName;
          
          
          Student (string fname, string lname)
          {
              firstName = fname;
              lastName = lname;
          }
          
          static Student* ReadStudent ()
          {
              string fname, lname;
              cout << "Enter student's first name : ";
              cin >> fname;
              cout << "Enter student's last name : ";
              cin >> lname;
              Student* newStudent = new Student (fname, lname);
              return newStudent;              
          }
          
          void Print ()
          {
               cout << firstName << " " << lastName << endl;
          }
};


int main ()
{
   vector <Student*> students;
   
   while (true)
   {
         cout << "Enter 1 to add a student, 2 to view students, 3 to exit : ";
         int choice;
         cin >> choice;
         
         switch (choice)
         {
             case 1:
                  Student* newStudent = Student::ReadStudent ();
                  students.push_back (newStudent);
                  break;
             case 2:
                  int numStudents = students.size ();
                  for (int i = 0; i < numStudents; i++)
                      students.at (i)->Print ();
                  break;
             case 3:
                  cout << "Bye!\n";
                  return 0;
                  break;
         }
   } 
}

Hello, and thank you for taking the time to help me with this! 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 members, and then when I tell the program to display the members, nothing prints out. It just prompts for the next input. Is this an error in my main function, or in my StaffMemberParser function, which is called from the main function and used to add a member to the list? I am including both my main function, called Assignment7.cpp, and the StaffMemberParser.h file which I think could be the problem.

Can someone help me figure out why its not doing what I want it to?

Here is my code:

// Assignment #: 7
//         Name: 
// EmailAddress:
//  Description: It displays a menu of choices
//               (add volunteers, full time employees, or hourly employees,
//               compute their pay, search a member, list members,
//               quit, display menu) to a user.
//               Then it performs the chosen task. It will keep asking a user 
//               to enter the next choice until the choice of 'Q' (Quit) is
//               entered.

#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
#include "StaffMember.h"
#include "StaffMemberParser.h"

using namespace std;

void printMenu();

int main()
{
	char input1;
	string inputInfo;
	bool operation;

	vector<StaffMember *> memberList;


	printMenu();     // print out menu


	do
	{
		cout << "What action would you like to perform?" << endl;
		cin >> input1;

		switch (input1)
		{
		case 'A':   //Add Member
			cout << "Please enter a member information to add:\n";
			cin >> inputInfo;
			StaffMemberParser::parseStringToMember(inputInfo); 
			break;
		case 'C':   //Compute Pay
		    for (int j=0; j < memberList.size(); j++)
			{
                 memberList.at(j)->computePay();
		    }
            cout << "pay computed\n";
			break;
		case 'D':   //Search for Member
			cout << "Please enter a memberID to search:\n";
			cin >> inputInfo;
			operation = false;
			
            for (int j=0; j < memberList.size(); j++)
			{
			     if (inputInfo == memberList.at(j)->getMemberId())
			     {
                       operation = true;
                 }
                 else
                 {
                       operation = false;
                 }
		    }
			if (operation == true)
				cout << "member found\n";
			else
				cout << "member not found\n";
			break;
		case 'L':   //List Members
             if (sizeof(memberList) == 0)
             {
                 cout << "no member\n" << endl;
             }
             else 
             {
                 for (int j=0; j < memberList.size(); j++)
			     {
				     memberList.at(j)->printInfo();
		         }   
             }
			 break;
		case 'Q':   //Quit
			for (int j=0; j < memberList.size(); j++)
			{
				delete memberList.at(j);
			}
			break;
		case '?':   //Display Menu
			printMenu();
			break;
		default:
			cout << "Unknown action\n";
			break;
		}

	} while (input1 != 'Q' && input1 != 'q'); // stop the loop when Q is read

}
// 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

u use do while statement which is not appropriate for it ......plz change control statement problum is it that u use it n it always doing work never end okkkk change its limit n then ans ..is it solve or not

This is your SECOND C++ program and you're using classes, vectors, polymorphism, switch statements, etc.!!! What happened to "Hello World" as the first and "Hello <your name here>" as the second? For a second program, you're doing fantastic. But this should not be your second program. This should be your 30th program.

That said, I don't have a good Dev C++ debugging program handy, but google provides some good links. You need to build a project and then select "Debug". You can then stick some breakpoints in there, look at local variable values, etc. But again, I don't have a good tutorial handy, but there are lots of them.

Here's a small skeleton you can work off of. You add to a vector using push_back. You access a vector's size using the function of that name. You access an element using the at function or the [] operator. i don't know exactly where you put the statement that got you the jump error. But look at the skeleton and see if it helps.

Yeah, I am in a class that covers multiple languages, and we did C first, so I have experience writing code, but not with the specifics of C++. But yeah, this is the second program we have written in C++, so I am still a bit confused.

Okay, that code really helps. My code now compiles, but when I run it I am having a problem. I enter a user, and then try to display the user list, and the program suddenly prints the prompt for the next action over and over and over again, doing it infinitely. And, if I enter a user and then try to search for the user, the program has a windows error and I have to terminate the program.

My main function is posted below so that you can see what is going on. Thanks for the help, Vernon!! Oh, I fixed the jump error by putting brackets around the case that was causing it so that the variable can't be accessed outside of that case.

// Assignment #: 7
//         Name: 
// EmailAddress:
//  Description: It displays a menu of choices
//               (add volunteers, full time employees, or hourly employees,
//               compute their pay, search a member, list members,
//               quit, display menu) to a user.
//               Then it performs the chosen task. It will keep asking a user 
//               to enter the next choice until the choice of 'Q' (Quit) is
//               entered.

#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
#include "StaffMember.h"
#include "StaffMemberParser.h"

using namespace std;

void printMenu();

int main()
{
	char input1;
	string inputInfo;
	bool operation;

	vector<StaffMember *> memberList;


	printMenu();     // print out menu


	do
	{
		cout << "What action would you like to perform?" << endl;
		cin >> input1;

		switch (input1)
		{
		case 'A':   //Add Member
		{
			cout << "Please enter a member information to add:\n";
			cin >> inputInfo;
			StaffMember * newStaffMember = StaffMemberParser::parseStringToMember(inputInfo);
            memberList.push_back(newStaffMember); 
			break;
        }
		case 'C':   //Compute Pay
		    for (int j=0; j < memberList.size(); j++)
			{
                 memberList.at(j)->computePay();
		    }
            cout << "pay computed\n";
			break;
		case 'D':   //Search for Member
			cout << "Please enter a memberID to search:\n";
			cin >> inputInfo;
			operation = false;
			
            for (int j=0; j < memberList.size(); j++)
			{
			     if (inputInfo == memberList.at(j)->getMemberId())
			     {
                       operation = true;
                 }
                 else
                 {
                       operation = false;
                 }
		    }
			if (operation == true)
				cout << "member found\n";
			else
				cout << "member not found\n";
			break;
		case 'L':   //List Members
             if (memberList.size() == 0)
             {
                 cout << "no member\n" << endl;
             }
             else 
             {
                 for (int j=0; j < memberList.size(); j++)
			     {
				     memberList.at(j)->printInfo();
		         }   
             }
			 break;
		case 'Q':   //Quit
			for (int j=0; j < memberList.size(); j++)
			{
				delete memberList.at(j);
			}
			break;
		case '?':   //Display Menu
			printMenu();
			break;
		default:
			cout << "Unknown action\n";
			break;
		}

	} while (input1 != 'Q' && input1 != 'q'); // stop the loop when Q is read

}

/** The method printMenu displays the menu to a user **/
void printMenu()
{
	cout << "Choice\t\tAction\n"; 
	cout << "------\t\t------\n"; 
	cout << "A\t\tAdd Member\n"; 
	cout << "C\t\tCompute Pay\n"; 
	cout << "D\t\tSearch for Member\n"; 
	cout << "L\t\tList Members\n"; 
	cout << "Q\t\tQuit\n"; 
	cout << "?\t\tDisplay Help\n\n";
}

With programs like this, it's useful to zip up all of the .h and .cpp files and upload them along with your post. That way anyone who wants to run it can download, unzip, compile, and see exactly what's going on. It saves a lot of copying and pasting, and we know we're running the exact same thing as you are.

You have more than one "prompt", so please be more specific as to where this is happening. I'm assuming you're referring to the main menu prompt. Your description suggests the possibility that cin is in a "failed" state. There are a variety of ways this can happen. An easy way to check is to put this before line 37.

if (cin.fail ())
{
    cout << "cin is in a failed state.  Terminating program.\n";
    exit (1);
}

If that line displays, your next step is to hunt down where exactly this happens. You can sprinkle that line around in different spots, changing the message slightly each time till you come across exactly where this happens. You have C code in your function that accepts input. I'm not sure what happens to the cin flag if/when functions like scanf and its derivatives fail. It may set the cin flag, it may not. I don't know. Anyway, again, if the cin flag is in a failed state AFTER you return from your employee input function, but was good BEFORE it was called, there's your culprit. You can hard code an employee in that function, and return it, thus bypassing the sscanf call. If that results in the cin flag NOT being put into a failed state, then you've narrowed it down further.

Edited 7 Years Ago by VernonDozier: n/a

With programs like this, it's useful to zip up all of the .h and .cpp files and upload them along with your post. That way anyone who wants to run it can download, unzip, compile, and see exactly what's going on. It saves a lot of copying and pasting, and we know we're running the exact same thing as you are.

You have more than one "prompt", so please be more specific as to where this is happening. I'm assuming you're referring to the main menu prompt. Your description suggests the possibility that cin is in a "failed" state. There are a variety of ways this can happen. An easy way to check is to put this before line 37.

Anyway, again, if the cin flag is in a failed state AFTER you return from your employee input function, but was good BEFORE it was called, there's your culprit. You can hard code an employee in that function, and return it, thus bypassing the sscanf call.

Okay, I will zip and upload my files for you guys now. And yes, you are totally correct in assuming that it is the main menu prompt that I am talking about. I will try to be more specific next time. :-)

When I put that check in, I get the error statement, but not until after I return from the input function. So it appears that cin is failing when coming back from the employee input function. So does that mean something is wrong with that function? I know you said I can hard code an employee, but I am unsure what that means, and how to bypass the sscanf call.

My files are attached (in a format which I hope works - I've never actually zipped folders before, I've just unzipped stuff). Thank you for your help!!

It zipped fine. You have several problems. First, in line 16 in your main loop:

do
	{
        if (cin.fail ())
        {
           cout << "cin is in a failed state.  Terminating program.\n";
           exit (1);
        }
		cout << "What action would you like to perform?" << endl;
		cin >> input1;

		switch (input1)
		{
		case 'A':   //Add Member
		{
			cout << "Please enter a member information to add:\n";
			cin >> inputInfo;
			StaffMember * newStaffMember = StaffMemberParser::parseStringToMember(inputInfo);
            memberList.push_back(newStaffMember); 
			break;
        }
		case 'C':   //Compute Pay
		    for (int j=0; j < memberList.size(); j++)
			{
                 memberList.at(j)->computePay();
		    }
            cout << "pay computed\n";
			break;
		case 'D':   //Search for Member
			cout << "Please enter a memberID to search:\n";
			cin >> inputInfo;
			operation = false;
			
            for (int j=0; j < memberList.size(); j++)
			{
			     if (inputInfo == memberList.at(j)->getMemberId())
			     {
                       operation = true;
                 }
                 else
                 {
                       operation = false;
                 }
		    }
			if (operation == true)
				cout << "member found\n";
			else
				cout << "member not found\n";
			break;
		case 'L':   //List Members
             if (memberList.size() == 0)
             {
                cout << "no member\n" << endl;
             }
             else 
             {
                 for (int j=0; j < memberList.size(); j++)
			     {
				     memberList.at(j)->printInfo();
		         }   
             }
			 break;
		case 'Q':   //Quit
			for (int j=0; j < memberList.size(); j++)
			{
				delete memberList.at(j);
			}
			break;
		case '?':   //Display Menu
			printMenu();
			break;
		default:
			cout << "Unknown action\n";
			break;
		}

	} while (input1 != 'Q' && input1 != 'q'); // stop the loop when Q is read

If you type in "John Smith 1234 Volunteer", that isn't going to be stored in inputInfo in line 16. The >> operator does not read a full line. It reads to the first space, so it will read "John" and " 1234 Volunteer" stays in the cin buffer. If you want the entire line, and I think you do, you want to use getline:

getline (cin, inputInfo, '\n');

Note that when using functions like getline and get versus the >> operator, you can have problems if you aren't careful. Flushing the input stream using the ignore command can solve the problem sometimes.

http://www.daniweb.com/forums/thread90228.html

I still think you have problems in your parsing function. I'm not very experienced with sscanf, so i don't know exactly what the problem is, but it doesn't appear to be parsing the line properly even when it's given the entire line.

You're using C++, so unless you already are quite familiar with sscanf, you want to check out the string library and the stringstream library and take some tutorials on both. stringstreams, which are available in C++, but not C, can be quite valuable for parsing. I would actually put the entire assignment aside for now and practice these basic concepts like input and output and parsing strings till you understand them. You're using them in a class, and if C++ is brand new to you, you need to learn about strings, input and output, and classes. Learn them separately and it'll be easier. Right now you're trying to learn them all in the same program and it's too big. So my general advice is this:

  1. Put aside the assignment for now.
  2. Pick C or C++ for your strings and don't try to mix them.
  3. Practice input and output using get, getline, the >> operator, and cin's "ignore" function when mixing these three things.
  4. Practice parsing strings, either using the string library, the cstring library and/or stringstreams.
  5. Learn about classes and polymorphism.
  6. Then come back and try to tackle the rest of the assignment.

I cant exactly read programs with header included but i can read full on programs where all the code is put together rather than separating part with a header... but I can tell you, usually when a program doesnt stop and it keeps going on when u enter something, it has to do with your loop. maybe your loop goes on until it gets to the last member and then it starts to print output... instead u can change it to make it output after each member is called... Im pretty sure its either the loop or your print function... I dont know but I see that you loop keeps going until the quit command Q is entered...

Edited 7 Years Ago by triumphost: n/a

This article has been dead for over six months. Start a new discussion instead.