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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:09:22+00:00 2026-05-11T03:09:22+00:00

I’ve got a function inside of a class that returns a string. Inside this

  • 0

I’ve got a function inside of a class that returns a string. Inside this function, I can only get it to work when I add cout<<endl to the function before the return statement. Any idea why this is, or how I can fix it? I’m running this in Eclipse on a Mac

In ‘main.cpp’:

#include <iostream> #include <fstream> #include <string> #include <vector> #include <cstdlib> #include 'Braid.h' using namespace std;  static int size=3;  int main(){   Braid * b1 = new Braid(size); b1->setCanon();//creates canonical braid.   cout<<'a '; cout<<b1->getName()<<endl; cout<<' b '; }   

In ‘Braid.h’ :

  public:           Braid(int);           void setCanon();           string getName();      };   

And in ‘Braid.cpp’:

string Braid::getName(){       string sName='';        /* body commented out     for(int i=0; i<height; i++)     {         for(int j=2; j<(width-2); j++)             {                 sName += boxes[i][j];                 sName += '|';             }         }          */           //cout<<endl;         return sName;     }  

When I run my main code, without the body of that function commented, the output I get is
‘a 0|0|12|12|0|0|2|1|1|1|1|2|’

The ‘name’ it returns is correct, but it’s not making it past the function call. If I uncomment the //cout<<endl line, the function works and my output is
‘a 0|0|12|12|0|0|2|1|1|1|1|2|
b ‘

After commenting out the body of the function, so that it only creates an empty string, and returns it, my output is only ‘a’ then if I add the endl back, I get the ‘a b’ that is expected.

What am I doing wrong? Is there something that comes with endl that I’m missing?

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T03:09:23+00:00Added an answer on May 11, 2026 at 3:09 am

    actually getName() function is probably working correctly. However, the cout ‘caches’ the output (i.e. it prints the output on screen when it’s internal text buffer is full). ‘endl’ flushes the buffer and forces cout to dump the text (in cache) to screen.

    Try cout.flush() in main.cpp

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is nothing wrong with running (limited) code in the… May 11, 2026 at 11:49 pm
  • Editorial Team
    Editorial Team added an answer Error Handling in VBA On Error Goto ErrorHandlerLabel Resume (Next… May 11, 2026 at 11:49 pm
  • Editorial Team
    Editorial Team added an answer Assignment is (sort of) right to left: int a =… May 11, 2026 at 11:49 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.