读书人

数组输出不正确

发布时间: 2013-03-29 14:24:52 作者: rapoo

求助:数组输出不正确
输入长度4,数据为:1 2 3 4
为什么输出错误啊!输出的89219332 8123213313 82132131 3123131这类数值!哪里错了啊!
#include<iostream>
#include<iomanip>
using namespace std;
typedef int ElemType;
const int MAXSIZE = 100;
class Sqlist
{
private:
ElemType elem[MAXSIZE];
int length;
public:
Sqlist(){length = 0;};
~Sqlist(){};
void Creat();//创建顺序表
void Insert(int i,ElemType e);//插入
ElemType Delet(int i);//删除
void PrintOut();//输出

};


void Sqlist::Creat()
{
cout<<"\nInput The Length:";
cin>>length;
cout<<"\nInput The Data:";
for(int k=0;k<length;k++);
cin>>elem[k];
}


void Sqlist::PrintOut()
{
cout<<"\nThe Length is:"<<length;
cout<<"\nThe Data is:\n";
for(int k=0;k<length;k++)
cout<<" "<<elem[k];
cout<<endl;
}
int main()
{
int i;
Sqlist as;
as.Creat();
as.PrintOut();
return 0;
}
[解决办法]


#include<iostream>
#include<iomanip>
using namespace std;
typedef int ElemType;
const int MAXSIZE = 100;
class Sqlist
{
private:
ElemType elem[MAXSIZE];
int length;
public:
Sqlist(){length = 0;};
~Sqlist(){};
void Creat();//创建顺序表
void Insert(int i,ElemType e);//插入
ElemType Delet(int i);//删除
void PrintOut();//输出

};


void Sqlist::Creat()
{
cout<<"\nInput The Length:";
cin>>length;
cout<<"\nInput The Data:";
for(int k=0;k<length;k++)//把这个去掉好像就对了
cin>>elem[k];
}


void Sqlist::PrintOut()
{
cout<<"\nThe Length is:"<<length;
cout<<"\nThe Data is:\n";
for(int k=0;k<length;k++)
cout<<" "<<elem[k];
cout<<endl;
}
int main()
{
int i;
Sqlist as;
as.Creat();
as.PrintOut();
return 0;
}

读书人网 >C++

热点推荐