首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

数组输出不正确

2013-03-29 
求助:数组输出不正确输入长度4,数据为:1 2 3 4为什么输出错误啊!输出的89219332 8123213313 82132131 3123

求助:数组输出不正确
输入长度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;
}

热点排行