user puts in the number of employees but inputs less employees. He terminates the loop
by pressing the ENTER key. How do i do that. I tried strlen,, and even trid atoi but i dont know how to terminated
it. Also when it terminated its just uses that many employees that were entered and outputs the result.

The below method give me garbage. I need the return statment
to give me the count so i can display the results for the employees that were entered.

#include <iostream> 
#include<sstream>
#include <string>

#include <iomanip> 

using namespace std;

const float HOURLYPAYRATE=18.75;
 const float COMMISSIONRATE=1000.00;

enum PayType {salary, hourly, commission};

union PayRate{
	long salary;
	float hours;
	int commission;
};

struct Employee {

	char id[4];
    char name[80]; 

   PayType type;
   PayRate rate; 

float grossPay;

};


int getEmployeeData(Employee* e, int size);
float calculateGrossPay(Employee [], int);
void displayEmployeeInfo(Employee [], int size);
int maxPayIndex(Employee[], int size);



int main()
{
 
 

 int MAX=0;
 


 cout<<"Enter maximum number of employees to enter?"<<endl;
 cin>>MAX;
  
 Employee* emp;

  emp=new Employee[MAX];
    if(emp==NULL)
  {
	  cerr<<"couldn't allocate memory"<<endl;
	  return 1;
  }

MAX=getEmployeeData(emp,MAX);
emp->grossPay=calculateGrossPay(emp,MAX);
displayEmployeeInfo(emp,MAX);
delete [] emp;
return 0;
}


int getEmployeeData(Employee* e, int size)
{

  bool test=true;
  int count=0;
 
  
  
  for(int i=0; i<size && test ; i++)
	{
    count++;
	
	stringbuf buffer;
	cout << "Enter the employee's id (Enter to quit):  ";
	cin.get(buffer);
	cout << buffer.str() << endl;
   if ( buffer.str().empty() )
   {
    return count;
   }
	else 
	{
    cout << "Enter name: ";
	cin.ignore();
	cin.getline((e+i)->name, 80);
	cout << "Enter payroll status:  ";
	char input;
	cin >> input;
	cin.ignore();
    if (input=='h'|| input=='H')
		{
		(e+i)->type=hourly;
		cout<<"Enter number of hours worked this month:  ";
	    cin>>(e+i)->rate.hours;
		}
   else if ( input== 's' || input =='S')
		 {
		(e+i)->type=salary;
		cout<<"Enter monthly salary:  ";
		cin>>(e+i)->rate.salary;
		 }

		  else if( input=='c'|| input=='C')
		  {
			  (e+i)->type=commission;
			  cout<<"Enter total sales this month: ";
			  cin>>(e+i)->rate.commission;
		  }
	}
  }
  
  return count;
}


float calculateGrossPay(Employee e[], int size)
{
	for(int i=0; i<size; i++)
	{

		if(e[i].type==salary)
		{  
			e[i].grossPay= (float(e[i].rate.salary));
	    
		}
        else if(e[i].type==hourly)
		{
			e[i].grossPay=(HOURLYPAYRATE * e[i].rate.hours);	
		}
		else if(e[i].type==commission)
		{                                                 
			e[i].grossPay=float(COMMISSIONRATE + (0.06 * e[i].rate.commission));
		}
	}

	return e->grossPay;
	
}


void displayEmployeeInfo(Employee e[], int size)
{
cout<<"  ID   Name   PayrollStatus  GrossPay\n";
cout<<endl;
for(int i=0; i<size; i++)
{
cout <<setw(4)<<e[i].id<<setw(10)<<"  "<<e[i].name;
  if(e[i].type==hourly)
  {
    cout<<setw(10)<<"Hourly";
    cout << fixed << showpoint << setprecision(2);
    cout<<setw(10)<< e[i].grossPay<< endl;
   }
else if(e[i].type==salary)
   {
	   cout<<setw(10)<<"Salaried";
	   cout<<fixed<<showpoint<<setprecision(2);
	   cout<<setw(10)<<e[i].grossPay<<endl;

  }
else if(e[i].type==commission)
  {      
	   cout<<setw(10)<<"Commisioned";
	   cout<<fixed<<showpoint<<setprecision(2);
	   cout<<setw(10)<<e[i].grossPay<<endl;
   }

  
  
  }
  int max =maxPayIndex(e,size);
	  cout<<"the employee with maxium gross pay is"<<e[max].name<<endl;
}

int maxPayIndex(Employee e[], int size)
{
	float largest=e[0].grossPay;
	int count=0;
	for(int i=1; i<size; i++)
	{   
		count++;
		if(e[i].grossPay>largest)
			largest=e[i].grossPay;
	}

	return count+1;
}

Can you abstract the question to something like "how do you input everything the user types until the enter key is pressed?" or something like that? I'd say 3/4 of those lines are not related to your question. Please post
1) the shortest example possible that demonstrates the problem
2) a sample input
3) the current output
4) the expected output

and then we can probably help :)

Dave

Comments
Agreed :)

A common, simple model for entering data till there is no more is something like:

struct empl
{string name;
int ID, department;
};
empl workers[10]
int limit = 10;
int count = 0;
while( count < limit && getline( cin, workers[count].name )
{ 
      cin >> workers[count].ID;
      cin >> workers[count].dept;
      count++;
}

Thanks for all the help .
What i am trying to do is basically detect when an Enter key was pressed, and return the number of employees that were entered. This happens in getEmployeeData right when the id is entered. The program dynamically gets the number of employees but if the user decides that they just want to input less number of employees then they end their input with pressing Enter. How do I accomplish this? What the program does is return the count of employees entered and calls the function to calculate and display the data for gross pay . For example if the user is asked how many employees, they enter 5, but then after inputting the id, name, payrollstatus, and either their number of hours worked, monthly salary, or sales that month ... Then they input one more data for another employee. They decided they don't want to input any more info . So they press Enter when they are asked for the id. The program then outputs their info along with their gross Pay. this is accomplished by calling the displayEmployeeInfo. Also the maxPayINdex returns the index number of the array that corresponds to the employee with the highest grosspay.

Let me know if you need anymore explanation. So basically i am trying to terminate the input with an Enter key. I have tried getch
and also tried the buffer method but i get garbage after the Enter
key is pressed and the program terminates.

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