isdigit()一小段代码,string读入问题

 #ifndef READMATRIX_HPP
#define READMATRIX_HPP


void readMatrix(int **m, int sz);



#endif

#include "readMatrix.hpp"
#include <iostream>
#include <cmath>
#include <cctype>
#include <stdlib.h>
using namespace std;

void readMatrix(int **m, int sz)
{
    cout << "Fill the Matrix" << endl;
    string s;
    getline(cin, s);
    int index = 0;
    for (int i = 0; i < sqrt(sz); i++)
    {
        for (int j = 0; j < sqrt(sz); j++)
        {
            if (isdigit(s[index]))
            {
                m[i][j] = s[index] - '0';
            }
            else
            {
                cout << "it is not digit";
            }
            index++;
        }                   
    }
}

#include "readMatrix.hpp"
#include "Determinant.hpp"
#include <iostream>
#include <cmath>
#include <cctype>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
    int row, column;
    int sz;
    int d = 0;
    cout << "Enter the size of matrix, 2 * 2 or 3 * 3, input 4 or 9:" << endl;
    cin >> sz;
    row = sqrt(sz);
    column = sqrt(sz);

//dynamically allocate a 2D array
    int **a = new int *[row];
    for (int i = 0; i < row; i++)
    {
        a[i] = new int[column]; 
    }


    readMatrix(a, sz);
        for (int i = 0; i < sqrt(sz); i++)
    {
        for (int j = 0; j < sqrt(sz); j++)
        {
            if (j != 0)
            {
                cout << " ";
            }
            cout << a[i][j];                                        
        }
        cout << endl;
    }
    cout << "determinant: " << d << endl;

    for (int i = 0; i < row; i++)
    {
        delete[] a[i];
    }
    delete[] a;
    }

不知道为什么string没有读入

你第一次cin输入时其实输入了一个数值和回车,sz把数值取了,然后剩下了回车 到getline时它把回车取了,所以也不用你输入了,你可以输入时直接输入 整数字符串连起来输入或getline2次,第一次排除掉回车,第二次输入字符串

你getline两次试试,cin>>sz之后可能没有读入回车符,因此getline会得到回车符前的空字符串

cin读到空格或换行符结束,但是不会丢弃遇到的换行符,仍存留在缓冲区中,所以getline读到的相当于空行