简体   繁体   English

暂停输入C ++

[英]Pausing for input C++

I'm trying to write some code that will allow the input of a matrix and then calculate the inverse of the matrix followed by the input for a vector. 我正在尝试编写一些代码,以允许输入矩阵,然后计算矩阵的逆值,然后再输入向量。 However once the matrix values have been entered I cannot get the code to pause and wait for the vector entries, it just executes the rest of the main filling the vector with zeroes by default. 但是,一旦输入了矩阵值,我就无法使代码暂停并等待向量输入,默认情况下,它只是执行剩余的主要部分,用零填充向量。 Is it possible to reset the input or something so that the console will wait for input when I want to fill the vector? 是否可以重置输入或其他方式,以便控制台在我要填充矢量时等待输入?

#include <iostream>
#include "MatrixInverse.h"
#include "ReadMatrixRow.h"
#include "GlobalMinVar.h"

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

int main(){


    int i;
    int const n_size=3;
    double **SigmaInv, **Sigma,*mu,*MinVarWeight;

    Sigma = (double **)malloc(n_size * sizeof(double *));
    Sigma[0] = (double *)malloc(n_size * n_size * sizeof(double));
    for(i = 1; i < n_size; i++)
    { Sigma[i] = Sigma[0] + i * n_size;}

    cout<<"Enter the entries of the covariance matrix row by row,"<<endl;
    cout<<"breaking each row with a non-numeric character:"<<endl;

    //PAUSES HERE FOR MATRIX INPUT

    for (i=0;i<n_size;i++)
    {
        ReadMatrixRow(cin,Sigma,i);
    }

    mu=(double *)malloc(n_size * sizeof(double *));

    cout<<"Input the expected return vector values:"<<endl;

    //WANT A PAUSE HERE FOR FURTHER INPUT THAT IS SEPARATE

    ReadVector(cin,mu);

    for (i=0;i<n_size;i++)
    {
        cout<<mu[i]<<endl;
    }

The functions that read the matrix and the vector are 读取矩阵和向量的函数是

istream& ReadMatrixRow(istream& in, double **s,int i)
{
    if (in)
    {
        int j=0;
        double x;
        while (in>>x)
        {
            s[i][j]=x;
            ++j;
        }
        in.clear();
        in.ignore(1);
    }
    return in;
}

istream& ReadVector(istream& in, double *s)
{
    if (in)
    {
        int i=0;
        double x;
        while (in>>x)
        {
            s[i]=x;
            ++i;
        }
        in.clear();

    }
    return in;
}

You seem to end the first (and second) loop of input by pressing the end-of-file sequence for your system. 您似乎通过按系统的文件结束序列来结束第一个(和第二个)输入循环。 That effectively closes the input so your second loop is not executed at all. 这有效地关闭了输入,因此根本不会执行第二个循环。 This should have been very obvious if you just stepped through the code, line by line, in a debugger. 如果您只是在调试器中逐行浏览代码,这应该非常明显。

Instead of doing that use a for loop to read the input. 而不是使用for循环读取输入。 That way you will not have to use the end-of-file sequence to terminate the input, you also will (or should) be protected against going out of bounds of your allocated memory (why aren't you using static arrays if you just allocate a fixed number of elements?). 这样,您将不必使用文件结尾序列来终止输入,还可以(或应该)防止超出分配的内存范围(为什么不使用静态数组,如果您只是分配固定数量的元素?)。

To pause until the user presses a key use the getch(). 要暂停直到用户按下某个键,请使用getch()。 You need to include conio.h header file to use getch 您需要包括conio.h头文件才能使用getch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM