C++击球率程序文件IO问题:无法输出最高/最低击球率及对应球员
Let's break down and fix the issues in your batting average calculator step by step:
1. Fix the printHigh() and printLow() functions to display player names
Right now, these functions only print header text—they don't actually iterate through the Stack instances holding the player names for highest/lowest averages. We'll modify them to copy the stack to a temporary one (so we don't empty the original) and print each player:
void Player::printHigh() { cout << "Highest batting average is: " << highest << endl; cout << "Players with the highest batting average: " << endl; // Use a temp stack to preserve the original data Stack<string> tempHigh = high; while (!tempHigh.empty()) { cout << " - " << tempHigh.peek() << endl; tempHigh.pop(); } } void Player::printLow() { cout << "Lowest batting average is: " << lowest << endl; cout << "Players with the lowest batting average: " << endl; Stack<string> tempLow = low; while (!tempLow.empty()) { cout << " - " << tempLow.peek() << endl; tempLow.pop(); } }
2. Fix the main function's broken file I/O logic
Your current main has several critical issues:
- It checks
if(!infile)after closing the input file, which will always trigger the error message - It overwrites your original input file (
avgs.txt) when opening it for output - It doesn't actually write any meaningful results to the output file
Here's the corrected version, with clear input/output separation and proper error handling:
#include "Player.h" #include "Stack.h" #include <fstream> #include <iostream> using namespace std; int main() { Player x; string name; double avg; // Step 1: Read input file (check for success first) ifstream infile("avgs.txt"); if (!infile) { cout << "Unable to open input file: avgs.txt" << endl; return 1; } while (infile >> name >> avg) { x.insertPlayer(name, avg); } infile.close(); // Step 2: Print results to console cout << "\n--- Batting Average Results ---" << endl; x.printHigh(); cout << endl; x.printLow(); // Step 3: Write results to a separate output file (avoid overwriting input) ofstream outfile("results.txt"); if (!outfile) { cout << "Error opening output file: results.txt" << endl; return 1; } // Write highest average data to file outfile << "Highest batting average is: " << x.highest << endl; outfile << "Players with the highest batting average: " << endl; Stack<string> tempHigh = x.high; while (!tempHigh.empty()) { outfile << " - " << tempHigh.peek() << endl; tempHigh.pop(); } outfile << endl; // Write lowest average data to file outfile << "Lowest batting average is: " << x.lowest << endl; outfile << "Players with the lowest batting average: " << endl; Stack<string> tempLow = x.low; while (!tempLow.empty()) { outfile << " - " << tempLow.peek() << endl; tempLow.pop(); } outfile.close(); cout << "\nResults saved to results.txt!" << endl; return 0; }
3. Add safe initialization to the Player class
To avoid edge cases (like an empty input file), add a constructor to initialize highest and lowest to logical default values for batting averages:
In Player.h:
class Player { public: double lowest; double highest; Stack<string> low; Stack<string> high; // Initialize lowest to max possible avg (1.0), highest to min (0.0) Player() : lowest(1.0), highest(0.0) {} void insertPlayer(string name, double batAvg); void printHigh(); void printLow(); };
4. Clean up the Stack class
Your Stack class doesn't need to inherit from exception—this serves no purpose since you're already throwing standard exceptions in pop() and peek(). Fix the class definition in Stack.h:
template <class T> class Stack { // Remove ": public exception" private: vector<T> myStacks; public: void push(T const& uStack); void pop(); T peek() const; bool empty() const { return myStacks.empty(); } };
Testing with your sample input
Using your avgs.txt content:
Orlando .775 Charles .606 J.D. .775 Gina .400 Sam .702 Rich .686
The program will now output to console:
Player Orlando has an average of: 0.775 Player Charles has an average of: 0.606 Player J.D. has an average of: 0.775 Player Gina has an average of: 0.4 Player Sam has an average of: 0.702 Player Rich has an average of: 0.686 --- Batting Average Results --- Highest batting average is: 0.775 Players with the highest batting average: - J.D. - Orlando Lowest batting average is: 0.4 Players with the lowest batting average: - Gina Results saved to results.txt!
And results.txt will contain identical results.
内容的提问来源于stack exchange,提问作者Mgonz

