Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6245397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:29:52+00:00 2026-05-24T12:29:52+00:00

I wrote a small snippet to search for matched strings in an array, then

  • 0

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?!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:29:56+00:00Added an answer on May 24, 2026 at 12:29 pm

    Using the following source to recreate your problem:

    $ cat test.cpp 
    #include <iostream>
    #include <string>
    
    int main() {
        using std::cout;
        using std::string;
    
        const std::string array1[] = {"MatchedString1", "MatchedString2", "MatchedString3"};
        const std::string arrayCopy[] = {"MatchedString1", "MatchedString2", "MatchedString3"}; 
        const std::string localString = "String";
        const std::string subArray1[] = {"Data", "Data", "Data"};
        const std::string subArray2[] = {"More data", "More data", "more data"};
        const unsigned int arrayCount = 3;
    
        for (int i = 0; i < arrayCount; i++) {
            if (arrayCopy[i].find(localString) != string::npos) {
                cout << "\n\t"
                    << array1[i] << " {"
                    << subArray1[i] << ", "
                    << subArray2[i] << "}";
            }
        }
    }
    

    Compile and run:

    $ g++ test.cpp 
    $ ./a.out
    
            MatchedString1 {Data, More data}
            MatchedString2 {Data, More data}
            MatchedString3 {Data, more data}
    

    Conclusion:

    The extra line-breaks are in your data.

    Suggestion:

    Use square brackets to delineate your input:

    std::cout << "[" << array1[i] << "] {[" << subArray1[i] << "], [" << subArray2[i] << "]}" <<  std::endl;
    

    Stripping the strings:

    If you find you need to strip your strings, you may find the following functions useful:

    std::string lstrip(const std::string& s, const char* chars = " \t\r\n")
    {
        std::string::size_type begin = s.find_first_not_of(chars);
        if (begin == std::string::npos)
        {
            return "";
        }
        return std::string(s, begin);
    }
    
    std::string rstrip(const std::string& s, const char* chars = " \t\r\n")
    {
        std::string::size_type end = s.find_last_not_of(chars);
        return std::string(s, 0, end + 1);
    }
    
    std::string strip(const std::string& s)
    {
        return lstrip(rstrip(s));
    }
    

    Use like this:

            std::cout << "\t"
                << strip(array1[i]) << " {"
                << strip(subArray1[i]) << ", "
                << strip(subArray2[i]) << "}"
                << std::endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a small snippet in jQuery to just put a search word into
I wrote this small snippet to calculate Fibonacci numbers. It works well for numbers
The http.get() function inside http.createServer is not responding. I wrote a small snippet to
I wrote a small PHP application several months ago that uses the WordPress XMLRPC
I wrote a small PHP application that I'd like to distribute. I'm looking for
We wrote a small Windows class library that implements extension methods for some standard
I wrote a small sample of code in C# to capture selected text from
I wrote a small internal web app using (a subset of) pylons . As
I wrote some small apps using .NET 3.5 but now I am stuck with
I wrote a small app that turns out to be using a lot of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.