C++文件读写编程作业问题:无法正确展示房产经纪人销售数据
C++房产销售数据处理程序修复
问题背景
我是编程入门课程的学生,正在完成一项涉及文件操作的C++作业。作业要求程序实现以下功能:
- 录入任意数量房产经纪人的4套住宅和1套公寓销售额;
- 将数据写入文件;
- 从文件读取数据;
- 计算每位经纪人的总销售额和平均销售额;
- 计算公司平均销售额;
- 找出销售额最高的经纪人;
- 按顺序展示结果:先所有经纪人的销售信息,再公司平均销售额,最后销售额最高的经纪人。
目前编写的代码无法按预期展示结果,现有代码如下:
#include <iostream> #include <iomanip> #include <string> #include <cmath> #include <fstream> using namespace std; int main() { double hSale, aSale, totalSales, companySale = 0.0, avgSales, companyAvg; int numAgents = 0; const int HOME_SALES = 4, APPT_SALE = 1; char go = 'Y'; string fileName, agentName, highAgent; ofstream outputFile; cout << "\nEnter the file name: "; getline(cin, fileName); outputFile.open(fileName); while (go == 'Y' || go == 'y') { cout << "\nEnter the agent's name: "; cin >> agentName; numAgents++; outputFile << agentName << "\n"; for (int a = 0; a < HOME_SALES; a++) { cout << "\nEnter a house sale : $"; cin >> hSale; while (hSale < 0 || hSale > 200) { cout << "\nThe sale must be between $0.00 and $200.00. Please try again: "; cin >> hSale; } outputFile << hSale << " \n"; } for (int b = 0; b < APPT_SALE; b++) { cout << "\nEnter an appartment sale: $"; cin >> aSale; while (aSale < 0 || aSale > 200) { cout << "\nThe sale must be between $0.00 and $200.00. Please try again: "; cin >> aSale; } outputFile << aSale << " \n"; } cout << "\nEnter another agent's sales? Y for yes, N for no: "; cin >> go; while (go != 'Y' && go != 'y' && go != 'N' && go != 'n') { cout << "\nError. You must enter Y or N. Please re- enter: "; cin >> go; } } outputFile.close(); ifstream textFileIn; textFileIn.open(fileName); if (textFileIn.fail()) { cout << "\nError. File not found. Ending program."; cout << endl << endl; system("pause"); return 0; } double higestSale = 0.0; while (textFileIn >> agentName) { totalSales = 0.0; for (int s = 0; s < HOME_SALES; s++) { textFileIn >> hSale; totalSales += hSale; } if (totalSales > higestSale) { higestSale = totalSales; highAgent = agentName; } companySale += totalSales; avgSales = totalSales / HOME_SALES; companyAvg = companySale / numAgents; cout << setprecision(2) << fixed << endl << endl; cout << "\nAgent " << agentName << "'s total sales were $" << totalSales << " and average sales were $" << avgSales; cout << "\nThe company average sales are $" << companyAvg; cout << "\nAgent " << highAgent << " had the higest sales of $" << higestSale; } cout << endl << endl; system("pause"); return 0; }
代码问题分析
- 输出逻辑错误:在读取文件的循环中,每次处理一个经纪人就输出公司平均销售额和最高经纪人信息,导致重复输出,不符合先展示所有经纪人信息的预期。
- 数据计算遗漏:总销售额只计算了4套住宅的销售额,完全忽略了公寓销售额,导致数据错误。
- 公司平均计算时机错误:公司平均销售额应该在所有经纪人数据统计完成后再计算,而非每次循环都更新。
- 拼写错误:
higestSale应为highestSale,属于笔误;appartment拼写错误,应为apartment。 - 输入换行符残留:
getline(cin, fileName)后,后续用cin读取数据时可能残留换行符,导致输入异常。
修复后的代码
#include <iostream> #include <iomanip> #include <string> #include <fstream> #include <vector> using namespace std; // 存储经纪人销售信息的结构体 struct AgentSales { string name; double total; double average; }; int main() { double hSale, aSale, totalSales, companySale = 0.0, companyAvg; int numAgents = 0; const int HOME_SALES = 4, APPT_SALE = 1; char go = 'Y'; string fileName, highAgent; double highestSale = 0.0; vector<AgentSales> agentData; // 存储所有经纪人数据 ofstream outputFile; cout << "\nEnter the file name: "; getline(cin, fileName); outputFile.open(fileName); while (go == 'Y' || go == 'y') { cout << "\nEnter the agent's name: "; cin >> agentName; // 清理cin缓冲区的换行符,避免后续输入异常 cin.ignore(); numAgents++; outputFile << agentName << "\n"; totalSales = 0.0; for (int a = 0; a < HOME_SALES; a++) { cout << "\nEnter a house sale : $"; cin >> hSale; while (hSale < 0 || hSale > 200) { cout << "\nThe sale must be between $0.00 and $200.00. Please try again: "; cin >> hSale; } totalSales += hSale; outputFile << hSale << "\n"; } for (int b = 0; b < APPT_SALE; b++) { cout << "\nEnter an apartment sale: $"; cin >> aSale; while (aSale < 0 || aSale > 200) { cout << "\nThe sale must be between $0.00 and $200.00. Please try again: "; cin >> aSale; } totalSales += aSale; outputFile << aSale << "\n"; } cout << "\nEnter another agent's sales? Y for yes, N for no: "; cin >> go; // 清理换行符 cin.ignore(); while (go != 'Y' && go != 'y' && go != 'N' && go != 'n') { cout << "\nError. You must enter Y or N. Please re-enter: "; cin >> go; cin.ignore(); } } outputFile.close(); ifstream textFileIn; textFileIn.open(fileName); if (textFileIn.fail()) { cout << "\nError. File not found. Ending program."; cout << endl << endl; system("pause"); return 0; } string agentName; while (textFileIn >> agentName) { totalSales = 0.0; // 读取4套住宅销售额 for (int s = 0; s < HOME_SALES; s++) { textFileIn >> hSale; totalSales += hSale; } // 读取1套公寓销售额 textFileIn >> aSale; totalSales += aSale; double avgSales = totalSales / (HOME_SALES + APPT_SALE); agentData.push_back({agentName, totalSales, avgSales}); companySale += totalSales; // 更新最高销售额经纪人 if (totalSales > highestSale) { highestSale = totalSales; highAgent = agentName; } } // 计算公司平均销售额 companyAvg = companySale / numAgents; // 设置输出格式 cout << setprecision(2) << fixed; // 第一步:展示所有经纪人的销售信息 cout << "\n=== 所有经纪人销售信息 ===" << endl; for (const auto& agent : agentData) { cout << "\nAgent " << agent.name << "'s total sales were $" << agent.total << " and average sales were $" << agent.average << endl; } // 第二步:展示公司平均销售额 cout << "\n=== 公司整体销售数据 ===" << endl; cout << "The company average sales are $" << companyAvg << endl; // 第三步:展示销售额最高的经纪人 cout << "\n=== 销售额最高经纪人 ===" << endl; cout << "Agent " << highAgent << " had the highest sales of $" << highestSale << endl; cout << endl << endl; system("pause"); return 0; }
修复说明
- 新增结构体与容器:用
AgentSales结构体存储每个经纪人的姓名、总销售额和平均销售额,用vector统一管理所有经纪人数据,便于后续统一输出。 - 修正数据计算:读取文件时加入公寓销售额的读取与计算,总销售额包含住宅和公寓的全部数据,平均销售额按5笔交易计算(4套住宅+1套公寓)。
- 调整输出逻辑:先遍历
vector输出所有经纪人信息,再计算并输出公司平均销售额,最后输出最高销售额的经纪人,完全符合预期顺序。 - 修复输入问题:在
cin读取后添加cin.ignore()清理缓冲区的换行符,避免getline读取异常。 - 修正拼写错误:将
higestSale改为正确的highestSale,同时修正"appartment"的拼写错误。
内容的提问来源于stack exchange,提问作者user21559827
相关产品推荐
相关产品推荐

