QString line.split - c++

I am having trouble with split function on QT. I have a text file and the very first line is like this:
10,100;100,100;100,100;100,200;100,200;300,200;300,200;300,350;300,350;250,350;250,350;250,300;250,300;200,300;200,300;200,400;200,400;10,400;10,400;10,100
Here every two numbers represents a point with x an y values and semicolons divides the points. After every semicolon there is a new point.
What I want to do is split these numbers element by element like;
Element 0: 10
Element 1: 100
Element 2: 100
Element 3: 100
...
I have managed to do this but there wasn't any semicolons or colons in the text file.
First line of the text file:
10 100 100 100 100 200 300 200 300 350 250 350 250 300 200 300 200 400 10 400 10 100
This is how i did it:
void Dialog::readFile()
{
QFile file("/Path/of/the/text/file/Data.txt");
if(!file.open(QIODevice::ReadOnly)) {
QMessageBox::information(0, "info", file.errorString());
}
QString line = file.readLine();
qDebug() << "line:" << line << "\n";
QStringList list = line.split(" ");
double element;
for(int i = 0; i < list.size(); i++) {
element = list.at(i).toDouble();
points_.append(element);
qDebug() << "element:" << element << "\n";
}
}
Output of this code is:
line: "10 100 100 100 100 200 300 200 300 350 250 350 250 300 200 300 200 400 10 400 10 100\r"
element: 10
element: 100
element: 100
element: 100
element: 100
element: 200
element: 300
element: 200
....
I want to do this exactly the same way. Is there any suggestion?
PS: I am new on QT so please consider this.

You can use regular expression with the QString::split overload taking QRegExp or the next one taking QRegularExpression:
QStringList list = line.split(QRegExp(",|;")); // ",|;" matches ',' or ';'
QRegExp is called to be deprecated, but it can do the job here.

Related

Array Lookup test cases failing

So, this is one of two practice questions given by our professor on HackerRank. I came up with the code but the solution only passes 2/5 test cases. I tried thinking about the possible test cases that give an error for around 4 hours, but, I could not wrap my mind around it.
Can someone point out where I went wrong?
Below is the question.
Alice went for shopping and bought 8 goods costing different prices.
At the time of billing she realises that she did not have enough
money. Now, she have decided to remove the item costing maximum amount
and replace it with another item whose price is same as the item
costing minimum amount. Help Alice and display final prices on the
screen.
Note: if more than one element cost maximum price, replace the first
item.
Example 1:
Input: 250 1000 50 20 10 100 200 25
Output: 250 10 50 20 10 100 200 25
Example 2:
Input: 2500 2500 50 20 10 100 200 25
Output: 10 2500 50 20 10 100 200 25
Sample Input: 250 1000 50 20 10 100 200 25
Sample Output: 10 2500 50 20 10 100 200 25
Constraints
8 items prices are required
Output Format
Prints the final prices of different items after replacing
MY CODE:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int arr[8];
int check=0;
int max=0,min=0;
for(int i=0;i<8;i++)
{
cin>>arr[i];
if(i==0)
{
max =arr[0];
min =arr[0];
}
else
if(arr[i]>max)
max = arr[i];
if(arr[i]<min)
min = arr[i];
}
for(int i=0;i<8;i++)
{
if(arr[i]==max && check==0)
{
arr[i]=min;
check++;
}
cout<<arr[i]<<" ";
}
}

perft-function of chess engine is giving self-contradictory output

