c++ - How can I compare line from text file to multiple lines following? -
i have text file 1s, 2s , 3s below:
1 1 2 3 3 3 1 2 2 2 2 1 ..and trying find way find out how many in row each.
for example if checking 1 output: 1 in row: 2, 2 in row: 1, 3 in row: 0, 4 in row: 0.... way 20 in row (array size), since there 2 1s in row once , 2 1s (only 1 in row)
i trying calculate how many times number 1 1 in row, 2 in row, 3 in row, etc 20 (if had longer list)
so far have, don't know @ ??? line:
int main() { ifstream file("test.txt"); string linebuffer; int sequencecounts[20]; int onez = 0; while (file && getline(file, linebuffer)){ if (linebuffer.length() == 0)continue; { if (linebuffer == "1") { ??? while next 1->onez++ sequencecounts[onez]++; } } } return 0; }
try along lines of this:
int sequencecounts[20]; int currentones = 0; while (file && getline(file, linebuffer)){ if (linebuffer.length() == 0){ if (currentones > 0){ sequencecounts[currentones]++; } continue; } if (linebuffer == "1") { currentones++; //we found 1, //meaning current group bigger in last line. } else if (currentones > 0){ //this line not contain "1", previous lines did sequencecounts[currentones]++; currentones = 0; } } basically each time encounter "1" increase counter how long current sequence is. when sequence finished (a line without "1" "1"s before) increase counter particular number of "1"s , reset counter current sequence.
edit: previous failed if file ended "1"
Comments
Post a Comment