Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 对角线差分算法的输入对于超出代码中指定界限的值是不正确的_C++_Algorithm_Matrix - Fatal编程技术网

C++ 对角线差分算法的输入对于超出代码中指定界限的值是不正确的

C++ 对角线差分算法的输入对于超出代码中指定界限的值是不正确的,c++,algorithm,matrix,C++,Algorithm,Matrix,二维向量矩阵的输入首先是一个要求矩阵大小的输入,这很好。但是对代码的部分要求是矩阵中的值必须在-100和100之间,但是我设置的用于将值保持在该范围内的缓冲区在重新输入行时会导致矩阵发生故障 我真的不知道问题的根源,所以我不知道该怎么办。我得到的最远的结果是确定问题的根源是,当缓存后重新输入行时,该行在最终矩阵中的顺序会出现问题,我通过在分配所有值后可视化矩阵来确定 /**Matrix Input**/ int main(){ cout << "Enter size of m

二维向量矩阵的输入首先是一个要求矩阵大小的输入,这很好。但是对代码的部分要求是矩阵中的值必须在-100和100之间,但是我设置的用于将值保持在该范围内的缓冲区在重新输入行时会导致矩阵发生故障

我真的不知道问题的根源,所以我不知道该怎么办。我得到的最远的结果是确定问题的根源是,当缓存后重新输入行时,该行在最终矩阵中的顺序会出现问题,我通过在分配所有值后可视化矩阵来确定

/**Matrix Input**/
int main(){
    cout << "Enter size of matrix: ";
    int n = 0;
    cin >> n;
    cout << endl;
    vector<vector<int> > arr (n, vector<int>(n));
    cout << "Enter Format: \na b c[en.]\nd e f[en.]\ng h i[en.]" << endl;
    for (int i = 0; i < n; i++){
        cout << "Enter Line " << i + 1 << endl;
        for (int r = 0; r < n; r++){
            cin >> arr[r][i];
            /**Root of Error: Out of Specified Range**/
            if (arr[r][i] > 100){
                cout << endl;
                cout << "Error: Value " << r + 1 << " is over 100. Please re-enter line" << endl;
                i--;
                break;
            }
            if (arr[r][i] < -100){
                cout << endl;
                cout << "Error: Value " << r + 1 << " is less than -100. Please re-enter line" << endl;
                i--;
                break;
            }
        }
    }
    /* Printing out the Matrix */
    for (int i = 0; i < n; i++){
        for (int r = 0; r < n; r++){
            cout << arr[r][i] << " ";
        }
        cout << endl;
    }
    return 0; 
}
/**矩阵输入**/
int main(){
cout>n;

问题是当你进入

4 500 6
500触发错误并退出循环,但
6
仍在等待读取。您的下一次
i
迭代将找到

6 <enter> 4 5 6
6456
因此,该行将为645,“6”将保留到下一行

6 <enter> 7 8
678
程序将永远不会处理您的最终
9


如果要按行操作,则需要读取字符串(使用
getline
)并自己将其拆分为字段。

使用
std::cin.Ignore(std::numeric\u limits::max(),'\n')忽略其余错误行。
。请添加“已解决”在答案的标题中。感谢您的回复。这确实是问题所在,使用Alexander Zhang在评论中推荐的cin.ignore,我能够解决问题。