Sorry, for the easy question as I am new to c but I cant figure out how to count the number of lines in the text.
I need the program to output the total value and commissions which I have done but I also need the code to count the amount of lines in the text file. But I can't figure it out with the code I have done.
The text file used has 5 lines with this data:
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
This is the code:
#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{};
// Sum functions
int sumSalary() {
int result{};
for (const TaxPayer& taxPayer : taxPayers)
result = taxPayer.salary;
return result;
}
int sumDeductions() {
int result{};
for (const TaxPayer& taxPayer : taxPayers)
result = taxPayer.deductions;
return result;
}
// 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 check()
{
std::ifstream sourceFile(ffname);
if (!sourceFile.is_open())
{
cout << "No file found, please try again" << endl;
return;
}
int i = 0;
TaxPayers taxPayers{};
sourceFile >> taxPayers;
std::cout << endl << "Processing " << ffname << endl;
cout << "\n\n";
cout << "Checking Report" << endl;
cout << "----------------" << endl;
cout << "File:" << ffname << endl;
cout << "Count of Records : " << i << endl;
cout << "Total Value : " << taxPayers.sumSalary() << endl;
cout << "Total Commissions : " << taxPayers.sumDeductions() << endl;
}
int main()
{
check();
}
Mainly, I don't know where to put a count for the lines.
This should be the output of the program but with the total count being 5. Thank you for the help in advance.
CodePudding user response:
I think that this should work in your case:
int i = taxPayers.taxPayers.size();
CodePudding user response:
Looks like you may need to split your if
statement:
unsigned int line_count = 0U;
if (std::getline(is, line))
{
line_count;
if (not line.empty())
{
Every time you read a text line into the string variable, increment a counter.
CodePudding user response:
I started investigating why it displays zero as count-of-records. I've found that this line prints the count:
cout << "Count of Records : " << i << endl;
So, you print out the value of the variable i
. I found only one point where the variable i
is mentioned, some lines above:
int i = 0;
It's a proper initialization. But there's no more reference to i
. So, the problem is: your program actually doesn't count the lines.
I would say, you forgot it, but as you haven't figured out it yourself, I have to say, this exercise is too hard for you. You may try to understand the concept of variables etc., read some tutorials, or watch videos in the topics.
CodePudding user response:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string filename; // Name of the file
cout<<"Enter filename:";
cin>>filename;
string line; // To read each line from code
int count=0; // Variable to keep count of each line
ifstream mFile (filename);
if(mFile.is_open())
{
while(mFile.peek()!=EOF)
{
getline(mFile, line);
count ;
}
mFile.close();
cout<<"Number of lines in the file are: "<<count<<endl;
}
else
cout<<"Couldn't open the file\n";
return 0;
}
Code taken from java2blog.com