- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的计算机类(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::ostream
是 std::ofstream
的基类,std::cout
是 std::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/
我正在开发一个使用多个 turtle 的滚动游戏。玩家 turtle 根据按键命令在 Y 轴上移动。当危害和好处在 X 轴上移动时,然后循环并改变 Y 轴位置。我尝试定义一个名为 colliding(
我不明白为什么他们不接受这个作为解决方案,他们说这是一个错误的答案:- #include int main(void) { int val=0; printf("Input:- \n
我正在使用基于表单的身份验证。 我有一个注销链接,如下所示: 以及对应的注销方法: public String logout() { FacesContext.getCurren
在 IIS7 应用程序池中有一个设置 Idle-time out 默认是 20 分钟,其中说: Amount of time(in minutes) a worker process will rem
我是一名优秀的程序员,十分优秀!