为什么会出现乱码呢?

zhanghailang 2004-03-21 11:34:50
我这个程序编译能通过,但是输出结果商品名称时出现乱码,请问是怎么一回事?请高手指点迷津!谢谢了!
#include<iostream.h>
#include<stdlib.h>
class publication
{
protected:
char *title;
float price;
public:
virtual void getData(char *name,float rate);
virtual void putData();
};
void publication::getData(char *name,float rate)
{

title=name;
price=rate;
}
void publication::putData()
{
cout<<endl<<"***商品信息***"<<endl;
cout<<"商品名称"<<title<<endl;
cout<<"价格"<<price<<endl;
}
class book:public publication
{
int pageCount;
public:
void getData(char *name,float price)
{
int pages;
publication::getData(name,price);
cout<<"请输入书的页数:";
cin>>pages;
pageCount=pages;
}
void putData()
{
publication::putData();
cout<<"您输入的书籍的页数为: ";
cout<<pageCount;
cout<<endl;
}
};
class tape:public publication
{
int tapeTime;
public:
void getData(char *name,float price)
{ int times;
publication::getData(name,price);
cout<<"请输入磁带播放时间:";
cin>>times;
tapeTime=times;
}
void putData()
{
publication::putData();
cout<<"磁带播放时间为: ";
cout<<tapeTime<<endl;
}

};
int main()
{
char n;
float p;
publication *ptr;
cout<<"请选择以下菜单:"<<endl;
cout<<"B 您选择的是书籍"<<endl;
cout<<"T 您选择的是磁带"<<endl;
char choice;
cin>>choice;
switch(choice)
{
case 'B':
case 'b':
ptr=new book;
break;
case 'T':
case 't':
ptr=new tape;
}

cout<<"请输入物品名称: ";
cin>>n;
cout<<endl<<"请输入物品价格: ";
cin>>p;
ptr->getData(&n,p);
ptr->putData();
system("pause");

}
...全文
92 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
gameboy007 2004-03-22
  • 打赏
  • 举报
回复
以下打上星的是改正过来的,還要多多努力吧!

#include<iostream> // <--**
using namespace std; // <-- **

class publication
{
protected:
char *title;
float price;
public:
publication() : title(0) {} // <--- **
~publication() { delete title; } // <-- **
virtual void getData(char *name,float rate);
virtual void putData();
};
void publication::getData(char *name,float rate)
{
delete title; // <--**
title = new char[ strlen(name)+1 ]; // <--**
strcpy(title, name); // <--**
price=rate;
}
void publication::putData()
{
cout<<endl<<"***商品信息***"<<endl;
cout<<"商品名称"<<title<<endl;
cout<<"价格"<<price<<endl;
}
class book:public publication
{
int pageCount;
public:
void getData(char *name,float price)
{
int pages;
publication::getData(name,price);
cout<<"请输入书的页数:";
cin>>pages;
pageCount=pages;
}
void putData()
{
publication::putData();
cout<<"您输入的书籍的页数为: ";
cout<<pageCount;
cout<<endl;
}
};
class tape:public publication
{
int tapeTime;
public:
void getData(char *name,float price)
{ int times;
publication::getData(name,price);
cout<<"请输入磁带播放时间:";
cin>>times;
tapeTime=times;
}
void putData()
{
publication::putData();
cout<<"磁带播放时间为: ";
cout<<tapeTime<<endl;
}

};
int main()
{
char n[100]; // <-- **
float p;
publication *ptr;
cout<<"请选择以下菜单:"<<endl;
cout<<"B 您选择的是书籍"<<endl;
cout<<"T 您选择的是磁带"<<endl;
char choice;
cin>>choice;
switch(choice)
{
case 'B':
case 'b':
ptr=new book;
break;
case 'T':
case 't':
ptr=new tape;
}

cout<<"请输入物品名称: ";
cin>>n;
cout<<endl<<"请输入物品价格: ";
cin>>p;
ptr->getData(n,p);
ptr->putData();

delete ptr; // <-- **
system("pause");

}

letifly 2004-03-22
  • 打赏
  • 举报
回复
估计是32位系统把?没有用中文系统输出,建议进入dos,用ccdos中文系统,否则直接输出是没有可能输出中文的,xp下没有任何可能输出中文,至少我没有成功。如果有哪位高手体会成功,说来听听。先谢了。
gameboy007 2004-03-22
  • 打赏
  • 举报
回复
这么长的代码如可解释呢.
先看書,不明白地方再那出问吧。 ^_^
gameboy007 2004-03-22
  • 打赏
  • 举报
回复
是用来设算字串的长度,例如
char name[] = "Hello";
strlen(name) = 5
zhanghailang 2004-03-22
  • 打赏
  • 举报
回复
谢谢gameboy007这位朋友,但是有这个strlen是什么函数,可能我们没学到,不过还是要谢谢你。
lr322 2004-03-22
  • 打赏
  • 举报
回复
ameboy007 你好
能把你上面的代码解释一吗?
非常感谢!~!
谢谢DX了~!

64,654

社区成员

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

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