My code below should take information from a text file that I have already creat
ID: 3637232 • Letter: M
Question
My code below should take information from a text file that I have already created. A example of that text file is:Robert Fisher
3 1 1 2 1
Robert Smith
5 2 2 1 0
After taking the information from the text file the program should calculate and present the data in a output file. A example of the output text file would look something like this:
Chess Player Report
Player Pawns Knights Bishops Rooks Queens Total
Robert Fisher 3 1 1 2 1 29
Robert Smith 5 2 2 1 0 22
My problem is the program is making the output file but nothing is being written into it. My question is where am i going wrong in my code?
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main ()
{
ofstream outFile;
ifstream inFile;
char input[30];
char output[30];
char Player[21];
int Pawn, Knight, Bishop, Rook, Queen, Total;
// Output report headings
outFile << "Chess Player Report" << endl << endl;
outFile << "Player" << setw(20) << " "
<< "Pawns" << setw(5) << " "
<< "Knights" << setw(5) << " "
<< "Bishops" << setw(5) << " "
<< "Rooks" << setw(5) << " "
<< "Queens" << setw(5) << " "
<< "Total" << endl << endl;
// and direct results to the output file
do
{
cout << "What is the input file?: ";
cin >> input;
inFile >> ws;
inFile.open(input);
cout << "what is the output file?: ";
cin >> output;
outFile.open(output);
inFile >> Pawn;
inFile >> Knight;
inFile >> Bishop;
inFile >> Rook;
inFile >> Queen;
inFile >> ws;
Total = Pawn * 1 + Knight * 3 + Bishop * 3 + Rook * 5 + Queen * 9;
inFile >> Total;
outFile << left << setw(20) << Player
<< right << setw(9) << Pawn
<< right << setw(11) << Knight
<< right << setw(12) << Bishop
<< right << setw(11) << Rook
<< right << setw(11) << Queen
<< right << setw(11) << Total << endl;
}
while (!inFile.eof());
inFile.close();
outFile.close();
return 0;
}
Explanation / Answer
please rate - thanks
you have to open the files before the loops
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main ()
{
ofstream outFile;
ifstream inFile;
char input[30];
char output[30];
char Player[21];
char ws;
int Pawn, Knight, Bishop, Rook, Queen, Total;
cout << "What is the input file?: ";
cin >> input;
inFile.open(input);
// Output report headings
cout << "what is the output file?: ";
cin >> output;
outFile.open(output);
outFile << "Chess Player Report" << endl << endl;
outFile << "Player" << setw(20) << " "
<< "Pawns" << setw(5) << " "
<< "Knights" << setw(5) << " "
<< "Bishops" << setw(5) << " "
<< "Rooks" << setw(5) << " "
<< "Queens" << setw(5) << " "
<< "Total" << endl << endl;
inFile.getline(Player,21);
// and direct results to the output file
do
{
inFile >> Pawn;
inFile >> Knight;
inFile >> Bishop;
inFile >> Rook;
inFile >> Queen;
Total = Pawn * 1 + Knight * 3 + Bishop * 3 + Rook * 5 + Queen * 9;
outFile << left << setw(20) << Player
<< right << setw(9) << Pawn
<< right << setw(11) << Knight
<< right << setw(14) << Bishop
<< right << setw(14) << Rook
<< right << setw(14) << Queen
<< right << setw(14) << Total << endl;
inFile.ignore(10,' ');
inFile.getline(Player,21);
}while (inFile);
inFile.close();
outFile.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.