Login
User Name:

Password:



Register
Forgot your password?
Vote for Us!
auth_update crash
Dec 23, 2017, 10:15 pm
By Remcon
check_tumble
Dec 18, 2017, 7:21 pm
By Remcon
parse description bug
Dec 15, 2017, 10:08 pm
By Remcon
Couple bugs
Dec 12, 2017, 5:42 pm
By Remcon
Bug in disarm( )
Nov 12, 2017, 6:54 pm
By GatewaySysop
LoP 1.46
Author: Remcon
Submitted by: Remcon
LOP 1.45
Author: Remcon
Submitted by: Remcon
LOP Heroes Edition
Author: Vladaar
Submitted by: Vladaar
Heroes sound extras
Author: Vladaar
Submitted by: Vladaar
6Dragons 4.3
Author: Vladaar
Submitted by: Vladaar
Users Online
CommonCrawl, Bing, Yandex

Members: 0
Guests: 8
Stats
Files
Topics
Posts
Members
Newest Member
478
3,708
19,242
614
BenitoVirg
Today's Birthdays
There are no member birthdays today.
Related Links
» SmaugMuds.org » General » Coding » [SOLVED]Need a little help wi...
Forum Rules | Mark all | Recent Posts

[SOLVED]Need a little help with this.
< Newer Topic :: Older Topic >

Pages:<< prev 1 next >>
Post is unread #1 Jan 19, 2010, 1:04 am
Go to the top of the page
Go to the bottom of the page

Andril
Magician
GroupMembers
Posts147
JoinedJun 9, 2009

I had a crazy idea to add optional tags to room descriptions in order to make them more dynamic, in a similar fashion to the one Remcon instituted for LoP. It's not where I want it to be, doesn't support nested or multiple tags yet, but the system works as is for the most part. The problem I'm running into right now is with my string wrapping function.
Specifically this little bit. This is using an instance of a std::string.
// strip all excess spaces, ' ', from the string
for( unsigned int i = 0; i < desc.size(); i++ )
   if( desc[i] == ' ' && desc[i + 1] == ' ' )
      desc = desc.erase(i, 1);

It's just not erasing that first space. I've tried replacing that spot with nothing, "", but it doesn't work then either. If anyone can clue me in to what's going wrong I'd be eternally grateful.
       
Post is unread #2 Jan 19, 2010, 8:21 am
Go to the top of the page
Go to the bottom of the page

Darrik
Fledgling
GroupMembers
Posts20
JoinedJan 29, 2008

As far as I can tell, your code should work. I just reproduced it:
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
  string desc, tmp;

  while(1)
  {
  cout << "Enter string: ";
  
  getline( cin, desc );
  
  // strip all excess spaces, ' ', from the string
  for( unsigned int i = 0; i < desc.size(); i++ )
    if( desc[i] == ' ' && desc[i + 1] == ' ' )
      desc = desc.erase(i, 1);
  
  cout << desc << endl;
  }
}


and it works perfectly:
Enter string: Counting  spaces  sucks
Counting spaces sucks


Are you sure you are using the modified string when checking the results?

DV
       
Post is unread #3 Jan 19, 2010, 10:34 am   Last edited Jan 19, 2010, 10:36 am by Andril
Go to the top of the page
Go to the bottom of the page

Andril
Magician
GroupMembers
Posts147
JoinedJun 9, 2009

Here's the whole wrap_string function so you can see exactly what's going on.
// wrap a string "str" at the desired column "col". Defaults to 75
string wrap_string( string str, unsigned int col = 75 )
{
   string desc(str);

   // if the size of the string is less then col we don't need to do anything
   if( desc.size() < col )
      return desc;
   // if col is less then 50 there's really no point to doing this anyways
   if( col < 50 )
      return desc;

   // strip all excess spaces, ' ', from the string
   for( unsigned int i = 0; i < desc.size(); i++ )
      if( desc[i] == ' ' && desc[i + 1] == ' ' )
         desc = desc.erase(i, 1);

   unsigned int loc(col);
   bool done = false;
   do 
   {
      // well that was easy!
      if( desc[loc] == ' ' )
      {
         desc = desc.replace(loc, 1, "\r";);
         desc = desc.insert(loc + 1, "\n";);
      }
      else
      {
         int a(0), b(0);
         // how many characters from current location back is next space
         for( int i = loc; desc[i] != ' '; i--, a++ )
            ;
         // how many forwards?
         for( int i = loc; desc[i] != ' '; i++, b++ )
            ;
         // wrap at space, ' ', closest to desired column
         if( a < b )
         {
            desc = desc.replace(loc - a, 1, "\r";);
            desc = desc.insert(loc - a, "\n";);
         }
         else
         {
            desc = desc.replace(loc + b, 1, "\r";);
            desc = desc.insert(loc + b, "\n";);
         }
      }
      loc += col;
      if( loc >= desc.size() )
         done = true;
   } while (!done);
   return desc;
}

wrap_string is called in process_room_desc like so: desc = wrap_string(desc);

This is what I see in-game in the room that I'm using to test the whole tag system as well as the actual room desc.
Output:
This is the office of the Immortal Andril. kwe kwe kwe kwe kwe kwe kwe kwe
kwe kwe kwe kwe kwe

Actual description:
1> This is the office of the Immortal Andril.
2> #nelona[test]
3> #elona[test]
4> #relona[relona]
5> kwe
6> kwe
7> kwe
8> kwe
9> kwe
10> kwe
11> kwe
12> kwe
13> kwe
14> kwe
15> kwe
16> kwe
17> kwe

As you can see, lines 2, 3 and 4 have spaces in the beginning because zMUD treats the # sign as special, and I can't get it to stop, so I want to make sure those excess spaces are removed. Which they aren't, for some reason.

Oddly enough, the replace and insert calls work fine. Just not that erase one.
       
Post is unread #4 Jan 19, 2010, 11:21 am
Go to the top of the page
Go to the bottom of the page

Darrik
Fledgling
GroupMembers
Posts20
JoinedJan 29, 2008

Now, a small disclaimer, looking at string parsing functions gives me a headache, but I believe I'm following:

You are inputting the following:
1> This is the office of the Immortal Andril.
2>  #nelona[test]
3>  #elona[test]
4>  #relona[relona]
5> kwe
6> kwe
7> kwe
8> kwe
9> kwe
10> kwe
11> kwe
12> kwe
13> kwe
14> kwe
15> kwe
16> kwe
17> kwe


And getting this:
Output:
This is the office of the Immortal Andril.    kwe kwe kwe kwe kwe kwe kwe kwe
kwe kwe kwe kwe kwe 


If this is correct, your problem is that you are checking for double spaces before there are any. For example, the input:
 #nelona[test]
 #elona[test]


Is, in the string:
' #nelona[test]\n #elona[test]\n'

(It may be \n\r, depending on your system.)
In that string, there are no double spaces. You should run your double space for-loop after you run through the do/while loop to get the intended effect.

DV
       
Post is unread #5 Jan 19, 2010, 11:29 am   Last edited Jan 19, 2010, 11:35 am by Andril
Go to the top of the page
Go to the bottom of the page

Andril
Magician
GroupMembers
Posts147
JoinedJun 9, 2009

Bah! Guess I'll just move it down to right before returning the string. It's always the little stuff that throws everything off.. :)

Now if I can just get the rest of it working the way I want I'll be a happy camper.

Thanks for the help Darrik.
       
Pages:<< prev 1 next >>