大神帮找找错!!十万火急!!

yingair 2012-10-21 02:30:45
#include<iostream>
using namespace std;
template<class T>
struct Term
{
int row,col;
T value;
};


template<class T>
class SparseMatrix
{
public:
SparseMatrix(int maxTerms=10);
~SparseMatrix(){delete []a;};
void Input();
void Output();
private:
int rows,cols;
int terms;
Term<T> *a;
int MaxTerms;
};


template<class T>
SparseMatrix<T>::SparseMatrix(int maxTerms)
{
if(maxTerms<1)cout<<"error!";
MaxTerms = maxTerms;
a = new Term<T>[MaxTerms];
terms=rows=cols=0;
};


template<class T>
void SparseMatrix<T>::Output()
{
cout<<"rows="<<rows<<" "<<"columns="<<cols<<endl;
cout<<"nonzero terms = "<<terms<<endl;
for(int i=0;i<terms;i++)
cout<<"a("<<a[i].row<<','<<a[i].col<<")="<<a[i].value<<endl;
}


template<class T>
void SparseMatrix<T>::Input()
{
int r,c,v;
cout<<"Enter number of rows,columns,and terms"<<endl;
getchar();
cin>>rows;
getchar();
cin>>cols;
getchar();
cin>>terms;
getchar();
if(terms>MaxTerms)cout<<"error!";
cout<<"Enter row,column,and value of term"<<endl;
int i=0;
for(int j=0;j<terms;j++){
getchar();
cin>>r;
getchar();
cin>>c;
getchar();
cin>>v;
getchar();
a[i].row=r;
a[i].col=c;
a[i].value=v;
i++;

}

}


int main()
{
SparseMatrix<int> s1;
s1.Input();
s1.Output();
system("pause");
return 0;
}





运行的时候是这样的:
Enter number of rows,columns,and terms
(2,2,1)
Enter row,column,and value of term
(1,1,1)
rows=2 columns=2
nonzero terms=1
a(-858993460,-858993460)=-858993460



最后为什么是这样呢????求解答!!!!!
...全文
80 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yingair 2012-10-21
  • 打赏
  • 举报
回复
谢谢诸位了!!问题已经解决了!确实是getchar()的问题
JiMoKuangXiangQu 2012-10-21
  • 打赏
  • 举报
回复
有2个getchar()调用是多余的.
JiMoKuangXiangQu 2012-10-21
  • 打赏
  • 举报
回复
#include<iostream>
using namespace std;

template<class T>
struct Term
{
int row,col;
T value;
};


template<class T>
class SparseMatrix
{
public:
SparseMatrix(int maxTerms=10);
~SparseMatrix(){delete []a;};
void Input();
void Output();
private:
int rows,cols;
int terms;
Term<T> *a;
int MaxTerms;
};


template<class T>
SparseMatrix<T>::SparseMatrix(int maxTerms)
{
if(maxTerms<1)cout<<"error!";
MaxTerms = maxTerms;
a = new Term<T>[MaxTerms];
terms=rows=cols=0;
};


template<class T>
void SparseMatrix<T>::Output()
{
cout<<"rows="<<rows<<" "<<"columns="<<cols<<endl;
cout<<"nonzero terms = "<<terms<<endl;
for(int i=0;i<terms;i++)
cout<<"a("<<a[i].row<<','<<a[i].col<<")="<<a[i].value<<endl;
}


template<class T>
void SparseMatrix<T>::Input()
{
int r,c,v;
cout<<"Enter number of rows,columns,and terms"<<endl;
//getchar();
cin>>rows;
getchar();
cin>>cols;
getchar();
cin>>terms;
getchar();
if(terms>MaxTerms)cout<<"error!";
cout<<"Enter row,column,and value of term"<<endl;
int i=0;
for(int j=0;j<terms;j++){
//getchar();
cin>>r;
getchar();
cin>>c;
getchar();
cin>>v;
getchar();
a[i].row=r;
a[i].col=c;
a[i].value=v;
i++;

}

}


int main()
{
SparseMatrix<int> s1;
s1.Input();
s1.Output();
system("pause");
return 0;
}
startservice 2012-10-21
  • 打赏
  • 举报
回复
把getchar全部去掉试试
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