Hi i modified my code that parse some text document by adding two more lines(some text) in my Input file. The lines are after PLEASE CONTACT YOUR OFFICE. It's two lines but i want to add just the second line to my output file. I add 2 new states in your code that can read each lines but it can't read all the notes from my input file. In my case i have NOTES: with 4 lines and it reads only the first line with notes not all lines. I read all notes with state ReadNotes, until find empty line, then goto state WaitForText1. Here if i found "Text1" goto State WaitForText2 and when i get my line stop reading lines with "tryToRead = false". But my problem here is that i don't get all the note lines, i get only the first line. Can someone tell me Where is the problem?
Working code before adding 2 more lines (get me all the output that i have):
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <fstream>
#include <iterator>
#include <numeric>
#include <algorithm>
struct ClientData
{
// The clients data. Everything stored as string
std::string id{};
std::string date{};
std::string time{};
// The notes will bes trored in a map. Key is not type and value is note count
std::map<unsigned int, unsigned int> notes{};
// Get the overall number of notes for thiy record
size_t getNumberOfNotes() const { size_t sum{ 0 }; for (auto const& [k, v] : notes) sum += v; return sum; }
// Read and parse parts of the input file
friend std::istream& operator >> (std::istream& is, ClientData& cd);
// Out put function. Print data in required format
friend std::ostream& operator << (std::ostream& os, ClientData& cd) {
os << " Client: " << cd.id << "\nDATE: " << cd.date << " Time: " << cd.time;;
for (auto const& [k, v] : cd.notes) os << '\n' << v << " x " << k << " USD";
return os;
}
};
// The inserter operator. Parse the input using a sate machine
std::istream& operator >> (std::istream& is, ClientData& cd)
{
// States for our state machine
enum class States { WaitForDate, ReadDate, WaitForID, WaitForNotes1, WaitForNotes2, ReadNotes};
// Initially or note map is empty
cd.notes.clear();
// Loop conditions
bool tryToRead{ true }; bool failureDuringRead{ false };
// Initial state
States state{ States::WaitForDate };
// Read all line belonging to this record
while (tryToRead && !failureDuringRead) {
std::string line{};
if (std::getline(is, line)) {
// State machine to always do the appropiate operation
switch (state)
{
case States::WaitForDate: // Wait for the line
if (line == "DATA/DATE TIME TERMINAL")
state = States::ReadDate; // If found, goto next state
break;
case States::ReadDate:
{
// extract the date and the time
std::istringstream iss(line);
iss >> cd.date >> cd.time;
}
state = States::WaitForID; // Next:: Wait for the ID string
break;
case States::WaitForID:
// We are waiting for the ID string. Denominated with "ID" or "CARD"
if ((line.find("CARD:") == 0) || (line.find("ID:") == 0))
{
// Found, extract is string
std::string idOrCard{}; // Dummy. Will not be used
std::istringstream iss(line);
iss >> idOrCard >> cd.id;
state = States::WaitForNotes1; // Next: wait for "Notes" String
}
break;
case States::WaitForNotes1:
if (line.find("NOTES:") == 0) // If we can find the NOTES string
state = States::WaitForNotes2; // Then Next: Read the empty line after "NOTES:"
break;
case States::WaitForNotes2:
// Read empty line
state = States::ReadNotes;
break;
case States::ReadNotes:
// now read the notes. As long as there is data avaliable (and no empty line)
if (line != "") {
std::string usd{}; // Dummy. Will not be used
std::istringstream iss(line);
// Extract notes
unsigned int type{}; unsigned int count{};
iss >> type >> usd >> count;
// And put it in map
cd.notes[type] += count;
}
else {
// Empty line found. Done with this record. Everything read.
tryToRead = false; // Stop reading lines
state = States::WaitForDate; // Wait for Date again
}
break;
default:
std::cerr << "Invalid State\n";
failureDuringRead = true;
tryToRead = false;
}
}
else {
// Error while reading
cd.id.clear(); cd.date.clear(); cd.time.clear(); cd.notes.clear();
tryToRead = false;
}
}
return is;
}
int main()
{
// Open input file
std::ifstream ifs("r:\\input.txt");
if (ifs) {
// Read and parse all records with one statement
std::vector<ClientData> clientData{std::istream_iterator<ClientData>(ifs),std::istream_iterator<ClientData>() };
// Get total sum of notes
size_t totalSumOfNotes{ 0 };
for (const ClientData& cd : clientData) totalSumOfNotes += cd.getNumberOfNotes();
// Print result in requested format. You can of course replace std::cout by any ofstream
std::cout << "Total notes: " << totalSumOfNotes << "\n===============================";
for (size_t i = 0; i < clientData.size(); ++i) {
std::cout << '\n' << (i + 1) << ". " << clientData[i] << '\n';
}
std::cout << "===============================\n";
}
else
std::cerr << "Could not open imput file\n";
return 0;
}
This is my modified code: friend std::ostream& operator << (std::ostream& os, ClientData& cd) {os << " Client: " << cd.id << "\nDATE: " << cd.date << " Time: " << cd.time<< "Text2: "<< cd.exp_txt2;; for (auto const& [k, v] : cd.notes) os << '\n' << v << " x " << k << " USD"; return os; enum class States { WaitForDate, ReadDate, WaitForID, WaitForNotes1, WaitForNotes2, ReadNotes, WaitForText1, WaitForText2};
....
case States::ReadNotes:
if (line != "") {
std::string usd{};
std::istringstream iss(line);
unsigned int type{};
unsigned int count{};
iss >> type >> usd >> count;
cd.notes[type] += count;
}
else{
state = States::WaitForText1;
}
case States::WaitForText1:
if (line.find("Text1") == 0)
state = States::WaitForText2;
break;
case States::WaitForText2:
if (line!=" "){
std::istringstream iss(line);
iss >> cd.exp_txt2;
tryToRead = false;
state = States::WaitForDate;
}
break;.....
Input file:
Some other text
APPROVAL CODE:1264
Decline reason message: Common decline
=================================================
10:22:23 INFORMATION REQUEST AB C
10:22:24 INFORMATION REPLY NEXT FUNCTION 5
10:22:32 INFORMATION REQUEST AA
10:22:32 INFORMATION REPLY NEXT 0 FUNCTION 100
10:22:35 INFORMATION REQUEST AC
10:22:36 INFORMATION REPLY NEXT 016 FUNCTION
10:22:45 INFORMATION REQUEST B DA
10:22:45 INFORMATION REPLY NEXT 021
10:22:52 INFORMATION REQUEST A AA CB
10:22:53 INFORMATION REPLY NEXT 104 FUNCTION 2047
================ INFO ================
2018.07.18 10:23:41 - 4784
IDCR: 1111520
SOLUTION: A:5 B:5 C:5
=================================================
DATA/DATE TIME TERMINAL
2019.07.16 20:07:27 TID00302
----------------------------------------
ID: 123456******3381
AID: A0000000043060
**************************************
PLEASE KEEP THIS RECEIPT
AND CONTACT YOUR OFFICE
**************************************
NOTES:
50 USD 1
100 USD 2
PLEASE CONTACT YOUR OFFICE
SOME TEXT1
SOME TEXT2
----------------------------------------
10:23:09 INFORMATION REQUEST AB
10:23:09 INFORMATION REPLY NEXT 010 FUNCTION 5000
10:39:42 INFORMATION REQUEST AA
10:40:04 INFORMATION REQUEST A AA BB
10:40:04 INFORMATION REPLY NEXT 620 FUNCTION 5000
10:40:06 INFORMATION REQUEST ACCBB
=================================================
10:22:23 INFORMATION REQUEST AB C
10:22:24 INFORMATION REPLY NEXT FUNCTION 5
10:22:52 INFORMATION REQUEST A AA CB
10:22:53 INFORMATION REPLY NEXT 104 FUNCTION 2047
DATA/DATE TIME TERMINAL
2019.07.16 20:07:27 TID00302
----------------------------------------
ID: 123456******3381
AID: A0000000043060
**************************************
PLEASE KEEP THIS RECEIPT
AND CONTACT YOUR OFFICE
**************************************
NOTES:
10 USD 1
20 USD 3
50 USD 5
100 USD 10
PLEASE CONTACT YOUR OFFICE
SOME TEXT1
SOME TEXT2
----------------------------------------
APPROVAL CODE:
Decline reason message: Common decline
=================================================
10:22:23 INFORMATION REQUEST AB C
10:22:24 INFORMATION REPLY NEXT FUNCTION 5
10:22:32 INFORMATION REQUEST AA
10:22:32 INFORMATION REPLY NEXT 0 FUNCTION 100
Aucun commentaire:
Enregistrer un commentaire