Basically the idea is to center a string. I selected the total length to equal 80. I know there is no fail safe placed into this program. I know there is another way to do this. I am stuck in a way. The program works, but I think I am doing it the hard way. any information or advice would be helpful.

#include <iostream>
#include <string>


int main(void)
{
using namespace std;
	
	string sname;
	cout << "Enter the a sentence " <<endl;
	getline(cin,sname);
	
	int s = sname.size();
	int pad1 = (80 - s)/2;
	int pad2 = pad1;
	
	for (int f = 0; f < pad1; f++)
	  {
	  cout << " ";
	  ++f;
	  }

	 cout << sname;

          for (int e = 0; e < pad2; e++)
	  {
	  cout << " ";
	  ++e;
	  }
     
	cout << endl;
}

Thank you in advance.

Recommended Answers

All 7 Replies

You could use setw() from <iomanip>

Here's how I would do it: (assuming your screenwidth of 80)

#include <iomanip>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string sname;
    cout << "Enter the a sentence " <<endl;
    getline(cin,sname);
    unsigned int padding = 40 + (sname.size() / 2);
    cout << setw(padding) << sname;
    return 0;
}

note: not tested, I don't have a compiler at the moment. but you get the basic idea!

Never thought of doing it that way. I want to take the string and center it on the line. For example assuming the screen width is 80. I want to take the string and set my padding on both sides of that string. The way your doing it if I read the code correctly your only padding the front, what about the back? Also, I want it to take into account the full screen width. (80-string size) /2
Thank you

The way your doing it if I read the code correctly your only padding the front, what about the back?

What about the back? I don't care what's in the back :) The string is centered right?

Also, I want it to take into account the full screen width.

You said your screen was 80 characters wide, so half of that is 40. Did you try my code?

I want to pad both sides. Being able to center more than one line. what if it was 20 lines?

I want to pad both sides. Being able to center more than one line. what if it was 20 lines?

Then you still do not pad the rightside. Just print a newline and start padding again :)

#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    const unsigned int SIZE = 5; // change this to your needs
    vector<string> lines;
    string sname;

    //read loop:
    for (unsigned int i = 0; i < SIZE; i++){
        cout << "Enter sentence " << i+1 << " of " << SIZE << '\n';
        getline(cin,sname);
        lines.push_back(sname);
    }   

    //display loop
    for (unsigned int j = 0; j < SIZE; j ++){
        string current_line = lines.at(j);
        unsigned int padding = 40 + (current_line.size() / 2);
        cout << setw(padding) << current_line << '\n'; // '\n' works faster then endl
    }
    return 0;
}
#include <iostream>
#include <string>


int main(void)
{
    using namespace std;

    string in_str;
    cout << "Enter the a sentence " << endl;
    getline(cin,in_str);

    int const screen_len = 10;
    int in_len = in_str.size();

    int pad_left  = (screen_len - in_len)/2;
    int pad_right = screen_len - pad_left - in_len;

    string out_str = string( " ", pad_left ) + in_str + string( " ", pad_right );
    cout << out_str;

    return 0;
}

I made a mistake in the string init syntax in my last post. Here is the corrected program.

#include <iostream>
#include <string>

int main(void)
{
    using namespace std;

    string in_str;
    cout << "Enter the a sentence " << endl;
    in_str = "and";

    int const screen_len = 10;
    int in_len = in_str.size();

    int pad_left  = (screen_len - in_len)/2;
    int pad_right = screen_len - pad_left - in_len;

    string out_str = string( pad_left, '-' ) + in_str + string( pad_right, '-' );
    cout << out_str;

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.