I have a text file with data that looks like this: Thomas Robinson 17 28 10 16 1
ID: 3640528 • Letter: I
Question
I have a text file with data that looks like this:Thomas Robinson 17 28 10 16 10 11 12 13 8 9 1 1 1 0 1
Tyshawn Taylor 27 24 12 9 20 2 4 0 0 5 0 1 0 0 0
Jeff Whithey 6 2 11 9 18 2 1 7 8 11 1 0 4 4 9
Elijah Johnson 5 8 21 6 5 7 2 3 3 5 0 0 0 0 0
Travis Releford 4 7 4 12 4 2 3 7 5 6 0 0 1 0 0
Conner Teahan 5 12 4 11 2 2 1 8 1 2 0 0 1 0 0
Kevin Young 4 5 4 10 0 2 8 2 2 1 0 4 1 0 0
Justin Wesley 2 1 0 0 0 4 1 1 1 0 2 0 0 0 0
Where the first two positions are names, the next 5 positions are points scored in five games, the next five are rebounds in the same 5 games respectively, and the final five positions are blocks.
I am trying to read this information into a table organized something like this:
Game Log
----------------------------------------------
Player Name: <firstname> <lastname>
----------------------------------------------
Game # Points Rebounds Blocks
1
2
3
4
5
----------------------------------------------
I don't know how to open the input or output files using a variable declared by the user or how to declare a function of prototype: void generateStates(ifstream& infile, ofstream& outfile).
None of my attempts have come close to working.
Explanation / Answer
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
//PROTOTYPES
void generateStates(ifstream&, ofstream& outfile);
//LOCAL DECLARATIONS
char infileName[40];
char outfileName[40];
ifstream infile; //file in
ofstream outfile; //file out
///
///get input file name and output file name from user
///
cout << "Enter input file: ";
cin >> infileName; //get input file name
infile.open(infileName); //open file
if (!infile) //check if input file opened successfully
{
cerr << "Cannot open input file. Program aborted. ";
exit(1);
}
cout << "Enter output file: ";
cin >> outfileName; //get output file name
outfile.open(outfileName); //open file
if (!outfile) //check if output file opened successfully
{
cerr << "Cannot open output file. Program aborted. ";
exit(1);
}
///
///generate game log
///
generateStates(infile, outfile);
cout << " Successfully printed out game log to " << outfileName << endl;
///
///close all files before exiting
///
outfile.close(); //after writing to file, you must CLOSE the file
infile.close(); //do the same thing wi file in
cout << endl;
cin.sync();
cin.get();
return 0;
}
//---------------------------------------------------------
// FUNCTION DEFINITIONS
//---------------------------------------------------------
void generateStates(ifstream& infile, ofstream& outfile)
{
char firstName[20];
char lastName[30];
int points[5];
int rebounds[5];
int blocks[5];
outfile << "Game log "; //print header
//read and write player stat one by one
while (infile >> firstName) //while can read in firstName
{
infile >> lastName; //read in lastName
for (int i = 0; i < 5; i++) //read in 5 game points
infile >> points[i];
for (int i = 0; i < 5; i++) //read in 5 rebounds
infile >> rebounds[i];
for (int i = 0; i < 5; i++) //read in 5 blocks
infile >> blocks[i];
//write player info to outFile
outfile << "---------------------------------------------- ";
outfile << "Player Name: " << firstName << " " << lastName << endl;
outfile << "---------------------------------------------- ";
outfile << "Game # Points Rebounds Blocks ";
for (int i = 0; i < 5; i++)
{
outfile << setw(3) << i + 1
<< setw(11) << points[i]
<< setw(11) << rebounds[i]
<< setw(10) << blocks[i] << endl;
}
outfile << "---------------------------------------------- ";
}
}
**Sample run**
Enter input file: gamestat1.dat
Enter output file: gamelog1.txt
Successfully printed out game log to gamelog1.txt
**Output file content**
Game log
----------------------------------------------
Player Name: Thomas Robinson
----------------------------------------------
Game # Points Rebounds Blocks
1 17 11 1
2 28 12 1
3 10 13 1
4 16 8 0
5 10 9 1
----------------------------------------------
----------------------------------------------
Player Name: Tyshawn Taylor
----------------------------------------------
Game # Points Rebounds Blocks
1 27 2 0
2 24 4 1
3 12 0 0
4 9 0 0
5 20 5 0
----------------------------------------------
----------------------------------------------
Player Name: Jeff Whithey
----------------------------------------------
Game # Points Rebounds Blocks
1 6 2 1
2 2 1 0
3 11 7 4
4 9 8 4
5 18 11 9
----------------------------------------------
----------------------------------------------
Player Name: Elijah Johnson
----------------------------------------------
Game # Points Rebounds Blocks
1 5 7 0
2 8 2 0
3 21 3 0
4 6 3 0
5 5 5 0
----------------------------------------------
----------------------------------------------
Player Name: Travis Releford
----------------------------------------------
Game # Points Rebounds Blocks
1 4 2 0
2 7 3 0
3 4 7 1
4 12 5 0
5 4 6 0
----------------------------------------------
----------------------------------------------
Player Name: Conner Teahan
----------------------------------------------
Game # Points Rebounds Blocks
1 5 2 0
2 12 1 0
3 4 8 1
4 11 1 0
5 2 2 0
----------------------------------------------
----------------------------------------------
Player Name: Kevin Young
----------------------------------------------
Game # Points Rebounds Blocks
1 4 2 0
2 5 8 4
3 4 2 1
4 10 2 0
5 0 1 0
----------------------------------------------
----------------------------------------------
Player Name: Justin Wesley
----------------------------------------------
Game # Points Rebounds Blocks
1 2 4 2
2 1 1 0
3 0 1 0
4 0 1 0
5 0 0 0
----------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.