gpt4 book ai didi

c++ - Fstream 正在耗尽我的生命

转载 作者:行者123 更新时间:2023-11-30 05:46:10 28 4
gpt4 key购买 nike

我的计算机类(class)有一个代码编写作业,其中包括 fstream 类(class)。我必须编写一个代码来管理和存储用户数据。
我在类里面很难理解这个概念,而 C++ 教科书只想继续向我展示 IPO 图表而不是代码。
如果有人可以帮助我处理我的代码以及如何存储用户输入的数据以便稍后在记事本上查看,我将不胜感激。
预先感谢您提供的所有帮助,我已经搜索了好几天,但无法弄清楚。

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

const int MAXLASTNAME = 20;
const int MAXFIRSTNAME = 10;
const int MAXPLAYERS = 20;

struct Baseball {
char FirstName[MAXFIRSTNAME+1];
char LastName[MAXLASTNAME+1];
float AB;
float singles;
float doubles;
float triples;
float HR;
float walks;
double BA;
double SA;
double OBA;
};

int getData(Baseball[]);
void showData(Baseball[], int);

int main()
{
Baseball stats[MAXPLAYERS];

int players = getData(stats);
showData(stats, players);

}
//function: int getData
//description: get the data for all the players the user wishes to input
//input: numerical data for averages and char for name of player
//output: none
int getData(Baseball stats[])
{
int i, players;

cout << "How many aquasocks players would you like to enter data for(1-20): ";
cin >> players;
cout << endl;



for(i=0;i<players;i++) {
cout << "Please enter aquasock #" << i+1 << "'s first name: ";
cin >> stats[i].FirstName;

cout << "Please enter aquasock #" << i+1 << "'s last name: ";
cin >> stats[i].LastName;

cout << "Please enter the number of at bats for player #" << i+1 << ": ";
cin >> stats[i].AB;

cout << "Please enter the number of singles for player #" << i+1 << ": ";
cin >> stats[i].singles;

cout << "Please enter the number of doubles for player #" << i+1 << ": ";
cin >> stats[i].doubles;

cout << "Please enter the number of triples for player #" << i+1 << ": ";
cin >> stats[i].triples;

cout << "Please enter the number of home runs for player #" << i+1 << ": ";
cin >> stats[i].HR;

cout << "Please enter the number of walks for player #" << i+1 << ": ";
cin >> stats[i].walks;
}


double sum = 0;

for(i=0; i<players; i++) {
sum += (stats[i].singles + stats[i].doubles + stats[i].triples + stats[i].HR);

cout.precision(3);
cout.setf(ios::fixed);
stats[i].BA = sum / (stats[i].AB);
stats[i].SA = (stats[i].singles + (2*stats[i].doubles) + (3*stats[i].triples) + (4*stats[i].HR)) / (stats[i].AB);
stats[i].OBA = (sum + stats[i].walks) / (stats[i].AB + stats[i].walks);
}

return players;
}
//function: void showData
//description: uses the data from function int getData to make a calculated chart of the players statistics.
//input: none
//output: chart of the users inputed data from function int getData
void showData(Baseball stats[], int players)
{
int i;
cout << endl << endl;
cout << "lets see how this aquasock stats stack up!\n\n";


cout << "First Name" << setw(14) << "Last Name" << setw(6) << "AB" << setw(6) << "1B" << setw(6) << "2B";
cout << setw(6) << "3B" << setw(6) << "HR" << setw(6) << "BB" << setw(6) << "BA" << setw(6) << "SA";
cout << setw(6) << "OBA" << "\n";


cout << "----------" << setw(14) << "---------" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--";
cout << setw(6) << "--" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--";
cout << setw(6) << "---" << "\n";


for(i=0; i<players; i++)
{
cout << stats[i].FirstName
<< setw(15) << stats[i].LastName
<< setw(8) << stats[i].AB
<< setw(8) << stats[i].singles
<< setw(8) << stats[i].doubles
<< setw(8) << stats[i].triples
<< setw(8) << stats[i].HR
<< setw(8) << stats[i].walks
<< setw(8) << stats[i].BA
<< setw(8) << stats[i].SA
<< setw(8) << stats[i].OBA
<< endl;
}


}

最佳答案

您可以调整showData要使用的函数 std::ofstream而不是 std::cout .

bool writeData(Baseball stats[], int players, std::string const& filePath)
{
std::ofstream ofs(filePath);

if(!ofs.is_open())
return false; // failed to open stream

ofs << endl << endl;
ofs << "lets see how this aquasock stats stack up!\n\n";


ofs << "First Name" << setw(14) << "Last Name" << setw(6) << "AB" << setw(6) << "1B" << setw(6) << "2B";
ofs << setw(6) << "3B" << setw(6) << "HR" << setw(6) << "BB" << setw(6) << "BA" << setw(6) << "SA";
ofs << setw(6) << "OBA" << "\n";


ofs << "----------" << setw(14) << "---------" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--";
ofs << setw(6) << "--" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--";
ofs << setw(6) << "---" << "\n";


for(int i = 0; i < players; ++i)
{
ofs << stats[i].FirstName
<< setw(15) << stats[i].LastName
<< setw(8) << stats[i].AB
<< setw(8) << stats[i].singles
<< setw(8) << stats[i].doubles
<< setw(8) << stats[i].triples
<< setw(8) << stats[i].HR
<< setw(8) << stats[i].walks
<< setw(8) << stats[i].BA
<< setw(8) << stats[i].SA
<< setw(8) << stats[i].OBA
<< endl;
}

return true;
}

或者要重用代码,您可以使用函数模板,或者更好的是,将流传递给函数(std::ostreamstd::ofstream 的基类,std::coutstd::ostream 本身)

void writeData(Baseball stats[], int players, std::ostream &os);

并更改cout s 至 os秒。然后您可以使用它写入控制台:

writeData(stats, players, std::cout);

或文件:

std::ofstream ofs(filePath);

if(!ofs.is_open())
{
// error
}

writeData(stats, players, ofs);

希望这能解决你的疑惑。

关于c++ - Fstream 正在耗尽我的生命,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29064857/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com