Program requiring enter key be pressed before running
May 19, 2011 at 12:12am UTC
Whenever I run my program, it starts at a blank screen and I have to press the enter key before it will proceed. I have narrowed it down to the cin.ignore(100, '\n'); line in my obtainPayrollInfo() function, but no matter how I have adjusted I can't get it to work with it or without the line. I am needing to enter a name and that is why I have the cin.ignore. If anybody can help me, I would really appreciate it.
Payroll.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
class Payroll
{
private :
// Private class data members
string Name;
double Rate;
double HrsWrkd;
double GrossPay;
double FedTax;
double SocSec;
double Medical;
double NetPay;
// Private facilitator functions
void calcGrossPay(double , double );
void calcFedTax();
void calcSocSec();
void calcMedical();
void calcNetPay();
static const int UNIONDUES = 20;
public :
// Public function prototypes
// Constructors
Payroll();
Payroll(string, double , double );
// Public mutator function prototypes
void setName(string);
void setRate(double );
void setHrsWrkd(double );
// Public accessor function prototypes
string getName();
double getRate();
double getHrsWrkd();
double getGrossPay();
double getFedTax();
double getSocSec();
double getMedical();
int getUnionDues();
double getNetPay();
// Public facilitator function prototypes
void obtainPayrollInfo();
void calcPayroll();
double calcDeductions();
void displayPayroll();
};
// Constructor functions
Payroll::Payroll()
{
}
Payroll::Payroll(string name, double rate, double hours)
{
Name = name;
Rate = rate;
HrsWrkd = hours;
}
// Mutator functions
void Payroll::setName(string name)
{
Name = name;
}
void Payroll::setRate(double rate)
{
Rate = rate;
}
void Payroll::setHrsWrkd(double hours)
{
HrsWrkd = hours;
}
string Payroll::getName()
{
return Name;
}
double Payroll::getRate()
{
return Rate;
}
double Payroll::getHrsWrkd()
{
return HrsWrkd;
}
double Payroll::getGrossPay()
{
return GrossPay;
}
double Payroll::getFedTax()
{
return FedTax;
}
double Payroll::getSocSec()
{
return SocSec;
}
double Payroll::getMedical()
{
return Medical;
}
int Payroll::getUnionDues()
{
return UNIONDUES;
}
double Payroll::getNetPay()
{
return NetPay;
}
// Facilitator functions
void Payroll::obtainPayrollInfo()
{
cin.ignore(100, '\n' );
cout << "Enter employee's name: " ;
getline(cin, Name);
cout << "Enter employee's hourly pay rate: " ;
cin >> Rate;
cout << "Enter employee's hours worked: " ;
cin >> HrsWrkd;
}
void Payroll::calcPayroll()
{
calcGrossPay(Rate, HrsWrkd);
calcFedTax();
calcSocSec();
calcMedical();
calcNetPay();
}
void Payroll::calcGrossPay(double rate, double hours)
{
GrossPay = rate * hours;
}
void Payroll::calcFedTax()
{
FedTax = GrossPay * .25;
}
void Payroll::calcSocSec()
{
SocSec = GrossPay * .088;
}
void Payroll::calcMedical()
{
Medical = GrossPay * .02;
}
double Payroll::calcDeductions()
{
double deductions = FedTax + SocSec + Medical + UNIONDUES;
return deductions;
}
void Payroll::calcNetPay()
{
NetPay = GrossPay - calcDeductions();
}
void Payroll::displayPayroll()
{
cout << setiosflags(ios::fixed) << setprecision(2);
cout << "Employee's Name: " << Name << endl;
cout << "Employee's Pay Rate: " << "$" << Rate << endl;
cout << "Employee's Hours Worked: " << HrsWrkd << endl;
cout << "Employee's Gross Pay: " << "$" << GrossPay << endl;
cout << "Employee's Total Deductions: " << "$" << calcDeductions() << endl;
cout << "Employee's Net Pay: " << "$" << NetPay << endl;
}
arrayPayroll.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <iostream>
#include <iomanip>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::ios;
using std::setiosflags;
using std::setprecision;
#include "Payroll.h"
int main()
{
char entry = 'Y' ;
int maxNo = 0;
int x;
Payroll arrPayroll[100];
for (x = 0; x < 100; x++)
{
Payroll myPayroll;
myPayroll.obtainPayrollInfo();
myPayroll.calcPayroll();
arrPayroll[x] = myPayroll;
maxNo++;
cout << "Do you want to enter another employee? (Y or N): " ;
cin >> entry;
entry = toupper(entry);
if (entry == 'N' )
{
x = 100;
}
}
for (x = 0; x < maxNo; x++)
{
cout << endl << "Employee No. " << (x + 1) << endl;
arrPayroll[x].displayPayroll();
}
system("pause" );
return 0;
}
May 19, 2011 at 4:57am UTC
May 19, 2011 at 5:03am UTC
Try moving that
cin.ignore(100, '\n' );
command to follow line 33 in arrayPayroll.cpp. You can then remove it from Payroll::obtainPayrollInfo()
1 2 3
cin >> entry;
cin.ignore(100, '\n' );// new line
entry = toupper(entry);
I think that will work.
Last edited on May 19, 2011 at 5:04am UTC
Topic archived. No new replies allowed.