I need to list each line in the text file with specific elements but I can get the list numbering and the correct output for the first line.
But as the program, lists the lines for the text file, it keeps on only listing the last line. I also need to have them in order of id.
The data in the imported text file:
7101003,Mike,23 boinig road,2615,48000,12000,0
7201003,Jane Philips,29 boinig cresent,2616,47000,12000,0
7301003,Philip Jane,23 bong road,2615,49000,000,0
7401004,Peta,23 bong bong road,2615,148000,19000,0
7101205,Abdulla,23 Station st,2615,80000,21000,0
The code I am using :
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <sstream>
using namespace std;
string ffname = "C:\\Users\\chuboi\\Documents\\ST2TaxExample\\Taxpayerdata.txt";
struct TaxPayer {
// Data part
std::string taxpayerId{};
std::string name{};
std::string address{};
std::string postcode{};
int salary{};
int deductions{};
double taxDue{};
// Extractor
friend std::istream& operator >> (std::istream& is, TaxPayer& tp) {
// Read a complete line
std::string line{};
if (std::getline(is, line) and not line.empty()) {
// Put it into a stringstream for further extraction
std::istringstream iss{ line };
// Now, extract the data parts
std::getline(iss, tp.taxpayerId, ',');
std::getline(iss, tp.name, ',');
std::getline(iss, tp.address, ',');
std::getline(iss, tp.postcode, ',');
// Read and convert
std::getline(iss, line, ',');
tp.salary = std::stoi(line);
std::getline(iss, line, ',');
tp.deductions = std::stoi(line);
std::getline(iss, line, ',');
tp.taxDue = std::stod(line);
}
return is;
}
// Simple inserter
friend std::ostream& operator << (std::ostream& os, const TaxPayer& tp) {
return os << tp.taxpayerId << ' ' << tp.name << ' ' << tp.address << ' ' << tp.postcode
<< ' ' << tp.salary << ' ' << tp.deductions << ' ' << tp.taxDue << '\n';
}
};
struct TaxPayers {
// Data
std::vector<TaxPayer> taxPayers{};
string showinfo() {
string info{};
for (const TaxPayer& taxPayer : taxPayers)
info = taxPayer.taxpayerId " " taxPayer.name " "
taxPayer.address " " taxPayer.postcode;
return info;
}
// Extractor
friend std::istream& operator >> (std::istream& is, TaxPayers& tp) {
// clar old data
tp.taxPayers.clear();
// Read all new existing data and store in vector
TaxPayer taxPayer{};
while (is >> taxPayer)
tp.taxPayers.push_back(taxPayer);
return is;
}
// Simple inserter
friend std::ostream& operator << (std::ostream& os, const TaxPayers& tp) {
for (const TaxPayer& taxPayer : tp.taxPayers)
os << taxPayer;
return os;
}
};
void opt_L()
{
std::ifstream sourceFile(ffname);
if (!sourceFile.is_open())
{
cout << "No file found, please check for files again" << endl;
return;
}
TaxPayers taxPayers{};
sourceFile >> taxPayers;
for (int i = 0; i < taxPayers.taxPayers.size(); i )
{
cout << "[" << i << "] - " << taxPayers.showinfo() << endl;
}
}
int main()
{
opt_L();
}
It outputs like this:
[0] - 7101205 Abdulla 23 Station st 2615
[1] - 7101205 Abdulla 23 Station st 2615
[2] - 7101205 Abdulla 23 Station st 2615
[3] - 7101205 Abdulla 23 Station st 2615
[4] - 7101205 Abdulla 23 Station st 2615
While I need it to output like this:
[0] - 7101003 Mike 23 boinig road 2615
[1] - 7201003 Jane Philips 29 boinig cresent 2616
[2] - 7301003 Philip Jane 23 Station st 2615
[3] - 7401004 Peta 23 23 bong bong road 2615
[4] - 7101205 Abdulla 23 Station st 2615
Thank you so much for the help in advance.
CodePudding user response:
I would write a function
string showinfo(size_t i) {
string info = taxPayers[i].taxpayerId " " taxPayers[i].name " "
taxPayers[i].address " " taxPayers[i].postcode;
return info;
}
and call that with
for (int i = 0; i < taxPayers.taxPayers.size(); i )
{
cout << "[" << i << "] - " << taxPayers.showinfo(i) << endl;
}