• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

Limiting character output

I'd like to limit the # of characters displayed on the screen to 61 in void printDeque () below.  
Thanks for your help!  
______________________________________________________

#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
Wookie68
Asked:
Wookie68
  • 5
  • 5
1 Solution
 
jkrCommented:
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
 
Wookie68Author Commented:
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
 
jkrCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Wookie68Author Commented:
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
 
jkrCommented:
Um, it lists the whole thing here just as you want it to be - what am I missing?
0
 
Wookie68Author Commented:
The whole queue is from 1 - 100. It looks like it is getting cut off at 83...
0
 
Wookie68Author Commented:
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
 
jkrCommented:
*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
 
Wookie68Author Commented:
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
 
jkrCommented:
>>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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now