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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:11:26+00:00 2026-05-23T00:11:26+00:00

My code counts spaces in string temp , which works fine. Later in the

  • 0

My code counts spaces in string temp, which works fine. Later in the code I have a similar loop that does not end as expected.

Based on my debugging and some research, I’ve determined that the while loop isn’t stopping when it is supposed to the second time around, so an exception is thrown (std::out_of_range)… Why? Both while loops’ conditions are identical, so they should behave the same, right? That is, they should both stop looping when find() didn’t find a space…

First part of the code:

// Just find out how many spaces are in the temp string for now
// Find index (position in the string) of the first space in temp string
_index = temp.find( " " );

// Loop as long as there are still spaces in temp string
while (_index != string::npos) {
    // Find the index of the next space
    _index = temp.find(" ", _index + 1);

    // Keep track of how many spaces are in temp string
    count++;
}

Later (don’t mind my debugging code interspersed in there):

// If the amount of spaces is just right, i.e. there are exactly SIZE - 1 spaces
if (count == SIZE - 1) {
    cout << "\n\n  - Inside the if (count == SIZE - 1) block\n"
         << "  - temp = \"" << temp << "\", temp's size = " << temp.length()
         << "\n\n";

    // This time, we're removing the spaces from the temp string in order to
    // be able to search it for any non-number characters
    // Find index of the first space in the temp string
    _index = temp.find(" ");

    cout << "  - The first temp.find(\" \") yields: " << _index << "\n\n";

    // Loop as long as there are still spaces in temp string
    while (_index != string::npos) {
        cout << "  - Right before temp.replace() in the loop\n"
             << "  - _index = " << _index << "\n\n";

        // Remove the space in temp string
        temp.replace(_index, 1, "");

        // find the index of the next space
        _index = input.find(" ", _index + 1);
    }
    // …
}
  • 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-23T00:11:27+00:00Added an answer on May 23, 2026 at 12:11 am

    Within the second loop you reference a variable input instead of temp.

    _index = input.find( " ", _index + 1 );`
    

    Therefore the loop does never terminate. This you don’t do within the first while-loop.

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

Sidebar

Related Questions

Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
I have a template string and an array of parameters that come from different
I would like to have a counter, that counts backwards, and is stopped if
I have the following code: SELECT <column>, count(*) FROM <table> GROUP BY <column> HAVING
I have a gridview I bound a DataTable with that Gridview Its dynamic so
I have a project that adds elements to an AutoCad drawing. I noticed that
I am having an issue handling HTTP URL Connections that do not return data.
I am writing a program that counts the number of words in a text
I am having difficulty finding space and time complexity for this code that i
The code below takes a keyword and a string of text (sanitized of html

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.