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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:53:51+00:00 2026-05-26T15:53:51+00:00

This is a snippet of code from C++ program. string TwoSeries::getArrays() { ostringstream outIndex;

  • 0

This is a snippet of code from C++ program.

string TwoSeries::getArrays()
{
    ostringstream outIndex;
    ostringstream outValueA;
    ostringstream outValueB;
    string stA;
    string stB;
    string valueA;
    string index;
    int *arA;
    int * arB;
    string valueB;
    for(int x = 0; x < 200; x++)  
    {         

        outIndex << x;
        index = outIndex.str();



         arA = getArrayA();
        outValueA << *(arA + x);
        valueA = outValueA.str();


          arB = getArrayB();
        outValueB << *(arB + x);
        valueB = outValueB.str();


        stA += index + ":" + valueA + " ";
        stB += index + ":" + valueB + " ";

    }

   // return "Series A: \n"+stA+ "\n"+"Series B: \n"+ stB;   
    return index;
}

This function should return the last index converted from int into string, and that should be 199. But instead this object ‘outIndex’ concatenates all the digits (strings) into one string and gives as the result something like this: 1234567891011121314151617 … 198199. And definitely the last number is 199. And what to force the function after full loop to output only the last number instead all the numbers it has come across. How to do this?

  • 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-26T15:53:51+00:00Added an answer on May 26, 2026 at 3:53 pm

    You want to clear the string streams:

    for(int x = 0; x < 200; x++)  
    {         
        outIndex.str("");
        outValueA.str("");
        outValueB.str("");
    

    Alternatively, you can adopt good C++ style and declare them locally in the loop:

    for(int x = 0; x < 200; x++)  
    {         
        ostringstream outIndex;
        ostringstream outValueA;
        ostringstream outValueB;
    

    While you’re at it you can move the rest as well. Or… rewrite as follows:

    string TwoSeries::getArrays()
    {
        string index;
    
        int x;
        for(x = 0; x < 200; x++)  
        {         
            ostringstream osA, osB;
    
            osA << x << ":" << *(getArrayA() + x) + " ";
            osB << x << ":" << *(getArrayB() + x) + " ";
    
            string stA = osA.str(); // warning: value isn't used
            string stB = osB.str(); // warning: value isn't used
        }
    
        ostringstream osA, osB;
        outIndex << (x-1); // previous index
        return outIndex.str();
    }
    

    Note that you’re doing a lot of redundant work, and right now all those values aren’t being used. Perhaps you have more code that you haven’t shown 🙂

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

Sidebar

Related Questions

This code snippet is from C# in Depth static bool AreReferencesEqual<T>(T first, T second)
What's wrong with this snippet of code? import numpy as np from scipy import
This is a code snippet from O'Reilly Learning Opencv, cvNamedWindow(Example3, CV_WINDOW_AUTOSIZE); g_capture = cvCreateFileCapture(argv[1]);
From this site: http://www.toymaker.info/Games/html/vertex_shaders.html We have the following code snippet: // transformations provided by
this is my code snippet, where the program doesn't enter the foreach loop: var
in the book i'm learning from i came across this code snippit: while (i
This snippet of code always parses the date into the current timezone, and not
Consider this snippet of code: public static class MatchCollectionExtensions { public static IEnumerable<T> AsEnumerable<T>(this
I came across this snippet of code on MSDN: entityBuilder.Metadata = @res://*/AdventureWorksModel.csdl| res://*/AdventureWorksModel.ssdl| res://*/AdventureWorksModel.msl;
I wrote this snippet of code and I assume len is tail-recursive, but a

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.