| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 256 人关注过本帖
标题:[求助]程序运行过程中出错
只看楼主 加入收藏
hegj
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2007-9-5
收藏
 问题点数:0 回复次数:0 
[求助]程序运行过程中出错

这是书中的一道练习题,我写了一个程序,编译通过了。但在运行时,系统提示出错,要关闭。
希望大家给我看看,对问题,对程序提提意见的。谢谢大家了!



给定如下的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;
}

2007-10-03 22:51
快速回复:[求助]程序运行过程中出错
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017413 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved