Sometimes it works, and sometimes it doesn't

1
2
	std::cout << "Press ENTER to continue...";
	std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );


Why does this only work sometimes? Is there an include I am missing?

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
#include "stdafx.h";
#include <iostream>
#include <string>
#include <iomanip>

using std::cin;
using std::cout;
using std::endl;
using std::string;

//Global Declarations of Variables
double iovertime_hours=0, iovertime_pay=0, iovertime_extra=0;
int ihours, iwage;
string cname;

int main ()
{
	//Enter Employee Information
	cout << "\n\nEnter the employee name = ";
	cin >> cname ;
	cout << "Enter the hours worked  = ";
	cin >> ihours;
	cout << "Enter his or her hourly wage = ";
	cin >> iwage; 

	// Determine if hours are greater than 40 
	if (ihours > 40) 
	{
		//Do Calculations
		iovertime_hours=ihours-40;
		iovertime_pay=iwage*1.5;
		iovertime_extra=iovertime_hours*iovertime_pay;

		// Display Employee Details
		cout << "\n\n";
		cout << "Employee Name ............. = " << cname 
			   << endl;
		cout << "Base Pay .................. = " << iwage*40 
			   << endl; 
		cout << "Hours in Overtime ......... = " << iovertime_hours 
			   << endl;
		cout << "Overtime Pay Amout......... = " << iovertime_extra 
			   << endl;
		cout << "Total Pay ................. = " << iovertime_extra+(40*iwage) 
			   << endl;
                std::cout << "Press ENTER to continue...";
		std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
		return 0;
	}    
	else // Else hours are less than 40 hours
	{
		cout << "\n\n";
		cout << "Employee Name ............. = " << cname 
			   << endl;
		cout << "Base Pay .................. = " << iwage*ihours 
			   << endl;
		cout << "Hours in Overtime ......... = " << iovertime_hours 
			   << endl;
		cout << "Overtime Pay Amout......... = " << iovertime_extra 
			   << endl;
		cout << "Total Pay ................. = " << iovertime_extra+(ihours*iwage) 
			   << endl;	
		std::cout << "Press ENTER to continue...";
		std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
		return 0;
	} // End of the primary if statement


} //End of Int Main 


It is not working here. Do I have it in the wrong place?
It's because of:

cin >> iwage;

This line will leave extra junk in the buffer. To fix that, copy the std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ); and put it right after it.
It still did not work. The console closes right after it displays the messages.
this is how i keep console open. you need to sync first. just add std::cin.sync(); in your code like below and you should be all set

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main(int argc, char* argv[])
{

//do stuff

std::cin.sync(); // you need this
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

}
Last edited on
Thank you very much!!!
Topic archived. No new replies allowed.