Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ChocolateCo-Production Results Notepad File Edit Format View Help 0-So-Good Chocolate Company Report Chocolate Type Pieces Batches 32374 22534 32 30875 40 21998 29 29359 33 32849 32648 43 33374 33381 43 33058 43 11 29308 38 2357e 33 13 14 25402 33 15 29193 38 39606 43 17 28935 38 19 27391 38 20 543 21 20238 28 22 3238 30730 23 46 24 24496 25 Average/Batch 735.773 704.188 771.875 758.552 889.667 746.568 759.256 814 776.302 768.791 771.263 714.242 769.758 768.237 921.07 761.447 720.816 543 722.786 647.6 668.043 720.471 Ln 18, Col 7

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.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote