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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:28:11+00:00 2026-06-03T12:28:11+00:00

I’m learning c++ and got the project to send a pascal’s triangle to output

  • 0

I’m learning c++ and got the project to send a pascal’s triangle to output (after n-rows of calculation)., getting output like this, stored in a stringstream “buffer”

 1
 1 1
 1 2 1
 1 3 3 1

But what I want is rather

    1
   1 1
  1 2 1
 1 3 3 1

My idea was: calculate the difference of the last line and current line length (I know that the last one is the longest). Then pad each row using spaces (half of the line-length-difference).
My Problem now is:

  • I didn’t get how getLine works, neither how I might extract a specific (-> last) line
  • I don’t know and could not find how to edit one specific line in a stringstream

Somehow I got the feeling that I’m not on the best way using stringstream.

So this is rather a common question: How’d you solve this problem and if possible with stringstreams – how?

  • 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-03T12:28:14+00:00Added an answer on June 3, 2026 at 12:28 pm

    To know the indentation of the first line, you would need to know the number of lines in the input. Therefore you must first read in all of the input. I chose to use a vector to store the values for the convenience of the .size() member function which will give the total number of lines after reading in all input.

    #include<iostream>
    #include<sstream>
    #include<vector>
    #include<iomanip> // For setw
    using namespace std;
    int main()
    {
      stringstream ss;
      vector<string> lines;
      string s;
    
      //Read all of the lines into a vector
      while(getline(cin,s)) 
        lines.push_back(s);
    
      // setw() - sets the width of the line being output 
      // right  - specifies that the output should be right justified 
      for(int i=0,sz=lines.size();i<sz;++i)
        ss << setw((sz - i) + lines[i].length()) << right << lines[i] << endl;
    
      cout << ss.str();
      return 0;
    }
    

    In this example, I am using setw to set the width of the line to be right justified. The padding on the left side of the string is given by (sz – i) where sz is the total number of lines and i is the current line. Therefore every subsequent line has 1 less space on the left hand side.

    Next I need to add in the original size of the line (lines[i].length()), otherwise the line will not contain a large enough space for the resulting string to have the correct padding on the left hand side.

    setw((sz - i) + lines[i].length())
    

    Hope this helps!

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:

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.