Search code examples
c++fstreamifstreamofstream

Fstream is draining the life out of me


I have a code writing assignment for my computer class and it includes fstream classes. I have to write a code that manages and stores user data.
I had a hard time understanding this concept in class and C++ text books just want to keep showing me IPO charts not codes.
I would appreciate if someone can help me with my code and how I can store the users input data to be viewed later on notepad.
thank you in advance for all the help, I have been searching for days and could not figure it out.

#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;
	}


}


Solution

  • You can adjust showData function to use std::ofstream indstead of 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;
    }
    

    Or to reuse the code, you could use function template, or even better, pass your stream into the function (std::ostream is base class of std::ofstream and std::cout is std::ostream itself)

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

    And change couts to oss. You can then use it to write to console:

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

    Or file:

    std::ofstream ofs(filePath);
    
    if(!ofs.is_open())
    {
        // error
    }
    
    writeData(stats, players, ofs);
    

    I hope this solves your doubts.