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 8516863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:32:48+00:00 2026-06-11T05:32:48+00:00

Possible Duplicate: Splitting a string in C++ Im trying to create a function that

  • 0

Possible Duplicate:
Splitting a string in C++

Im trying to create a function that mimics the behavior of the getline() function, with the option to use a delimiter to split the string into tokens.
The function accepts 2 strings (the second is being passed by reference) and a char type for the delimiter. It loops through each character of the first string, copying it to the second string and stops looping when it reaches the delimiter. It returns true if the first string have more characters after the delimiter and false otherwise. The position of the last character is being saved in a static variable.
for some reason the the program is going into an infinite loop and is not executing anything:

const int LINE_SIZE = 160;
bool strSplit(string sFirst, string & sLast, char cDelim) {
    static int iCount = 0;
    for(int i = iCount; i < LINE_SIZE; i++) {
        if(sFirst[i] != cDelim)
            sLast[i-iCount] = sFirst[i];
        else {
            iCount = i+1;
            return true;
        }
    }
    return false;
}

The function is used in the following way:

while(strSplit(sLine, sToken, '|')) {
    cout << sToken << endl;
}

Why is it going into an infinite loop, and why is it not working?
I should add that i’m interested in a solution without using istringstream, if that’s possible.

  • 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-06-11T05:32:50+00:00Added an answer on June 11, 2026 at 5:32 am

    It is not exactly what you asked for, but have you considered std::istringstream and std::getline?

    // UNTESTED
    std::istringstream iss(sLine);
    while(std::getline(iss, sToken, '|')) {
      std::cout << sToken << "\n";
    }
    

    EDIT:

    Why is it going into an infinite loop, and why is it not working?

    We can’t know, you didn’t provide enough information. Try to create an SSCCE and post that.

    I can tell you that the following line is very suspicious:

           sLast[i-iCount] = sFirst[i];
    

    This line will result in undefined behavior (including, perhaps, what you have seen) in any of the following conditions:

    • i >= sFirst.size()
    • i-iCount >= sLast.size()
    • i-iCount < 0

    It appears to me likely that all of those conditions are true. If the passed-in string is, for example, shorter than 160 lines, or if iCount ever grows to be bigger than the offset of the first delimiter, then you’ll get undefined behavior.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Splitting a string in C++ I have this vector function from one
Possible Duplicate: splitting a string i have a string which looks like this: http://pastebin.com/m5508ff19
Possible Duplicates: How to split a string in C++? Splitting a C++ std::string using
Possible Duplicate: Empty check with string split I split the below string with '&'
Possible Duplicate: Splitting a string into chunks of a certain size Is there anyway
Possible Duplicate: Split string in SQL I have seen a couple of questions related
Possible Duplicate: Python: Split string with multiple delimiters I have a program where I
Possible Duplicate: Splitting a string into a list in python I'd like to take
Possible Duplicate: Splitting a hunk with magit When you review your changes sections you
Possible Duplicate: Can main function call itself in C++? I found this problem very

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.