how to getline 2 times?

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
#include<iostream>
using namespace std;

float calcGross(int);
float totDeduct(float);

void main()
{
	char nextEmployee = 'Y';
	char employeeID[40];
	int hoursWorked;
	float grossPay, totalDeduction, netPay, totNetPay=0;

	while(nextEmployee != 'N' || nextEmployee != 'n')
	{
		cout<<"\nEnter employee's ID\t\t: "; 
		cin.getline(employeeID,40);
		cout<<"Enter hours worked per week\t: ";
		cin >> hoursWorked;

		grossPay = calcGross(hoursWorked);
		totalDeduction = totDeduct(grossPay);
		netPay = grossPay - totalDeduction;

		cout<<"\n\nEmployee ID\t: " <<employeeID;
		cout<<"\nGross pay\t: " <<grossPay;
		cout<<"\nTotal deduction\t: "<<totalDeduction;
		cout<<"\nNet pay\t\t: "<<netPay;

		totNetPay = totNetPay + netPay;

		cout<<"\n\nContinue with next employee (Y/N)? : ";
		cin >>nextEmployee;
		cout<<"\n\n";
	}

	cout<<"Total net pay for all employees in the company : "<<totNetPay <<endl;
	cout<<"\t\t---THANK YOU---" <<endl;
}

//Function to calculate Gross
float calcGross(int hours)
{
	float gross=0;
	if(hours>60)
		gross= ((hours-60)*(5.00*3));
	if(hours>50 && hours<60)
		gross= gross + ((hours-50)*(5.00*2));
	if(hours<=50)
		gross = gross + (hours * 5.00);
	else
		cout<<"Invalid input.";
	
	return gross;
}

//Function to calculate total Deduction
float totDeduct(float gross)
{
	float deduction, incomeTax, epf, empUnion;
	incomeTax = gross * 0.07;
	epf = gross * 0.11;
	empUnion = 15.00;

	deduction = incomeTax + epf + empUnion;

	return deduction;
}



Enter employee's ID          :  ABC
Enter hours worked per week  :  40

Employee ID     :  ABC
Gross pay       :  200
Total deduction :  51
Net pay         :  149

Continue with next employee (Y/N)? : Y

Enter employee's ID          :  Enter hours worked per week  : 


The problem is.. I cannot input the employee's ID for the second time. The program directly output the "Enter hours worked per week :" together with the "Enter employee's ID : ". I can't seem to figure out the problem?

Help??
cin >> leaves a newline in the input stream. Use cin.ignore(numeric_limits<streamsize>::max(), '\n'), I think, to remove it, before calling getline..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// ...

//void main() 
int main() // main MUST return an int
{
	char nextEmployee = 'Y';

    // ...
     
        // ( nextEmployee != 'N' || nextEmployee != 'n' ) will always be true 
	//while( nextEmployee != 'N' // nextEmployee != 'n' )
	while( nextEmployee != 'N' && nextEmployee != 'n' )
	{
		cout<<"\nEnter employee's ID\t\t: ";
		
		// there will be a new line left in the buffer after the last input
		// remove it before calling getline()  
		cin >> ws ; // *** added
		
		cin.getline(employeeID,40);
		cout<<"Enter hours worked per week\t: ";
		
		// ... 

AWESOMEEEE!! thanks for helping. I really appreciate it :)
The result printed just the way I want it. Thanks again.

But some question thou..
cin >> ws ;
what does it for? And what "ws" stands for?
There are two kinds of input possible with C++ streams - formatted input and unformatted input.
The formatted input operations leave unconsumed chars (typically white spaces - new line, space, tab etc.) in the input buffer. If the next input is also a formatted input, there is no problem - by default, formatted input skips over leading white spaces. For example:

1
2
3
4
5
6
int a, b ;
std::cin >> a ; // here you type in <space>678<new-line>
// the formatted input skips over the leading space, reads 678 into 'a' and leaves a new-line in the buffer
std::cin >> b ; // here you type in 99<new-line>
// the input buffer now contains <new-line>99<new-line>, but this causes no worries 
// the formatted input skips over the leading white space, reads 99 into 'b' and leaves a new-line in the buffer 


Now, let us say we add an unformatted input operation to the mix, with <new-line> still in the input buffer:
1
2
3
4
5
6
char cstr[100] ;
std::cin.getline( cstr, sizeof(cstr) ) ; // here you type in 'hello world' followed by a new line.
// the input buffer now contains <new-line>hello world<new-line>
// the unformatted input operation does not skip over leading white space
// the first thing it sees is the <new-line> in the input buffer, and it returns immediately 
// (after discarding the new line). hello world<new-line> still remains in the input buffer  at the end of all this. 


For the unformatted input to get to 'hello world', we have to remove the leading white space (new line) from the input buffer. And this is what std::cin >> std::ws does. A dumbed down explanation is here: http://www.cplusplus.com/reference/iostream/manipulators/ws/
Last edited on
Topic archived. No new replies allowed.