C++程序输出乱码 无法完成指定图书信息筛选输出功能
开发目标
实现三类图书信息的筛选输出:
- 印刷时间为2022年的最新图书
- 类型为
childrensbook的儿童图书 - 价格最高的IT类图书
问题现象
运行程序后按提示输入图书信息,测试输入示例如下:
bookname : flower bookISBN : 12 book printingtime : 2022 bookprice 500 booktype : childrensbook
输入完成后程序输出大量乱码符号,无法得到预期的筛选结果。
原始问题代码
#include <iostream> #include <string.h> using namespace std; class Books { protected: char name[25]; int ISBN; int printingtime; double price; char type[25]; double high_price = 0.0; public: void getBookdetails() { for(int i=0; i<2; i++) { cout <<"enter book"<<(i+1)<<"name"<<" "; cin >> name; cout <<"enter book"<<(i+1)<<"ISBN"<<" "; cin >> ISBN; cout <<"enter book"<<(i+1)<<"printingtime"<<" "; cin >> printingtime; cout <<"enter book"<<(i+1)<<"price"<<" "; cin >> price; cout <<"enter book"<<(i+1)<<"type"<<" "; cin >> type; cout << endl; } } virtual void print() { for(int i=0; i<2; i++) { cout <<"book "<<(i+1)<<" name: "<< name << endl; cout <<"book "<<(i+1)<<" ISBN: "<< ISBN << endl; cout <<"book "<<(i+1)<<" printingtime: "<< printingtime << endl; cout <<"book "<<(i+1)<<" price: "<< price << endl; cout <<"book "<<(i+1)<<" type: "<< type << endl; cout << endl; } } }; class latestBooks:public Books { public: void print() { cout <<"the latest books are:"<< endl; Books :: print(); } }; class childrensBooks :public Books { public: void print() { cout <<"the childrens books are:"<< endl; Books :: print(); } }; class ITBooks:public Books { public: void print() { cout <<"the IT books with hightest price are:"<< endl; Books :: print(); cout << high_price << endl; } }; int main() { int printingtime{}; double price=0; char type[25]; const int size=2; char books[size]={}; double high_price = 0.0; int hp=0; Books b; b.getBookdetails(); for(int i=0; i<size; i++) { if(printingtime = 2022) { latestBooks B1; B1.print(); }else if(type == ("childrensbook")) { childrensBooks B2; B2.print(); }else if(price >= high_price) { price = high_price; hp++; ITBooks B3; B3.print(); } } cout <<"the IT books with hightest price are "<< hp <<" at the price of "<< high_price << endl; }
问题根因定位
- 数据存储逻辑完全错误:
getBookdetails循环录入2本图书时,始终把数据写入同一个Books对象的成员变量,第二本录入的数据会直接覆盖第一本;main中定义的char books[size]={}是字符数组,根本不是存储图书对象的结构,多本图书数据从根源上没有被正确保存。 - 未初始化对象导致乱码:筛选逻辑中创建的
latestBooks/childrensBooks/ITBooks都是全新的空对象,没有拷贝之前录入的图书数据,对象内的name/type字符数组是内存中的随机未初始化值,调用print输出时就会打印乱码。 - 基础语法错误:
- 相等判断误用赋值号:
if(printingtime = 2022)是给变量赋值为2022,永远判定为真,不是判断印刷时间是否等于2022 - C风格字符串比较逻辑错误:直接用
==比较字符数组本质是比较两个数组的内存地址,永远不会相等,比较内容需要用strcmp函数 - 最高价格计算逻辑写反:
price = high_price是把初始值0赋给图书价格,不是更新记录的最高价格 - 变量作用域混乱:main中定义的
printingtime/price/type是独立局部变量,和Books对象内录入的成员变量完全不互通,判断逻辑根本读不到用户输入的图书数据
- 相等判断误用赋值号:
- 类设计逻辑失效:三个派生类没有实现任何筛选逻辑,只是直接调用基类的print方法;基类print方法还写了无意义的内置循环,完全没有做分类匹配的逻辑。
修复后可运行代码
#include <iostream> #include <string.h> using namespace std; class Books { protected: char name[25]; int ISBN; int printingtime; double price; char type[25]; public: // 录入单本图书信息 void getBookdetails() { cout <<"enter book name: "; cin >> name; cout <<"enter book ISBN: "; cin >> ISBN; cout <<"enter book printingtime: "; cin >> printingtime; cout <<"enter book price: "; cin >> price; cout <<"enter book type: "; cin >> type; cout << endl; } // 输出单本图书信息 virtual void print() { cout <<"book name: "<< name << endl; cout <<"book ISBN: "<< ISBN << endl; cout <<"book printingtime: "<< printingtime << endl; cout <<"book price: "<< price << endl; cout <<"book type: "<< type << endl; cout << endl; } // 提供属性读取接口供筛选逻辑使用 int getPrintTime() { return printingtime; } const char* getType() { return type; } double getPrice() { return price; } }; int main() { const int size=2; Books books[size]; double it_high_price = 0.0; int it_high_count = 0; // 循环录入所有图书 for(int i=0; i<size; i++) { cout << "--- 录入第" << i+1 << "本图书信息 ---" << endl; books[i].getBookdetails(); } // 1. 输出2022年印刷的最新图书 cout << "===== 印刷时间为2022年的最新图书 =====" << endl; for(int i=0; i<size; i++) { if(books[i].getPrintTime() == 2022) { books[i].print(); } } // 2. 输出儿童图书 cout << "===== 儿童图书(类型为childrensbook) =====" << endl; for(int i=0; i<size; i++) { if(strcmp(books[i].getType(), "childrensbook") == 0) { books[i].print(); } } // 3. 查找并输出价格最高的IT类图书 cout << "===== 价格最高的IT类图书 =====" << endl; // 第一遍遍历确定IT类图书最高价格 for(int i=0; i<size; i++) { if(strcmp(books[i].getType(), "IT") == 0) { if(books[i].getPrice() > it_high_price) { it_high_price = books[i].getPrice(); } } } // 第二遍遍历输出所有价格等于最高价的IT图书 for(int i=0; i<size; i++) { if(strcmp(books[i].getType(), "IT") == 0 && books[i].getPrice() == it_high_price) { books[i].print(); it_high_count++; } } cout <<"价格最高的IT类图书共"<< it_high_count <<"本,价格为"<< it_high_price << endl; return 0; }
内容的提问来源于stack exchange,提问作者ken
相关产品推荐
相关产品推荐

