Dark Bit Factory & Gravity

PROGRAMMING => C / C++ /C# => Topic started by: Clyde on August 17, 2009

Title: [C++] Len, Mid$ And Asc: String / Text Commands
Post by: Clyde on August 17, 2009
Im trying to develop my skills with a bitmap printer, in Blitz / Freebasic you'd use the following:

Code: [Select]
text_message="hello there how's the weather"
....
for index=1 to len(text_message); length of the message text.
 frame=(asc(mid(text_message,index,1)))-32 ;splits message into appropriate letter.
 draw_image( x, 120, frame)
 x=x+fontwidth
next

I'd love to know how to go about using those commands - ( i dont want the code for an entire bitmap routine as i'll work on that part ), just the equivalents: some more example are, left$ / right$, upper / ucase ), or similar if they dont exist in C++.

Hugest of thanks,
Clyde.
Title: Re: [C++] Len, Mid$ And Asc: String / Text Commands
Post by: Jim on August 17, 2009
Code: [Select]
#include <string.h>
...
char text[]="Three cheers for Clyde";
unsigned int len = strlen(text);//22
As I put in my other post, you need to add
Code: [Select]
using namespace std;
after the last of your includes to access the functions.

Since in this case in C++ a string is just an array of letters, you don't need Mid$(text, posn, 1), you just use
text[posn], remembering that posn starts at 0, ie text[0] is T, text[6] is c.

You don't need Asc(), letters are just char, which is just a number.

Code: [Select]
char text_message[]="hello there how's the weather";
...
for (int index=0; index < strlen(text_message); index++) //length of the message text.
{
 char frame=text_message[index]-32; //splits message into appropriate letter.
 draw_image(x, 120, frame);
 x+=fontwidth;
}

Jim

Title: Re: [C++] Len, Mid$ And Asc: String / Text Commands
Post by: Clyde on August 23, 2009
this is strange when I add using namespace std;  it keeps telling me that a namespace of that name doesnt exist.
Title: Re: [C++] Len, Mid$ And Asc: String / Text Commands
Post by: Jim on August 23, 2009
If everything's working OK without it, then don't worry.  At the moment the C++ code you're writing looks like C, that is, you're using zero of the C++ language features.  When we move more in the C++ direction it'll become more useful.

Jim
Title: Re: [C++] Len, Mid$ And Asc: String / Text Commands
Post by: Clyde on August 24, 2009
for ( int a=0; a<strlen(text); a+=1 )
char asc_letter=text[a]-32;
//

the above doesnt give me asc order / the correct letters to be drawn.
HELLO = MJRRU

i've tried -37, which displays H, but on the next increment of loop a, the letter isnt an E but D.

btw for the none returning function to work, i need to have in the call char *text
Title: Re: [C++] Len, Mid$ And Asc: String / Text Commands
Post by: Jim on August 24, 2009
Something else must be wrong, since the code above is correct.

Have you tried printing your alphabet to check everything's in the right place?

for (int a=32; a < 127; a++)
  draw_graphic(a-32);

Jim
Title: Re: [C++] Len, Mid$ And Asc: String / Text Commands
Post by: hellfire on August 24, 2009
In create_anim_images you're missing the last character of each line.
Your loops should look something like this:
Code: [Select]
for (y=0; y<frame_h*height; y+=height )
{
for (x=0; x<frame_w*wwidth; x+=wwidth )
{