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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:38:40+00:00 2026-06-11T06:38:40+00:00

I am attempting to return some information when my toString() method is called, which

  • 0

I am attempting to return some information when my toString() method is called, which include an integer and some floats. I learned about ostringstream works great but when the class that contains this method is called over and over again, the information gets stacked onto my previous output. Here is my code

    ostringstream int_buffer, float_buffer, float_buffer2;

is introduced at the beginning of my class, then

    string toString()
    {

        int_buffer << on_hand;
        float_buffer << price;
        float_buffer2 << generated_revenue;

        string stron_hand = int_buffer.str();
        string strprice = float_buffer.str();
        string strrev = float_buffer2.str();

        string output = "Product name: " + description + " Units left: " + stron_hand + " Price: " + strprice + " Revenue: $" + strrev;
        return output;
    }

I know my coding is awful, I’m still fairly new to this, but an example of my output is,

“Product name: Movie Ticket Units left: 49 Price: 9.99 Revenue: $9.99”

“Product name: Movie Ticket Units left: 4926 Price: 9.999.99 Revenue: $9.99239.76”

where the second one should display

“Product name: Movie Ticket Units left: 26 Price: 9.99 Revenue: $239.76”

I know it’s just a matter of updating, but that’s where I’m lost.

  • 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-11T06:38:42+00:00Added an answer on June 11, 2026 at 6:38 am

    Declare int_buffer, float_buffer, and float_buffer2 inside toString() function. Because you are declaring in the class, those objects are kept around, so every time you call toString() function you are concatenating to int_buffer, float_buffer, and float_buffer2 over and over. If you declare inside the method they will exist only while the toString is active. Anyway, you are doing too much code for what you are trying to do. You could simply do:

    std::string toString()
    {
        std::ostringstream buffer; 
        buffer << "Product name: "<< description << " Units left: " << on_hand << " Price: "<< price << " Revenue: $" << generated_revenue;
        return buffer.str();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to use HSlogger to get some information about my program. So I
I am attempting to get some information from a russian shipping website. Being a
I've got some basic questions about C++. Consider the following code in which I
Attempting to generics-ify some legacy code, I'm stuck. I have a ParentObject which wraps
I'm attempting to add some contextual information to some logs via the logging module.
I'm attempting to do some curve fitting within a class instance method, and the
I am attempting to return a rowcount from a subquery as part of a
I'm attempting to use Dapper to return a set of Shares and an associated
Attempting to build a C# NPAPI plugin I have found a tutorial which describes
Attempting to make a NSObject called 'Person' that will hold the login details for

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.