I wrote a small snippet to search for matched strings in an array, then output results for parallel arrays in a nicely formatted fashion. However, I must have some fundamental misunderstanding about how string outputs work, because for the life of me I cannot get this to output correctly, no matter where I put the tab, the newline, or whether I use an endl in my code.
Here is the relevant code below:
for (int i = 0; i < arrayCount; i++) {
if (arrayCopy[i].find(localString) != string::npos) {
cout << "\n\t"
<< array1[i] << " {"
<< subArray1[i] << ", "
<< subArray2[i] << "}";
}
}
I’m expecting results to have a tab at start of each line:
MatchedString1 {Data, MoreData}
MatchedString2 {Data, MoreData}
MatchedString3 {Data, MoreData}
Instead I am getting results like below, where the tabs appear on blank lines (except for the first result):
MatchedString1 {Data, MoreData}
MatchedString2 {Data, MoreData}
MatchedString3 {Data, MoreData}
What devilish quirk exists in c++ that is causing me so much pain?!
Using the following source to recreate your problem:
Compile and run:
Conclusion:
The extra line-breaks are in your data.
Suggestion:
Use square brackets to delineate your input:
Stripping the strings:
If you find you need to strip your strings, you may find the following functions useful:
Use like this: