这是书中的一道练习题,我写了一个程序,编译通过了。但在运行时,系统提示出错,要关闭。
希望大家给我看看,对问题,对程序提提意见的。谢谢大家了!
给定如下的main()函数,该函数仿效在空闲存储区中创建二维数组,分别以rows和cols作为二维,
int main()
{
    int **ptr;
    int rows, cols;
    while(!allocate(ptr, rows, cols))
    {
                         fill(ptr, rows, cols);
                         display(ptr, rows, cols);
                         release(ptr, rows);
    }
}
编写下面这些函数:
(1)allocate(),首先允许用户指定rows和cols参数值,然后分配一个指针数组,数组的
     每个元素都指向一个ints数组(如果遇到文件结束标志,这个函数就返回flase);
(2)fill(),给行×列个数组元素赋值;
(3)display()输出所有数组元素;
(4)release(),释放所有从空间存储区中分配的空间。
我写的程序是这样的:
#include<iostream>
using std::cout;
using std::cin;
bool allocate(int **ptr, int &rows, int &cols);
void fill(int **ptr, int rows, int cols);
void display(int **ptr, int rows, int cols);
void release(int **ptr, int rows);
int main()
{
    int **ptr;
    int rows, cols;
    while(!allocate(ptr, rows, cols))
    {
                         fill(ptr, rows, cols);
                         display(ptr, rows, cols);
                         release(ptr, rows);
    }
    system("pause");
    return 0;
}
bool allocate(int **ptr, int &rows, int &cols)
{
     if(cin.eof()) return true;
     cout << "Input the rows and cols:\n";
     cin >> rows >> cols;
     ptr = new int*[rows];
     for(int i = 0; i < rows; ++i)
           ptr[i] = new int[cols];
     return false;
}
void fill(int **ptr, int rows, int cols)
{          
    cout<<"Input rows * cols ints:\n";         // 刚输出这个,系统就提示出错,每次都是这里
    for(int i = 0; i < rows; ++i)
        for(int j = 0; j < cols; ++j)
                cin >> ptr[i][j];
}
void display(int **ptr, int rows, int cols)
{
     for(int i = 0; i < rows; ++i)
     {
        for(int j = 0; j < cols; ++j)
                cout << ptr[i][j] << '\t';
        cout << '\n';
     }
}
void release(int **ptr, int rows)
{
     for(int i = 0; i < rows; ++i)
        delete ptr[i];
      delete ptr;
}      

 
											





 
	    


 谢谢,我想明白了。
谢谢,我想明白了。