void matrix_input(double A_matrix[], int dimensions, char name)
{ bool correct_input = false;
cout<<"Enter the elements of the "<<dimensions<<"x"<<dimensions<<" matrix "<<name<<endl;
for(int i = 0; i<dimensions; ++i)
{
for(int j = 0; j<dimensions; ++j)
{ while(!correct_input)
{
correct_input = true;
cout<<name<<"("<<i+1<<j+1<<") = ";
cin>>A_matrix[i*dimensions+j];
if(cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout<<"ERROR!\nwrong input\nPlease enter a REAL NUMBER only.\n";
correct_input = false;
}
}
}
cout<<endl;
When i call this function, it works perfectly fine when I remove all the error checking stuff to see if the input is actually a double. But with it the function only asks the user to enter the very first value of the matrix and then ends. I don't really understand why or how this is happening.
I also have a question about cin.ignore(numeric_limits<streamsize>::max(),'\n');
Im a bit confused about type streamsize. Looking it up i found that it is the type to represent sizes and character counts in streams. Does cin.ignore(numeric_limits<streamsize>::max(),'\n');
mean that it will ignore 2147483648 characters or that many digits after the first 1?
Any help is always appreciated. Thanks.
Void~
The problem is with the code I posted above(unless I'm making some mistake in calling the function). Say if I do something like this
1 2 3 4 5 6 7 8 9
int main()
{
int Size;
cin>>Size;
double matrix_a [Size*Size], matrix_b[Size*Size];
matrix_input(matrix_a, size, 'A');
matrix_input(matrix_b, size, 'B');// matrix_input being the function I posted originally
return 0;
}
Say I input size to be 2. The program then asks me to enter A(1,1) and then asks me to enter B(1,1). Completely skipping over all the other elements.
Yes I know ignore will discard all characters till a new line but doest something like cin.ignore(80, '\n'); ignore 80 characters or to a newline, whichever comes first? Kindof why I'm a bit confused over streamsize. Returning numeric_limit<streamsize>::max() I got 2147483647. What difference would it make if I replace streamsize with int or double?
void matrix_input(double A_matrix[], int dimensions, char name)
{ bool correct_input = false;// Should have been after the 2nd for loop
cout<<"Enter the elements of the "<<dimensions<<"x"<<dimensions<<" matrix "<<name<<endl;
for(int i = 0; i<dimensions; ++i)
{
for(int j = 0; j<dimensions; ++j)
{ bool correct_input = false;
while(!correct_input)
{
correct_input = true;
cout<<name<<"("<<i+1<<j+1<<") = ";
cin>>A_matrix[i*dimensions+j];
if(cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout<<"ERROR!\nwrong input\nPlease enter a REAL NUMBER only.\n";
correct_input = false;
}
}
}
cout<<endl;