cin statement is skipped

My code is skipping cin statement below, so I never got to input a value.

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
70
71
72
#include <iostream>
#include "LinkedPolynomial.h"   
using namespace std;

LinkedPolynomial <double> CreatePolynomialFromInput()
{
	double input_coef;
	double input_exp;

	LinkedPolynomial<double> poly;

	cout << "Enter pairs of numbers for coefficients and the exponent." << endl
			 << "Press Enter after each entry, or separate each entry by a space." << endl
		   << "Entering a coefficient without an exponent will result in the coefficient being discarded." << endl
		   << "Enter any English alphabet to finish. " << endl;


	while((cin >> input_coef) && (cin >> input_exp))
	{

		poly.Add(input_coef, input_exp);


	}

	return poly;

}

void TestPolynomial()
{
	double exponent;
	double new_coefficient;

	cout << "Testing LinkedPolymial class. " << endl;

 	LinkedPolynomial<double> poly = CreatePolynomialFromInput();

	cout << "The polynomials are: " << endl;
	poly.DisplayPolynomial();

	cout << "\nThe degree of the polynomial is: " << poly.Degree() << endl;

	cout << "Enter a value of exponent. The program will determine whether it exists. " << endl;

	cin >> exponent; // This cin statement is skipped!

	cout << "The coefficient exists(1= exists, 0= doesn't exist): " << poly.Coefficient(exponent) << endl;

	cout << "Enter a value of coefficient." << endl; 

 	cout << "The program will change the value of coefficient corresponding to exponent: " << exponent 
			 << "To your input coefficient: " << new_coefficient << endl;

	cout << "The coefficient has changed(1= exists, 0= doesn't exist): " << poly.ChangeCoefficient(new_coefficient, exponent) << endl;

	cout << "The new polynomial is: " << endl;

	poly.DisplayPolynomial();


}

int main()
{

		TestPolynomial();



   return 0;
}; 
Last edited on
The stream is in a failed state after
while((cin >> input_coef) && (cin >> input_exp)) { /* ... */ }

1
2
3
4
5
6
7
8
9
10
11
	poly.DisplayPolynomial();
        
        // http://www.cplusplus.com/reference/ios/basic_ios/clear/
        std::cin.clear() ; // clear the failed state

        // http://www.cplusplus.com/reference/istream/istream/ignore/
        std::cin.ignore( 1000, '\n' ) ; // throw away the characters remaining in the input buffer

        // ...

	cin >> exponent; // This cin statement won't be skipped now. 
Topic archived. No new replies allowed.