Author Topic: [C++] Len, Mid$ And Asc: String / Text Commands  (Read 6597 times)

0 Members and 1 Guest are viewing this topic.

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
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.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [C++] Len, Mid$ And Asc: String / Text Commands
« Reply #1 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

« Last Edit: August 17, 2009 by Jim »
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: [C++] Len, Mid$ And Asc: String / Text Commands
« Reply #2 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.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [C++] Len, Mid$ And Asc: String / Text Commands
« Reply #3 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
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: [C++] Len, Mid$ And Asc: String / Text Commands
« Reply #4 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
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [C++] Len, Mid$ And Asc: String / Text Commands
« Reply #5 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
« Last Edit: August 24, 2009 by Jim »
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: [C++] Len, Mid$ And Asc: String / Text Commands
« Reply #6 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 )
{
Challenge Trophies Won: