我有这个击球平均计划。我的问题是我无法使用输入/输出文件输出此程序,因此它显示最高/最低平均值以及与它们一起使用的名称。我在main中的代码不正确,因为它只输出"最高的击球平均值是:0"和#34;击球率最高的球员:"没有一个平均值为0所以它不应该输出,并且最高击球平均值旁边应该有一个名字。最低的也是如此。这就是我到目前为止所做的:
#include "Player.h"
#include "Stack.h"
#include <fstream>
using namespace std;
int main()
{
Player x;
string name;
double avg;
ifstream infile("avgs.txt");
while (infile >> name >> avg)
{
x.insertPlayer(name, avg);
}
infile.close();
x.printHigh();
x.printLow();
if(!infile)
{
cout << "Unable to open the file for writing " << endl;
exit(0);
}
ofstream outfile("avgs.txt");
if (!outfile)
{
cout << "Error opening file " << endl;
exit(0);
}
outfile << endl;
}
以下是Player类的文件,以及Stack类,以防需要它:
////Player.h
#include "stdafx.h"
#include <iostream>
#include <string>
#include "Stack.h"
using std::string;
class Player
{
public:
double lowest;
double highest;
Stack<string> low;
Stack <string> high;
void insertPlayer(string name, double batAvg);
void printHigh();
void printLow();
};
这是Player类的.cpp文件:
#include "stdafx.h"
#include <iostream>
#include <string>
#include "Player.h"
using namespace std;
using std::string;
void Player::insertPlayer(string name, double batAvg)
{
cout << "Player " << name << " has an average of: " << batAvg << endl;
if (low.empty() && high.empty())
{
low.push(name);
high.push(name);
highest = lowest = batAvg;
}
else
{
if (batAvg>highest)
{
while (!high.empty())
high.pop();
highest = batAvg;
high.push(name);
}
else if (batAvg == highest)
high.push(name);
else if (batAvg<lowest)
{
while (!low.empty())
low.pop();
lowest = batAvg;
low.push(name);
}
else if (batAvg == lowest)
low.push(name);
}
}
void Player::printHigh()
{
cout << "Highest batting average is: " << highest << endl;
cout << "Players with the highest batting average: " << endl;
}
void Player::printLow()
{
cout << "Lowest batting average is: " << lowest << endl;
cout << "Players with the lowest batting average: " << endl;
}
如果需要我的Stack类:
#ifndef STACK
#define STACK
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
template <class T>
class Stack : public exception
{
private:
vector<T>myStacks;
public:
void push(T const& uStack);
void pop();
T peek() const;
bool empty() const
{
return myStacks.empty();
}
};
template <class T>
void Stack<T>::push(T const& uStack)
{
myStacks.push_back(uStack);
}
template <class T>
void Stack<T>::pop()
{
if (myStacks.empty())
{
throw std::out_of_range("Stack <>::pop(): This is an empty stack ");
}
myStacks.pop_back();
}
template <class T>
T Stack<T>::peek() const
{
if (myStacks.empty())
{
throw std::out_of_range("Stack<>::peek(): This is an empty stack ");
}
return myStacks.back();
}
#endif
我尝试输出的焦点代码是Player.cpp,但需要使用Stack.h和Player.h才能运行它。在main()中,我需要输出名称,平均值和具有最高/最低平均值的人。任何帮助表示赞赏!
文本文件由玩家及其平均值组成,例如: 奥兰多.775 查尔斯.606 J.D.775 吉娜.400 山姆.702 Rich .686
等等。
答案 0 :(得分:0)
您要做的第一件事是在main中x.printLow();
之后删除所有,然后验证avgs.txt
中是否存在预期输入。
您的代码将使用一个换行符覆盖输入文件,因此如果您尝试过一次,则可能无法获得您期望的输入。注意你永远不会看到cout << "Player " << name << " has an average of: " << batAvg << endl;
的输出。这表明insertPlayer
未被调用。
我非常确定找到输入文件是一个问题,因为works with a different input source。该文件的文件与.exe在同一文件夹中吗?它是否在不同的文件夹中,例如源文件在哪里?
一个简单的测试就是将文件的内容复制到cout
,以查看程序找到的内容
int main() {
std::ifstream infile("avgs.txt");
for (std::string line; std::getline(infile, line;) {
std::cout << line << std::endl;
}
}