Thread: Press "Enter" key to quit the program

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    11

    Press "Enter" key to quit the program

    Hi, would you please help me on how to end the "Do-While loop program" immediately if the user presses "Enter" key? (Only enter key is allowed to end the program).

    Is there a specific key or function that allow me to do this?

    Thank you very much for the help.
    Vitamin_C

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    while (getch() != 13)

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You need to include conio.h to use the getch() function. It's not standard, so keep in mind that some implementations won't have this.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    11
    sean_mackrory and h_howee, thank you for your help, the program does looping just perfect.
    Now I need another help and I don't want to create a new thread, so I am just going to continue in here.

    I am trying to make input validation of "Employee ID"
    Code:
    if(isValidID(e[i].ID, count)==true)
    It gives me an error-->error C2664: 'isValidID' : cannot convert parameter 1 from 'char [4]' to 'struct Employee []'<
    I think I put "(isValidID(e[i].ID, count)==true)" in a wrong format or something but I don't know how to fix this.

    Would someones help me to fix this? Is there any way to fix this?

    Here is the up to date code.
    Code:
    #include <iostream>
    #include <cstring>
    #include <iomanip>
    #include <cstdlib>
    #include <conio.h>
    using namespace std;
    
    //STRUCTURE, ENUM,AND UNION//////////////////////////////////////////////////////////////////////
    
    enum payStatus{ Salaries1, Hourly1, Commissioned1};
    
    union payRate
    {
    	long salaries;
    	float hourly;
    	int commissioned;
    };
    
    struct Employee
    {
    	char ID[4];
    	char name[36];
    	payStatus type;
    	payRate rate;
    	float grossPay;
    };
    ////////////////////////////////////////////////////////////////////////////////////////////
    //FUNCTIONS/////////////////////////////////////////////////////////////////////////////////
    
    int getEmployeeData(Employee*, int, int&);
    float calculateGrossPay(Employee[], int);
    int maxPayIndex(Employee[], int);
    int minPayIndex(Employee[], int);
    float avgPay(int, float);
    void displayEmployeeInfo(Employee[], int, float, float, int, int);
    bool isValidID(Employee[], int);
    //bool tryAgain
    //bool isValidStatus
    ////////////////////////////////////////////////////////////////////////////////////////////
    
    
    //MAIN (STARTING POINT)/////////////////////////////////////////////////////////////////////
    
    int main()
    {	
    
    	int numEmployee, max, min, count=0;
    	Employee *emp;
    	float total, average;
    	
    	cout<<fixed<<showpoint<<setprecision(2);
    
    	cout<<"Enter maximum number of employees to enter: ";
    	cin>>numEmployee;
    
    	emp = new Employee[numEmployee];
    
    	getEmployeeData(emp, numEmployee, count);
    
    	total=calculateGrossPay(emp, count);
    
    	max=maxPayIndex(emp, count);
    
    	min=minPayIndex(emp, count);
    
    	average=avgPay(count, total);
    
    	displayEmployeeInfo(emp, count, total, average, max, min);
    
    	delete [] emp;
    
    	return 0;
    }
    ////////////////////////////////////////////////////////////////////////////////////////////
    
    //ALL THE FUNCTION DEFINITIONS//////////////////////////////////////////////////////////////
    
    int getEmployeeData(Employee* e, int numEmployee, int& count)
    {
    	char inString[80][80];
    	int num;
    
    	for(int i=0; i<numEmployee; i++)
    	{
    		cout<<"Enter employee id (Enter to quit): ";
    		cin.ignore();
    		cin.getline(e[i].ID, sizeof(e[i].ID));
    
    		num=atoi(e[i].ID);
    
    		if(isValidID(e[i].ID, count)==true)//HERE IS THE PART THAT I AM HAVING PROBLEM WITH.
    		{
    			cout<<"Enter name (Last, First): ";
    			cin.getline(e[i].name, sizeof(e[i].name));
    
    			cout<<"Enter S or s for Salaried, H or h for Hourly, and C or c for Commissioned"<<endl;
    			cout<<"Enter payroll status: ";
    			cin.getline(inString[i], 80);
    
    			if(*inString[i]=='S' || *inString[i]=='s')
    			{
    				e[i].type=Salaries1;
    				cout<<"Enter monthly salary: ";
    				cin>>e[i].rate.salaries;
    			}
    			else if(*inString[i]=='H' || *inString[i]=='h')
    			{
    				e[i].type=Hourly1;
    				cout<<"Enter number of hours worked this month: ";
    				cin>>e[i].rate.hourly;
    			}
    			else
    			{
    				e[i].type=Commissioned1;
    				cout<<"Enter total sales for this month: ";
    				cin>>e[i].rate.commissioned;
    			}
    
    			count=count+1;
    		}
    		
    		else
    		{
    			if(num==0)
    			{
    				return 0;
    			}
    
    			else
    			{
    				cout<<"*** Invalid employee ID ***"<<endl;
    				return 0;
    			}
    		}
    	}
    
    	return count;
    }
    
    
    float calculateGrossPay(Employee e[], int count)
    {
    	float CalTotal=0.0;
    
    	for(int i=0; i<count; i++)
    	{
    		if(e[i].type==Salaries1)
    		{
    			e[i].grossPay = e[i].rate.salaries;
    		}
    		else if(e[i].type==Hourly1)
    		{
    			e[i].grossPay = 18.75 * e[i].rate.hourly;
    		}
    		else
    		{
    			e[i].grossPay = 1000+(e[i].rate.commissioned*0.06);
    		}
    
    		CalTotal+=e[i].grossPay;
    	}
    
    	return CalTotal;
    }
    
    
    float avgPay(int count, float total)
    {
    	float av;
    
    	av=total/count;
    
    	return av;
    }
    
    
    int maxPayIndex(Employee e[], int count)
    {
    	int bigger=0, x=0;
    
    	for(int i=0;i<count; i++)
    	{
    		if(e[i].grossPay>bigger)
    		{
    			bigger=e[i].grossPay;
    			x=i;
    		}
    
    		else
    		{
    			bigger=bigger;
    		}
    	}
    	
    	return x;
    }
    
    
    int minPayIndex(Employee e[], int count)
    {
    	int smaller=0, x=0;
    
    	smaller=e[0].grossPay;
    
    	for(int i=0;i<count; i++)
    	{
    		if(e[i].grossPay<smaller)
    		{
    			smaller=e[i].grossPay;
    			x=i;
    		}
    
    		else
    		{
    			smaller=smaller;
    		}
    	}
    	
    	return x;
    }
    
    
    bool isValidID(Employee e[], int count)
    {	
    	int length;
    
    	length=strlen(e[count].ID);
    
    	if(length==3)
    	{
    		//for(int i=0; i<3; i++)
    		//{
    			//if(isalnum(e[i].ID))
    				//return true;
    			//else 
    				//return false;
    		//}
    		
    		return true;	
    	}
    	
    	else 
    		return false;
    }
    
    
    void displayEmployeeInfo(Employee e[], int count, float finalTotal, float average, int max, int min)
    {
    	int num;
    
    	num=atoi(e[0].ID);
    
    	if(num==0)
    	{
    		cout<<endl;
    		cout<<"***The program is terminated***"<<endl;
    		cout<<endl;
    		return;
    	}
    
    	else
    	{
    
    	cout<<fixed<<showpoint<<setprecision(2);
    	cout<<endl;
    	cout<<endl;
    
    	cout<<"ID       Name                    Payroll Status          Gross Pay"<<endl;
    	cout<<endl;
    
    	for(int i=0; i<count; i++)
    	{
    		cout<<left<<setw(9)<<e[i].ID<<setw(24)<<e[i].name;
    		
    		if(e[i].type==Salaries1)
    		{
    			cout<<left<<setw(24)<<"Salaries"<<setw(5)<<e[i].grossPay<<endl;
    		}
    		else if(e[i].type==Hourly1)
    		{
    			cout<<left<<setw(24)<<"Hourly"<<setw(5)<<e[i].grossPay<<endl;
    		}
    		else
    		{
    			cout<<left<<setw(24)<<"Commissioned"<<setw(5)<<e[i].grossPay<<endl;
    		}
    	}
    	
    	cout<<endl;
    
    	cout<<"Total Payroll for this month: $"<<finalTotal<<endl;
    	cout<<"Employee with Maximum gross pay: "<<e[max].name<<endl;
    	cout<<"Employee with Minimum gross pay: "<<e[min].name<<endl;
    	cout<<"Average gross pay: $"<<average<<endl;
    
    	cout<<endl;
    
    	}
    }
    Thank you very much C++ people
    Vitamin_C
    Last edited by Vitamin_C; 12-18-2005 at 01:49 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It gives me an error-->error C2664: 'isValidID' : cannot convert parameter 1 from 'char [4]' to 'struct Employee []'<
    I think I put "(isValidID(e[i].ID, count)==true)" in a wrong format or something but I don't know how to fix this.

    Well look at how you declared the function - it expects the whole employee, not just the name.
    isValidID ( &e[i], count ); would be OK

    But perhaps change the function just to accept a char* parameter instead and do what you do now would be better?

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    11
    Hi, I have tried this:
    Code:
    isValidID ( &e[i], count );
    But the program doesn't work right. It stores the "Employee ID" at the first loop. From the second loop and so on, it stores junk number instead.

    Is there any way I can fix this?

    Thank you,
    Vitamin_C

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    11
    Can someones here help me please?
    Vitamin_C

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    11
    Ok, I just turned in my work for whatever I had there. Let's close this thread. Thank you very much for all the help.

    Vitamin_C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [HELP] key press
    By Dakaa in forum C Programming
    Replies: 6
    Last Post: 05-30-2009, 11:16 AM
  2. Replies: 2
    Last Post: 07-25-2003, 03:01 AM
  3. Registery with api?
    By ismael86 in forum Windows Programming
    Replies: 1
    Last Post: 05-14-2002, 10:28 AM
  4. Replies: 1
    Last Post: 10-07-2001, 12:27 AM
  5. Pause or press any key to continue
    By muffin in forum C Programming
    Replies: 3
    Last Post: 09-12-2001, 12:26 PM