Fstream正在耗尽我的生命

时间:2015-03-15 19:01:04

标签: c++ fstream ifstream ofstream

我为我的计算机类编写了一个代码,它包含了fstream类。我必须编写一个管理和存储用户数据的代码 我很难在课堂上理解这个概念,而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;
	}


}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您可以调整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::ofstreamstd::cout是{{1本身)

std::ostream

void writeData(Baseball stats[], int players, std::ostream &os); 更改为cout s。然后,您可以使用它来写入控制台:

os

或文件:

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

我希望这能解决你的疑虑。