Hi,
I was stuck in simple thing. Please don't mind replying for this simple stuff it will avoid wasting my time because already wasted a lot for the same .
If a string contains some 10 chars, but I want to display only 3characters per line, then what flag I need to set for achieving this output in cout ?
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
#include <iomanip>
usingnamespace std;
int main () {
// cout << setw (2) << "11111"; //tried this
cout.flags(ios::left);
cout.width(10); // tried this
cout << "7777777777777777777777777777777777777" << endl;
return 0;
}
but it is printing whole string in single line(7777777777777777777777777777777777777).
I understand setw/width() is not for this reason. But I googled but didn't get much info for this. Can anyone indicate me the exact function.
#include <iostream>
#include <string>
#include <windows.h>
usingnamespace std;
int main ()
{
string line = "7777777777777777777777777777777777777";
int x, len;
len = line.length();
cout << "Displaying 10 characters per line.." << endl << endl;
for (x=1;x<len;x++)
{
cout << line[x-1];
if (x%10==0)
cout << endl;
}
cout << endl << endl;
cout << "Now displaying only 3 characters per line.." << endl << endl;
for (x=1;x<len;x++)
{
cout << line[x-1];
if (x%3==0) // change number after mod sign (%) to specify how many characters per line
cout << endl;
}
cout << endl << endl << endl;
return 0;
}
Yeah thanks for your valuable response. Thought any inbuild function is there in C++ instead of doing it manually. Anyhow I will use the same. Thanks a lot.