Solved

# Limiting character output

Posted on 2006-04-29
221 Views
I'd like to limit the # of characters displayed on the screen to 61 in void printDeque () below.
______________________________________________________

#include <iostream>
#include <algorithm>
#include <deque>

using namespace std;

template<class T>
void printDeque(const deque<T>& lst, char *s) {
cout <<"The list is "<< s << ":"<< endl << endl;
int j = 1;
typename deque<T>::const_iterator i = lst.begin();
for ( ; i != lst.end(); i++)
{
cout << *i << ' ';
}
cout << endl << endl<<endl;
}

int main()
{
deque<int> dq1;
deque<int> dq3;
int i;

cout << " This program demonstrates some things we can do with the deque class "
<< endl;

/* Populates the deque list dq1 with numbers 1 to 100.
Uses the modulus to determine the even numbers. */
for (i=1; i<=100; i++)
{
if ( i%2==0)
{
dq1.push_back(i);            //puts the even numbers in the back
}
else
{
dq1.push_front(i);            //puts the odd numbers in the front.
}
}

sort(dq1.begin(), dq1.end());
printDeque(dq1, "dq1");

deque<int>dq2(dq1.begin()+50,dq1.end()-0);

printDeque(dq2, "dq2");
dq3.resize(dq1.size()+dq2.size());

merge(dq1.begin(),dq1.end(),dq2.begin(),dq2.end(),dq3.begin());
printDeque(dq3, "dq3");
reverse(dq3.begin(),dq3.end());
printDeque(dq3, "dq3");

cout << "\n Thanks for watching." <<endl<<endl;

system("pause");

return 0;
}
0
Question by:Wookie68

LVL 86

Expert Comment

template<class T>
void printDeque(const deque<T>& lst, char *s) {
cout <<"The list is "<< s << ":"<< endl << endl;
int j = 1;
int n = 0;
typename deque<T>::const_iterator i = lst.begin();
for ( ; i != lst.end(); i++)
{
cout << *i << ' ';
if (!(n++ % 61)) cout << endl; // <--------
}
cout << endl << endl<<endl;
}

Do you want a line feed or a hard limit? If the latter applies, make the above

if (!(n++ >= 60)) break;
0

Author Comment

Hello jkr -
Thanks for the help. Here is the output below from one of the calls... Can it be kept to less than 61 characters including the spaces?
____________________________________________________________
This program demonstrates some things we can do with the deque class
The list is dq1:

1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 3
1 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
58 59 60 61 62
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99 100
______________________________________________________________
Can it be made to look like this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

0

LVL 86

Expert Comment

Ah, I see - well, that's going to be difficult using my 1st suggestion, try

#include <iostream>
#include <algorithm>
#include <deque>
#include <sstream>

using namespace std;

template<class T>
void printDeque(const deque<T>& lst, char *s) {
cout <<"The list is "<< s << ":"<< endl << endl;

string line;
typename deque<T>::const_iterator i = lst.begin();
for ( ; i != lst.end(); i++)
{
stringstream ss;
ss << *i << ' ';
if (ss.str().size() + line.size() > 61) {

cout << line << endl;
line.erase();
}
line += ss.str();
}
cout << endl << endl<<endl;
}

int main()
{
deque<int> dq1;
deque<int> dq3;
int i;

cout << " This program demonstrates some things we can do with the deque class "
<< endl;

/* Populates the deque list dq1 with numbers 1 to 100.
Uses the modulus to determine the even numbers. */
for (i=1; i<=100; i++)
{
if ( i%2==0)
{
dq1.push_back(i);          //puts the even numbers in the back
}
else
{
dq1.push_front(i);          //puts the odd numbers in the front.
}
}

sort(dq1.begin(), dq1.end());
printDeque(dq1, "dq1");

deque<int>dq2(dq1.begin()+50,dq1.end()-0);

printDeque(dq2, "dq2");
dq3.resize(dq1.size()+dq2.size());

merge(dq1.begin(),dq1.end(),dq2.begin(),dq2.end(),dq3.begin());
printDeque(dq3, "dq3");
reverse(dq3.begin(),dq3.end());
printDeque(dq3, "dq3");

cout << "\n Thanks for watching." <<endl<<endl;

system("pause");

return 0;
}
0

Author Comment

Real close... this is what I get now... I'd still like to print the whole deque, just limit the lines to the max length as you have already done.
____________________________________________

This program demonstrates some things we can do with the deque class
The list is dq1:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
0

LVL 86

Expert Comment

Um, it lists the whole thing here just as you want it to be - what am I missing?
0

Author Comment

The whole queue is from 1 - 100. It looks like it is getting cut off at 83...
0

Author Comment

I got a little quick with the enter key...
The whole queue is from 1 - 100. It looks like it is getting cut off at 83... I still want it to print the whole list, I just don't want the lines to be over 61 chars including the spaces.
0

LVL 86

Accepted Solution

*slapping my forehead* - sorry ;o)

That should be

template<class T>
void printDeque(const deque<T>& lst, char *s) {
cout <<"The list is "<< s << ":"<< endl << endl;

string line;
typename deque<T>::const_iterator i = lst.begin();
for ( ; i != lst.end(); i++)
{
stringstream ss;
ss << *i << ' ';
if (ss.str().size() + line.size() > 61) {

cout << line << endl;
line.erase();
}
line += ss.str();
}

cout << line << endl;

cout << endl << endl<<endl;
}

then (sorry again, but it's early in the morning here)
0

Author Comment

That did the trick. Thanks for the help!!!
I learn something new each day as I try to work through some of these exercises. Still much more to learn.
0

LVL 86

Expert Comment

>>Still much more to learn.

You'll be done with learning only when you're dead. That also applies to C++ *g*
0

## Featured Post

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.