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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:42:07+00:00 2026-05-28T00:42:07+00:00

for each (std::string s in m_intro.text) // std::vector<string> { for (int i = 0;

  • 0
for each (std::string s in m_intro.text) // std::vector<string>
    {
        for (int i = 0; i < s.length(); i++)
        {
            char* chr = &s.at(i);
            chr[i+1] = NULL;
            std::string t(chr);

            // other code is used below not shown as not relivent

        }

    }

I want to get a char from a string. Each char I get I then want to turn into a string (there is a function that needs a const std::string&)

The above code works once, but after the first loop the entire of s is null. I can see why this is happening.

What I want is to get the next char from s on each loop and store it as a string.

  • 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-28T00:42:08+00:00Added an answer on May 28, 2026 at 12:42 am
    char* chr = &s.at(i);
    chr[i+1] = NULL;
    std::string t(chr);
    

    When the char was part of a C-string (or char array), you used the right (if antiquated) approach of setting the next element to NULL to terminate the string.

    However in this case, this is not relevant; you’re just indexing into a std::string and replacing all its characters with NULL, which is of course not what you meant.

    std::string has a constructor you can use to avoid this nastiness:

    std::string t(1, s.at(i));
    //   ^        ^    ^
    //   |        |    |
    // string     |    |
    //            |    |
    //     of length 1 |
    //        char     |
    //                 |
    //     each having value s.at(i)
    

    No need to mess with pointers or char arrays or NULL-termination.

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

Sidebar

Related Questions

In code below: map<string,vector<int>> create(ifstream& in, const vector<string>& vec) { /*holds string and line
I'm currently stuck finding the correct syntax for trimming each string in a std::vector.
When using the std::for_each, class A; vector<A*> VectorOfAPointers; std::for_each(VectorOfAPointers.begin(), VectorOfAPointers.end(), std::mem_fun(&A::foo)); If we have
I have a C++ std::vector denoted as: std::vector<GameObject*> vectorToSort; Each object in vectorToSort contains
Suppose I have two containers of elements: std::vector<std::string> foo = { aa, bb, cc
I'm having trouble initializing my dynamic_bitset . I have this code: for(int j=(data1.length()-1); j>=0;
I'm trying to use stl algorithm for_each without proliferating templates throughout my code. std::for_each
I'm trying to use templates to get std:list of items, where each item has
I am attempting to write a interpreter that will turn a string like: vector(random(0,
I'm getting the text from editbox and I'd want to get each name separated

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.