Link to home
Start Free TrialLog in
Avatar of osiris247
osiris247Flag for United States of America

asked on

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
Avatar of Axter
Axter
Flag of United States of America image

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.
Avatar of osiris247

ASKER

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
>>gotoxy(xpos,ypos);

What compiler are you using?
I am using visual c++, but i have two files included that have the code for gotoxy function.
SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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