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

Word Wrap in VC++ console app

Hi All
I am working on a small project where i am inputting a string then outputing that same string within a box, where the string is wrapped to fit in that box.
A pointer to the string is passed to the function.
What i have at the min is a for loop indexing the string, I then check for a 'space', when the space is found i copy into another string, the original string upto x places(where the space is found). If the str is longer than the box is wide i need to wrap only on a space. So at the mo, i have the first line in a temp string (this is fine prints to the screen ok).  So i now move to line two where i save the location of the space with a pointer, so i now print line two from the pointer.  This is also fine, what i cant seem to do is print the middle of the string (if say the string goes over 2 lines).  e.g. How do i save from point 10 to point 20 in the string?
When i keep updating the space pointer, it will not print anything before the space so i loose words if max width is not true.
What i thought i may be able to do is use an array of pointers, with each pointing to one line?  But i am not convinced that will do the trick.

Maybe you guys will have some ideas, and give me a few pointers on how i should go about doing this.

Thanks in advance.
Steve
0
osiris247
Asked:
osiris247
  • 3
  • 3
2 Solutions
 
AxterCommented:
I recommend you use std::string and std::vector.

You can store multiple lines of different width with a vector of std::string's

#include <string>
#include <vector>

std::vector<std::string>  MyArrayOfStrings;

Please post your existing code, so we can get a better picture of your requirements.
0
 
osiris247Author Commented:
This code below will do two lines, I think i am getting there with the way i am doing it just need put a check in for more than two lines.
I have never used string or vector, I am new to C++.
My code seems abit mixed up to me?! What do you think?
Thanks

      for(i=0;str[i];i++)
      {
            if(str[i] == ' ')
            {
                  space=i;
                  if (linecount == 0)
                  {
                        if (i<=maxwidth)
                        {
                              space=i;
                              strncpy(temp, str, i);
                              temp[i]='\0';
                        }
                  
                        else if (count>maxwidth)
                        {
                              gotoxy(xpos,ypos);
                              cout<<ln<<endl;
                              ptr_space = &str[i];//point to the space found
                              count=0;
                              ypos++;//increment y co-ordinate for carriage return within box
                              gotoxy(xpos,ypos);
                              linecount++;//increment line counter
                              cout << ptr_space << " space pointer\n ";
                        }
                  }//end of check for linecount if

                  
            }// end of "check for spaces" if statement
0
 
AxterCommented:
>>gotoxy(xpos,ypos);

What compiler are you using?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
osiris247Author Commented:
I am using visual c++, but i have two files included that have the code for gotoxy function.
0
 
AxterCommented:
I recommend you learn the std::string class, and use that instead.
The best way to learn is by practice.
0
 
PaulCaswellCommented:
I did something like this before writing a word-processor. The essence is the same though.

What you do is keep a copy of the most recent space that was within your borders. Each time you count a character beyond your border, step back to the space, emit everything up to it and start counting from there.

Something like:

int start = 0;
int lastSpace = 0;

for(int i=0;str[i] != '\0';i++)
{
 if ( str[i] == ' ' )
 {
  lastSpace = i;
 }
 else
 {
  if(i-start > width)
  {
    // Gone too far!
    // Do your gotoxy here for location.
    fwrite(&str[start],lastSpace-start,1,stdout);
    // Start after the space.
    start = lastSpace + 1;
    lastSpace = start;
  }
 }
}


That should be a good start. You'll have to deal with the case where you havent seen a space before you go over margin or the margin is at zero or you get multiple spaces or its all spaces but that's the essence of the algorithm.

Paul
0
 
osiris247Author Commented:
Paul
Thanks for that mate, you have ended my misery!!
One thing though, it doesn’t output the final line?  Because there is obviously no space found after the final word, but even when I added a space it still didn’t output? Give me something to work on though!
Fwrite… what a fab function, certainly one for the memory banks!

Thanks to Axter for bringing up the string class, I am hopefully going to finish up passing the section of the string for that line to a class.  So I it will be indispensable i think.
It’s for a college project you see, and the whole point is to use multiple inheritance.  Very surprised I have not been taught the string class yet.  From the short and basic tutorials I have read so far it certainly intrests me.
If you know of any good tutorials out there I am always after a good one!

Thanks again guys
Steve
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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