Can i have some help?

Write your question here.
i can't input but i declare cin in my codes, im doing an payroll system codes and i have to pass it til 5pm gmt+8
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
  #include <iostream>
#include <string>
using namespace std;

int main()
{
    
  float gross, tax, net;
  float pperiod, rhworked, rrate, bsalary, ohrendered, otpay;
  
  string ename;
  
  
  cout<<    "\t"    "Payroll Period" "\n"; 
  cin>> pperiod;
  cout<< "\n";
  
  cout<< "Employee Name: ";
  getline (cin, ename);
  cout<< "\n";
  
  cout<< "Regular Hours Worked: ";
  cin>> rhworked;
  cout<< "\n";
  
  cout<< "Regular Rate: ";
  cin>> rrate;
  cout<< "\n";
  
  cout<< "Basic Salary of the Week: ";
  cin>> bsalary;
  cout<< "\n";
  
  bsalary = rhworked * rrate;
  
  cout<< "Overtime Hours Rendered: ";
  cin>> ohrendered;
  cout<< "\n";
  
  
  otpay = ohrendered * rrate * 1.5;
  gross = bsalary + otpay;
  tax   = gross * 0.15;
  net   = gross - tax;
  
  cout<< "Overtime Pay: ";
  cout<< otpay;
  cout<< "Gross Pay: ";
  cout<< gross;
  cout<< "\n";
  cout<< "Less: Tax ";
  cout<< tax;
  cout<< "\n";
  cout<< "Net Pay: ";
  cout<< net;
  
  
  
    return 0;
}
Last edited on
Hello ezekiel17,

You are mixing formatted input, (cin >>pperiod;), with unformatted input, the "getline". The problem is that formatted input leaves the new line, "\n", in tthe input buffer. The unformatted "getline()" will read the "\n" from the input buffer and move on not allowing any new input until the next formatted input.

You can use this to deal with the "\n":
1
2
3
cin>> pperiod;

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>. 

This would clear the entire input buffer before the "getline()".

The rest of the "cin >>" statements should not be any problem.

While testing the change I changed the first prompt for "Payroll Period". It looked funny sticking out to the right the way you have it. Just made it match the rest of the inputs.

Your final "cout" statements can be written this way to make it easier to see what the output will look like.
1
2
3
4
5
  cout
     << "Overtime Pay: "<< otpay << '\n'
     << "Gross Pay: " << gross << '\n'
     << "Less: Tax " << tax << '\n'
     << "Net Pay: " << net << '\n';


This gave the output of:

Payroll Period: 2

Employee Name: asdf

Regular Hours Worked: 40

Regular Rate: 10

Basic Salary of the Week: 400

Overtime Hours Rendered: 0

Overtime Pay: 0
Gross Pay: 400
Less: Tax 60
Net Pay: 340



Andy
mixing cin and getline can lead to trouble. Google that for how to fix it and to understand it.
Last edited on
Topic archived. No new replies allowed.