keep_window_open() function won't work input for-loop program

I have this code:
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
// Osman Zakir
// 12 / 18 / 2016
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 4 Section 4.6.3 A Numeric Example (for vectors)
// An example code listing to illustrate a vector of doubles as temperatures

#include "../../std_lib_facilities.h"

int main()
{
	vector<double> temps;
	for (double temp; cin >> temp;)
	{
		cin.ignore();
		temps.push_back(temp);
		cin.ignore();
	}

	for (const double &x : temps)
	{
		cout << x << "\n";
	}
	
	cin.ignore();
	keep_window_open();
}


But the calls to cin.ignore() and keep_window_open() (the latter from std_lib_facilities.h from Stroustrup's book's library access header) fail and the program exits without waiting for a key press. Please help.

Edit for update: doing it like this seems to have helped:
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
// Osman Zakir
// 12 / 18 / 2016
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 4 Section 4.6.3 A Numeric Example (for vectors)
// An example code listing to illustrate a vector of doubles as temperatures

#include "../../std_lib_facilities.h"

int main()
{
	vector<double> temps;
	for (double temp; cin >> temp;)
	{
		cin.ignore();
		temps.push_back(temp);
	}

	for (const double &x : temps)
	{
		cout << x << "\n";
	}
	
	cin.clear();
	cin.ignore();
	keep_window_open();
}


But other ideas, if any, for doing it better are appreciated anyway. And if anyone wants to see the code in that header, just let me know and I'll post it.
Last edited on
Try cin.ignore( numeric_limits<streamsize>::max(), '\n' );
I'll give that a try. Thanks for the help.
1. Execute your console programs from a console.
a. ¿Do you know what `cin.ignore()' does? You seem to be putting it everywhere.


you read the input as while( cin>>input ), for the loop to end, then stream must become invalid.
`keep_window_open()' needs a valid stream, hence you need to do cin.clear()
Either from a console or from an IDE is fine. Running it directly from the .exe file requires that function, but it's not like I can test for that from within my code.

I think I can mark this as solved now. The code is now running as intended for keep_window_open();
Last edited on
Topic archived. No new replies allowed.