I am trying to create a program using formatted output to the screen, two columns 15 characters wide, with the output being strings. If the string is longer than 15 characters I want to truncate it to fit in the column. So far this is what I have:
Code:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
string mystring1="This is a string";
string mystring2="This is another string";
string tempstring="";
cout<<setiosflags(ios::left);
cout<<setw(17)<<"String 1"<<"String2\n";
cout<<tempstring.assign(mystring1, 0, 15)<<" ";
tempstring="";
cout<<tempstring.assign(mystring2, 0, 15)<<"\n";
return 0;
}
This works, but I was wondering if there was a way to do it without the use of tempstring. Maybe a string member function?