I have an assignment where I am required to output an array which contains information about songs. The problem I am having is formatting the output. My assignment specifies the length of each field that is displayed but I cannot find a good way to restrict the output. For example if a song title has 21 characters but is required to be 18, how would I keep it from going over the specified 18. I am using the setw() function to space everything correctly but it does not restrict output at all.
You can resize a c++ string using string::resize.
// resizing string
#include <iostream>
#include <string>
int main ()
{
std::string str ("I like to code in C");
std::cout << str << '\n';
unsigned sz = str.size();
.resize (sz+2,'+');
std::cout << str << '\n'; //I like to code in C++
str.resize (14);
std::cout << str << '\n';//I like to code
return 0;
}