Logical operators and while and string compare.
Displaying 1-5 of 5 total.
1
Please enter a numerical value for the importance of this sticky.
Enter 0 to unsticky.
Keast

Help again! This time it's how to combine OR with a while loop.

while (strcmp(at," "))
{
	atco++;
	at = mid(t,atco,1);
}


Works like a charm.

while (strcmp(at," ") || strcmp(at,"?") || strcmp(at,"!"))
{
	atco++;
	at = mid(t,atco,1);
}


Does not work. Crashes, rather.

What now?

Oh yes, this is supossed to break a text line at spaces and punctuation.

Posted on 2011-03-30 08:33:14 (last edited on 2011-03-30 08:35:08)

mcgrue

If the while-loop is supposed to break on spaces or punctuation, I think what you actually want is this (not tested, lunch-break psuedo-coded):


int function should_break( at ) {
    return !strcmp(at," ") || !strcmp(at,"?") || !strcmp(at,"!");
}

while( !should_break(at) ) {
    atco++;
    at = mid(t,atco,1);
}



strcmp will return 0 if the strings are equal. The way you originally coded it would always run, forever (because at least one of the three clauses would evaluate to -1 or 1, both of which are "true", and since they were or'd together you'd get an infinite loop.)

Posted on 2011-03-30 14:03:11 (last edited on 2011-03-30 14:03:33)

Overkill

Originally by Keast:

Help again! This time it's how to combine OR with a while loop.

while (strcmp(at," "))
{
	atco++;
	at = mid(t,atco,1);
}


Works like a charm.

while (strcmp(at," ") || strcmp(at,"?") || strcmp(at,"!"))
{
	atco++;
	at = mid(t,atco,1);
}


Does not work. Crashes, rather.

What now?

Oh yes, this is supossed to break a text line at spaces and punctuation.


Basically, right here you mixed up && with ||. It's not really 'crashing' so much as going irresponsive because it's in an infinite loop without ShowPage().

But yeah, if you want a loop that runs only while ALL of the conditionals are true (ie. run until ANY of the conditions is false), use && in your while().
while (strcmp(at," ") && strcmp(at,"?") && strcmp(at,"!"))
{
	atco++;
	at = mid(t,atco,1);
}


Alternatively, use an until() statement. until(condition) is equivalent to while(!(condition)):
until (strcmp(at," ") || strcmp(at,"?") || strcmp(at,"!"))
{
	atco++;
	at = mid(t,atco,1);
}


What Grue said will also work!

Also, if you're breaking a line at spaces/punctuation, is this for word-wrapping a textbox? In that case, you could just use WrapText. (I'd link to the doc entry for that, but the docs are linking to the wrong thing at the moment -- Grue? :o).

Posted on 2011-03-30 16:00:42 (last edited on 2011-03-30 18:06:45)

Keast

Thanks to both of you. Isn't Wraptext doing something different?
http://verge-rpg.com/docs/the-verge-3-manual/font-functions/wraptext/
Can't really figure out why I'd want a | in there though.

Posted on 2011-04-02 15:48:55

Overkill

There's a bit of a bug in the documentation atm. That WrapText entry is the one from the Code Vault documentation (which was a bunch of old v3 helper functions, before some things got built-in to Verge, and were made a little bit better.)

Uhhh, I can probably nuke that from the code vault to be honest, which will solve that problem for now. But basically the doc is looking up the function name and not making sure that function belongs to the correct doc first.



The real wraptext docs are a bit different. For now I'll just post it here:

str WrapText(int font, str text, int w)

Wraps a string text to fit within a box of w pixels wide, using font's letter sizes to determine when to wrap things. The resulting string will have a bunch of \n (ie. chr(10)) line break characters put into the string, to separate the lines. Functions such as PrintString, PrintCenter, and PrintRight will obey these line breaks.

Example:
str message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut ac aliquet eros. Proin pellentesque ipsum nec odio blandit vitae vestibulum risus elementum. Pellentesque et purus nibh, ut venenatis nisi. Mauris eget ligula id justo commodo condimentum ut sed neque. Sed tellus odio, mattis at interdum sed, accumsan sit amet sem. Proin mi risus, lobortis id faucibus vitae, iaculis ac lacus. Fusce sollicitudin, quam nec venenatis dapibus, leo ipsum mollis tortor, ut semper massa ante ornare erat. In hac habitasse platea dictumst. Aliquam dapibus urna ac justo vestibulum bibendum auctor tortor sodales. Donec commodo volutpat velit. Vivamus mollis lacus ut massa gravida condimentum.";
message = WrapText(0, message, 200);
PrintString(5, 5, screen, 0, message);

Posted on 2011-04-03 12:40:54


Displaying 1-5 of 5 total.
1
 
Newest messages

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.