I am currently developing a chess engine in C++, and I am in the process of debugging my move generator. For this purpose, I wrote a simple perft() function:
int32_t Engine::perft(GameState game_state, int32_t depth)
{
int32_t last_move_nodes = 0;
int32_t all_nodes = 0;
Timer timer;
timer.start();
int32_t output_depth = depth;
if (depth == 0)
{
return 1;
}
std::vector<Move> legal_moves = generator.generate_legal_moves(game_state);
for (Move move : legal_moves)
{
game_state.make_move(move);
last_move_nodes = perft_no_print(game_state, depth - 1);
all_nodes += last_move_nodes;
std::cout << index_to_square_name(move.get_from_index()) << index_to_square_name(move.get_to_index()) << ": " << last_move_nodes << "\n";
game_state.unmake_move(move);
}
std::cout << "\nDepth: " << output_depth << "\nTotal nodes: " << all_nodes << "\nTotal time: " << timer.get_milliseconds() << "ms/" << timer.get_milliseconds()/1000.0f << "s\n\n";
return all_nodes;
}
int32_t Engine::perft_no_print(GameState game_state, int32_t depth)
{
int32_t nodes = 0;
if (depth == 0)
{
return 1;
}
std::vector<Move> legal_moves = generator.generate_legal_moves(game_state);
for (Move move : legal_moves)
{
game_state.make_move(move);
nodes += perft_no_print(game_state, depth - 1);
game_state.unmake_move(move);
}
return nodes;
}
It's results for the initial chess position (FEN: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1) for depths 1 and 2 match the results of stockfish's perft command, so I assume they are correct:
h2h3: 1
h2h4: 1
g2g3: 1
g2g4: 1
f2f3: 1
f2f4: 1
e2e3: 1
e2e4: 1
d2d3: 1
d2d4: 1
c2c3: 1
c2c4: 1
b2b3: 1
b2b4: 1
a2a3: 1
a2a4: 1
g1h3: 1
g1f3: 1
b1c3: 1
b1a3: 1
Depth: 1
Total nodes: 20
Total time: 1ms/0.001s
h2h3: 20
h2h4: 20
g2g3: 20
g2g4: 20
f2f3: 20
f2f4: 20
e2e3: 20
e2e4: 20
d2d3: 20
d2d4: 20
c2c3: 20
c2c4: 20
b2b3: 20
b2b4: 20
a2a3: 20
a2a4: 20
g1h3: 20
g1f3: 20
b1c3: 20
b1a3: 20
Depth: 2
Total nodes: 400
Total time: 1ms/0.001s
The results stop matching at depth 3, though:
Stockfish:
go perft 3
a2a3: 380
b2b3: 420
c2c3: 420
d2d3: 539
e2e3: 599
f2f3: 380
g2g3: 420
h2h3: 380
a2a4: 420
b2b4: 421
c2c4: 441
d2d4: 560
e2e4: 600
f2f4: 401
g2g4: 421
h2h4: 420
b1a3: 400
b1c3: 440
g1f3: 440
g1h3: 400
Nodes searched: 8902
My engine:
h2h3: 361
h2h4: 380
g2g3: 340
g2g4: 397
f2f3: 360
f2f4: 436
e2e3: 380
e2e4: 437
d2d3: 380
d2d4: 437
c2c3: 399
c2c4: 326
b2b3: 300
b2b4: 320
a2a3: 280
a2a4: 299
g1h3: 281
g1f3: 280
b1c3: 357
b1a3: 320
Depth: 3
Total nodes: 7070
Total time: 10ms/0.01s
I figured that my move generator was just buggy, and tried to track down the bugs by making a move the engine gives incorrect values for on the board and then calling perft() with depth = 2 on it to find out which moves are missing. But for all moves I tried this with, the engine suddenly starts to output the correct results I expected to get earlier!
Here is an example for the move a2a3:
When calling perft() on the initial position in stockfish, it calculates 380 subnodes for a2a3 at depth 3.
When calling perft() on the initial position in my engine, it calculates 280 subnodes for a2a3 at depth 3.
When calling perft() on the position you get after making the move a2a3 in the initial position in my engine, it calculates the correct number of total nodes at depth 2, 380:
h7h5: 19
h7h6: 19
g7g5: 19
g7g6: 19
f7f5: 19
f7f6: 19
e7e5: 19
e7e6: 19
d7d5: 19
d7d6: 19
c7c5: 19
c7c6: 19
b7b5: 19
b7b6: 19
a7a5: 19
a7a6: 19
g8h6: 19
g8f6: 19
b8c6: 19
b8a6: 19
Depth: 2
Total nodes: 380
Total time: 1ms/0.001s
If you have any idea what the problem could be here, please help me out. Thank you!
EDIT:
I discovered some interesting new facts that might help to solve the problem, but I don't know what to do with them:
For some reason, using std::sort() like this in perft():
std::sort(legal_moves.begin(), legal_moves.end(), [](auto first, auto second){ return first.get_from_index() % 8 > second.get_from_index() % 8; });
to sort the vector of legal moves causes the found number of total nodes for the initial position (for depth 3) to change from the wrong 7070 to the (also wrong) 7331.
When printing the game state after calling game_state.make_move() in perft(), it seems to have had no effect on the position bitboards (the other properties change like they are supposed to). This is very strange, because isolated, the make_move() method works just fine.
I'm unsure if you were able to pin down the issue but from the limited information available in the question, the best I can assume (and something I faced myself earlier) is that there is a problem in your unmake_move() function when it comes to captures since
Your perft fails only at level 3 - this is when the first legal capture is possible, move 1 and 2 can have no legal captures.
Your perft works fine when it's at depth 1 in the position after a2a3 rather than when it's searching at depth 3 from the start
This probably means that your unmake_move() fails at a depth greater than 1 where you need to restore some of the board's state that cannot be derived from just the move parameter you are passing in (e.g. enpassant, castling rights etc. before you made the move).
This is how you would like to debug your move generator using perft.
Given startpos as p1, generate perft(3) for your engine and sf. (you did that)
Now check any move that have different nodes, you pick a2a3. (you did that)
Given startpos + a2a3 as p2, generate perft(2) for your engine and sf. (you partially did this)
Now check any move that have different nodes in step 3. Let's say move x.
Given startpos + a2a3 + x as p3, generate perft(1) for your engine and sf.
Since that is only perft(1) by this time you will be able to figure out the wrong move or the missing move from your generator. Setup that last position or p3 on the board and see the wrong/missing moves from your engine compared to sf perft(1) result.

