我正在尝试在输出文件中以某种格式打印一些值和文本,并且我正在使用\n
和\t
转义字符来实现格式。但是,我注意到,\t\t\t
前面有一个两位数的数字时,后面的值比一位数字时更远(请参阅下面的output.txt
)。我可以为此做些什么,还是有更好的方法在c ++中以某些方式对齐文本?
parking.cpp:
#include <iostream>
#include <fstream>
#include <vector>
#include "helper.h"
using namespace Stelios;
using namespace std;
int main() { // Stelios Papamichail csd4020
string fileName = "output.txt";
my_class mc; // instance of my_class from helper.h
vector<int> hours;
int totalCharges = 0;
int totalHours = 0;
ofstream ofs(fileName.c_str(),ios_base::app); // Try opening/creating a file for writing(in append mode)
if(!ofs) { // if we couldn't create/open the file for writing, exit
cout << "Error opening output file " << fileName << endl;
exit(-2);
}
ofs << "Customer No. \t Hours \t Charges\n\n";
mc.fillVector(hours,"Please enter how many hours each of the five customers spent in the parking lot:\n","Error : that was not a number. Please try again\n");
for(int i = 0; i < 5; i++) {
ofs << "No. " << i+1 << " \t\t\t " << hours[i] << " \t\t\t " << mc.calculateCharges(hours[i]) << endl;
totalCharges += mc.calculateCharges(hours[i]);
totalHours += hours[i];
}
ofs << "\n\nTotal Hours : " << totalHours << "\nTotal Charges : " << totalCharges << " EUR" << endl;
return 0;
}
output.txt:
Customer No. Hours Charges
No. 1 5 4
No. 2 24 23
No. 3 24 23
No. 4 5 4
No. 5 5 4
Total Hours : 63
Total Charges : 58 EUR