How to clears the error flag on cin in C++

1 Answer

0 votes
#include <iostream>
#include <string>

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

int main()
{
	std::string s;
	double d;
	
	cout << "Enter a string: ";
	cin >> s;
	cin.clear(); // clears the error flag for next I/O to work correctly
	cout << "Enter a float number: ";
	cin >> d;

	cout << s << " " << d << endl;

	return 0;
}


/*
run:

Enter a string: c++
Enter a float number: 3.14
c++ 3.14

*/

 



answered Apr 10, 2018 by avibootz

Related questions

1 answer 84 views
1 answer 129 views
129 views asked Apr 9, 2018 by avibootz
1 answer 160 views
1 answer 157 views
1 answer 204 views
...