Does fstream access the hardrive with read and writes during compile time if used within a class temlpate?

Consider the following code snippet of this class template...
template<class T>
class FileTemplate {
private:
std::vector<T> vals_;
std::string filenameAndPath_;
public:
inline FileTemplate( const std::string& filenameAndPath, const T& multiplier ) :
filenameAndPath_( filenameAndPath ) {
std::fstream file;
if ( !filenameAndPath_.empty() ) {
file.open( filenameAndPath_ );
T val = 0;
while ( file >> val ) {
vals_.push_back( val );
}
file.close();
for ( unsigned i = 0; i < vals_.size(); i++ ) {
vals_[i] *= multiplier;
}
file.open( filenameAndPath_ );
for ( unsigned i = 0; i < vals_.size(); i++ ) {
file << vals_[i] << " ";
}
file.close();
}
}
inline std::vector<T> getValues() const {
return vals_;
}
};
When used in main as such with the lower section commented out with the following pre-populated text file:
values.txt
1 2 3 4 5 6 7 8 9
int main() {
std::string filenameAndPath( "_build/values.txt" );
std::fstream file;
FileTemplate<unsigned> ft( filenameAndPath, 5 );
std::vector<unsigned> results = ft.getValues();
for ( auto r : results ) {
std::cout << r << " ";
}
std::cout << std::endl;
/*
FileTemplate<float> ft2( filenameAndPath, 2.5f );
std::vector<float> results2 = ft2.getValues();
for ( auto r : results2 ) {
std::cout << r << " ";
}
std::cout << std::endl;
*/
std::cout << "\nPress any key and enter to quit." << std::endl;
char q;
std::cin >> q;
return 0;
}
and I run this code through the debugger sure enough both the output to the screen and file are changed to
values.txt - overwritten are -
5 10 15 20 25 30 35 40 45
then lets say I don't change any code just stop the debugging or running of the application, and let's say I run this again 2 more times, the outputs respectively are:
values.txt - iterations 2 & 3
25 50 75 100 125 150 175 200 225 250
125 250 375 500 625 750 875 1000 1125 1250
Okay good so far; now lets reset our values in the text file back to default and lets uncomment the 2nd instantiation of this class template for the float with a multiplier value of 2.5f and then run this 3 times.
values.txt - reset to default
1 2 3 4 5 6 7 8 9
-iterations 1,2 & 3 with both unsigned & float the multipliers are <5,2.5> respectively. 5 for the unsigned and 2.5 for the float
- Iteration 1
cout:
5 10 15 20 25 30 35 40 45
12.5 25 37.5 50 62.5 75 87.5 100 112.5
values.txt:
12.5 25 37.5 50 62.5 75 87.5 100 112.5
- Iteration 2
cout:
60
150 12.5 62.5 93.75 125 156.25 187.5 218.75 250 281.25
values.txt:
150 12.5 62.5 93.75 125 156.25 187.5 218.75 250 281.25
- Iteration 3
cout:
750 60
1875 150 12.5 156.25 234.375 312.5 390.625 468.75 546.875 625 703.125
values.txt:
1875 150 12.5 156.25 234.375 312.5 390.625 468.75 546.875 625 703.125
A couple of questions come to mind: it is two fold regarding the same behavior of this program.
The first and primary question is: Are the file read and write calls being done at compile time considering this is a class template and the constructor is inline?
After running the debugger a couple of times; why is the output incrementing the number of values in the file? I started off with 9, but after an iteration or so there are 10, then 11.
This part just for fun if you want to answer:
The third and final question yes is opinion based but merely for educational purposes for I would like to see what the community thinks about this: What are the pros & cons to this type of programming? What are the potentials and the limits? Are their any practical real world applications & production benefits to this?
In terms of the other issues. The main issue is that you are not truncating the file when you do the second file.open statement, you need :
file.open( filenameAndPath_, std::fstream::trunc|std::fstream::out );
What is happening, is that, when you are reading unsigned int from a file containing floating points, it is only reading the first number (e.g. 12.5) up to the decimal place and then stopping (e.g. reading only 12)
, because there is no other text on the line that looks like an unsigned int. This means it only reads the number 12 and then multiplies it by 5 to get the 60, and writes it to the file.
Unfortunately because you don't truncate the file when writing the 60, it leaves the original text at the end which is interpreted as additional numbers in the next read loop. Hence, 12.5 appears in the file as 60 5
stream buffers
Extracts as many characters as possible from the stream and inserts them into the output sequence controlled by the stream buffer object pointed by sb (if any), until either the input sequence is exhausted or the function fails to insert into the object pointed by sb.
(http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/)

