?I have no clue on how to get the single digit numbers to align with the rest of
ID: 3783581 • Letter: #
Question
?I have no clue on how to get the single digit numbers to align with the rest of the columns. I have used tabs and setw but nothing I have done has fixed the alignment. Is there a way to fix that?
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
ifstream infile;
ofstream outfile;
//Maximum number of Chocolate Types
const int ARRAY_SIZE = 25;
int printReport(int ChocTypeArray[], int ChocPiecesArray[])
{
//Opens output file
outfile.open("ChocolateCo_Production_Results.txt");
//Print Title
outfile << "======================================================================================================" << endl;
outfile << "================================ O-So-Good Chocolate Company Report ==================================" << endl;
outfile << "======================================================================================================" << endl;
//Print Header
outfile << "Chocolate Type" << setw(24) << "Pieces" << setw(23) << " Batches" << setw(24) << " Average/Batch" << endl;
outfile << "======================================================================================================" << endl;
for (int count = 0; count < ARRAY_SIZE; count++)
{
float avg = 0;
if (ChocTypeArray[count] > 0)
{
avg = (float)ChocTypeArray[count] / (float)ChocPiecesArray[count];
}
outfile << count + 1 << " " << ChocTypeArray[count] << setw(23) << ChocPiecesArray[count] << setw(24) << avg << endl;
}
// Closes outout file
outfile.close();
return 0;
}
int main()
{
int ChocTypeArray[ARRAY_SIZE];
int ChocPiecesArray[ARRAY_SIZE];
//Initialize arrays
for (int count = 0; count < ARRAY_SIZE; count++)
{
ChocTypeArray[count] = 0;
ChocPiecesArray[count] = 0;
}
//Try to find data file
infile.open("Chocolates.txt");
if (!infile)
{
cout << "Failed to open Chocolates.txt. Make sure it is in the same directory as the program." << endl;
return -1;
}
//Read data until the end of file
int _chocType, _chocPieces;
while (!infile.eof())
{
infile >> _chocType >> _chocPieces;
// Validates if chocType is between 1 and 25
if (_chocType < 1 || _chocType > ARRAY_SIZE)
{
continue; //skip
}
else
{
//Adjust for array index
_chocType--;
//Add the pieces to their appropriate type
ChocTypeArray[_chocType] = ChocTypeArray[_chocType] + _chocPieces;
ChocPiecesArray[_chocType]++;
}
}
//Print the report
printReport(ChocTypeArray, ChocPiecesArray);
return 0;
}
Explanation / Answer
See one possible solution is to use following line:
in place of using setw(23).
Follow this url for better explanation:
http://www.cplusplus.com/forum/beginner/41939/
Hope it helps.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.