how to fetch data from a txt file using fstream

I am building a small application. in this i have saved some data in a txt file. i need to edit a value in particular row and column. i wrote a code to go to a particular line and fetch the values but i have tried almost everything to got a particular column and edit that value.
1000 400 120 110 800 110 150 500 0 1000
1000 400 90 150 800 120 150 600 0 1000
1000 400 80 60 **800** 132 150 700 0 1000
1000 400 120 60 800 123 150 200 0 1000
1000 400 111 80 800 143 150 700 0 1000
1000 400 30 90 800 155 150 500 0 1000
for example i have edit the highlighted value , which is the best way to do. i cannot paste my whole code as it is very long.
this is the one where i can go to a particular line
fstream& GotoLine(int num) {
infile.seekg(std::ios::beg);
for (int i = 0; i < num - 1; ++i) {
infile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
return infile;
}
i would appreciate any help over this.
Assuming that your file is formatted as fixed column size, my advice would be:
prepare an array or a vector of struct { size_t pos; size_t width} to define the fields
open the file in ios::in | ios::out mode
read the file one line at a time with your GotoLine function up to the line you want to process
note the index in the fstream with tellg
read the interesting line in a char array with a size greater than the line - here I would use 64 to be large enough - with istream::getline
as you have read the line as a plain array, you can rewrite the characters of one single field
go back to beginning of line with seekp
write the line back to the file
This is not a general method of editing a text file. It only works here because as you have fields of fixed size the edited line has exactly same size than original one, so it can be re-written in place. But never use it in a general case.

I can't figure out what's wrong with my c++ program

The question is: The ABC Hardware Company has hired you to write a program for it's Account Receivable dept. The master file is in ascending order by customer number with a 20 character customer name and balance due. The transaction file contains records of each transaction with customer number. You are to read in records one at a time from the two files and use the transaction file to update the info from the master file. Process all transaction records before going on to the next master record. If the transaction record contains an "O" in column 1, calculate the orderamount and add it to the balance due. If the record contains a "P" in column 1, subtract the payment from the balance due. Keep a running total of the AR balance of the ABC Company (the sum of balances for each customer). After processing a master record and all its transactions, the program should prepare an invoice for each customer which lists the customer name, number, previous balance, all transactions, and the final balance due.
The output should look like:
CUSTOMER NAME CUSTOMER NUMBER
PREVIOUS BALANCE $XXX.XX
(ALL TRANSACTIONS PER CUSTOMER:)
TRANSACTION# ITEM ORDERED $ORDER AMOOUNT
TRANSACTION# ITEM ORDERED $ORDER AMOUNT
TRANSACTION# PAYMENT $PAYMENT AMOUNT
BALANCE DUE $XXX.XX
I've tried changing up the arrays, the if statements etc. Now nothing's printing at all when I run the program. Please help!
Here is the code I have so far:
# include <iostream>
# include <fstream>
# include <iomanip>
# include <string>
using namespace std;
struct master {
double custnum;
string name;
double balance;
};
struct transactions {
char transtype;
int custnum;
int transnum;
string item;
int quantity;
double price;
double amountpaid;
};
int main ()
{
ifstream masterfile ("MASTER.txt");
ifstream transfile ("TRANSACTION.txt");
int prevbalance[7];
master main [7];
for (int i=0; !masterfile.eof(); i++) {
masterfile>>main[i].custnum>>main[i].name>>main[i].balance;
}
for (int i=0;!masterfile.eof();i++) {
cout << main[i].custnum<<" ";
cout << main[i].name<<" ";
cout << main[i].balance<<" "<<
endl<<endl;
prevbalance[i] = main[i].balance;
}
double companybalance = 0;
double orderamt=0;
transactions tran[35];
for (int i=0; !transfile.eof(); i++) {{
transfile>> tran[i].transtype;
cout<<tran[i].transtype<<" ";
if (tran[i].transtype == 'O') {
transfile>>tran[i].custnum;
cout<<tran[i].custnum<<" ";
transfile>> tran[i].transnum;
cout<<tran[i].transnum<<" ";
transfile>>tran[i].item;
cout<<tran[i].item<<" ";
transfile>>tran[i].quantity;
cout<<tran[i].quantity<<" ";
transfile>>tran[i].price;
cout<<tran[i].price<<" "<<endl<<endl;
orderamt= tran[i].price*tran[i].quantity;
main[i].balance+= orderamt;
companybalance += main[i].balance;
}
else if (tran[i].transtype == 'P'){
transfile>>tran[i].custnum;
cout<<tran[i].custnum<<" ";
transfile>> tran[i].transnum;
cout<<tran[i].transnum<<" ";
transfile>>tran[i].amountpaid;
cout<<tran[i].amountpaid<<endl<<endl<<endl;
main[i].balance-tran[i].amountpaid;
companybalance += main[i].balance;
}}
for(int i=0; i<50; i++) {
cout<<"Name: "<< main[i].name <<" Customer #: "<< main[i].custnum<<endl<<endl;
cout<<"Previous Balance "<<prevbalance[i]<<endl;
for(int j=0; j<7; j++){
cout<<"Transaction #: "<<tran[j].transnum<<" "<<tran[j].item<<" $"<<orderamt<<endl; }
cout<<"Balance Due: "<<main[i].balance<<endl;
}
}}
Here are the input two files, Masterfile:
1000 TIFFANY 7000.99
2000 MARY 6500.98
3000 JACOB 6560.99
4000 GENE 4560.98
5000 BELLA 5300.87
6000 ANNA 2340.90
7000 DEMI 4230.45
and transaction file:
O 1000 1000 PENS 20 2
O 1000 2000 CPUS 2 200
O 1000 3000 MONITER 2 100
P 1000 4000 4000
P 1000 5000 300
O 2000 6000 CPUS 3 500
O 2000 7000 MOUSE 3 50
O 2000 8000 WIRES 5 8
P 2000 9000 600
P 2000 1100 798
O 3000 1200 MONITERS 6 60
O 3000 1300 CPUS 7 300
O 3000 1400 MOUSE 30 40
O 3000 1500 SPEAKERS 20 20
P 3000 1600 5000
O 4000 1001 SPEAKERS 2 50
O 4000 2002 CABLES 4 20
P 4000 3003 400
P 4000 4004 500
P 4000 5005 68
P 5000 6001 600
P 5000 4002 55
P 5000 2003 450
O 5000 4004 SPEAKERS 4 60
O 5000 1005 LAPTOP 3 300
O 6000 6001 TVS 5 400
O 6000 8002 SPEAKERS 5 70
P 6000 6003 2000
P 6000 8004 1000
O 6000 8005 CABLES 10 15
O 7000 5001 PENS 50 2
O 7000 7002 PAPER 400 2
P 7000 4003 150
P 7000 3004 230
P 7000 6005 450
You're using masterfile.eof() as a loop condition twice.
for (int i=0; !masterfile.eof(); i++) {
masterfile>>main[i].custnum>>main[i].name>>main[i].balance;
}
for (int i=0;!masterfile.eof();i++) {
cout << main[i].custnum<<" ";
cout << main[i].name<<" ";
cout << main[i].balance<<" "<<
endl<<endl;
prevbalance[i] = main[i].balance;
}
Think about how this couldn't possibly work. You've already reached the end of the file by the time the first loop ends. The second loop is going to finish before it even starts!
Of course you could reset the file pointer to the beginning but a much better way is to keep a count of the number of records you have, and use that count for the second loop.
int iRecordCount = 0;
for (iRecordCount = 0; !masterfile.eof(); iRecordCount++) {
masterfile>>main[iRecordCount].custnum>>main[iRecordCount].name>>main[iRecordCount].balance;
}
for (int i=0; i < iRecordCount;i++) {
cout << main[i].custnum<<" ";
cout << main[i].name<<" ";
cout << main[i].balance<<" "<<
endl<<endl;
prevbalance[i] = main[i].balance;
}
Also note that you're using a fixed array (master main[7]) to store your records - what happens if you have an input file with more than 7 records in it? You'll exceed the bounds of the array. Really you should change to using a std::vector so that the array can grow dynamically, but if you don't want to do that you should put an additional check in when reading the records to make sure you don't overflow the array:
int iRecordCount = 0;
for (iRecordCount = 0; iRecordCount < sizeof(main) / sizeof(main[0]) && !masterfile.eof();
iRecordCount++) {
masterfile>>main[iRecordCount].custnum>>main[iRecordCount].name>>main[iRecordCount].balance;
}
You're also doing something weird when reading your transactions. You have things like:
orderamt= tran[i].price*tran[i].quantity;
main[i].balance+= orderamt;
If i is the transaction number (tran[i]) how can it also be the customer number as well (main[i])? You're allowed to use variable names other than i you know!
You also have this at the bottom of your code:
for(int i=0; i<50; i++) {
....
}
Again, you should use the actual number of elements in the array (iRecordCount) rather than a fixed number (and where did 50 come from since your array is only 7 elements in size?).
In addition to the suggestions made by #JonathanPotter in his answer, I want to add that using ifstream.eof() in the for loop is error prone.
Useful reading material: Why is “while ( !feof (file) )” always wrong?.
Use of !masterfile.eof() in the conditional of a for loop is wrong for exactly the same reasons.
It's better to use:
std::vector<master> masterData;
while ( masterfile )
{
// Try to read the data into a temporary object.
master m;
masterfile >> m.custnum >> m.name >> m.balance;
// If the input was successful, add it to masterData
if ( masterfile )
{
masterData.push_back(m);
}
}
You can use similar logic to read the transaction records too.
Also, you have a line:
main[i].balance - tran[i].amountpaid;
I am sure that is a typo and you meant to use:
main[i].balance -= tran[i].amountpaid;