Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array problem 1

Status
Not open for further replies.

kevin5059

Technical User
Oct 24, 2005
6
0
0
GB
Hi, I'm new in C and I found a problem in my program. Could anybody help me to check for me code?

Here is my Codes:

***********************************************************
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <beck.h>
#include <string.h>


void Lcd_Printf(char Rough_Text_Size[16])
{
char Actual_Text_Size[8];
int Actual_Loop=0;
int Rough_Loop=0;

int String_Length=strlen(Rough_Text_Size);
/*read the size of the input text and assign it to String_Length variable*/

printf("%d\n",String_Length);

for (int j=String_Length; j<16;j++)
{
Rough_Text_Size[j]=0x20;
}
/*change all the disuse space in the array to 'SPACE'=0x20*/

while(Actual_Loop<8)
{
if(Rough_Text_Size[Rough_Loop]== '*' && Rough_Text_Size[Rough_Loop+1]=='s')
{
Actual_Text_Size[Actual_Loop]=0x20;
Actual_Loop+=1;
Rough_Loop+=2;
}
else
{
Actual_Text_Size[Actual_Loop]=Rough_Text_Size[Rough_Loop];
Actual_Loop+=1;
Rough_Loop+=1;
}
}
/*convert the "*s" to become a single 'SPACE'=0x20*/

for(int g=0; g<8 ;g++)
printf("%c",Actual_Text_Size[g]);
printf("\n");
/*print the characters*/


for (int n=0; n<16; n++)
{
Rough_Text_Size[n]=0;
}
/*reset the Rough_Text_Size array to null*/

}/*end of Lcd_Printf function*/


void main(void)
{
Lcd_Printf("hhhhhh");

Lcd_Printf("abcdef");

}
/*End of main program*/

***********************************************************

I expected the output in the screen will be:

6
hhhhhh
6
abcdef

But this program gave me the output as below:

6
hhhhhh
0



this told me that the Rough_Text_Size array values were change. However, I can't find out why. Could any expert tell me why? and what should I change in my code so that I could get the output that I expected? Thank you!

 
Not had a chance to look properly yet but can I suggest that:
a) You sort out indentation?
b) You post code in "code" tags?
Those two issues will make your code much easier to read.


Trojan.
 
Thank you Trojan, but i'm new here. I don understand what did you mean by "sort out the indentation" and "post code in code tags"??,, i'm really new in the forum anyway. Sorry for any inconvenient..
 
What he means is, when you post code, put tags [code] [/code] around it.

Then you should see something like this appear:

Code:
int main(void){
    int x, y;
    double z;

    x = 1;
    y = 2;
    z = ((double) (x+y))/2.0;

    printf("z = %f",z);

    return(0);
}

Note also how the stuff within the { } brackets is indented. It makes the code much easier to read.
 
Trojan, i think I got what you mean. I will repost my code here:

Code:
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <beck.h>
#include <string.h>


void Lcd_Printf(char Rough_Text_Size[16])
{
  char Actual_Text_Size[8];
  int Actual_Loop=0;
  int Rough_Loop=0;

  int String_Length=strlen(Rough_Text_Size);
  /*read the size of the input text and assign it to String_Length variable*/

  printf("%d\n",String_Length);

  for (int j=String_Length; j<16;j++)
  {
    Rough_Text_Size[j]=0x20;
  }
  /*change all the disuse space in the array to 'SPACE'=0x20*/

  while(Actual_Loop<8)
  {
  if(Rough_Text_Size[Rough_Loop]== '*' && Rough_Text_Size[Rough_Loop+1]=='s')
  {
     Actual_Text_Size[Actual_Loop]=0x20;
     Actual_Loop+=1;
     Rough_Loop+=2;
  }
  else
  {
  Actual_Text_Size[Actual_Loop]=Rough_Text_Size[Rough_Loop];
  Actual_Loop+=1;
  Rough_Loop+=1;
  }
  }
  /*convert the "*s" to become a single 'SPACE'=0x20*/

  for(int g=0; g<8 ;g++)
  printf("%c",Actual_Text_Size[g]);

  printf("\n");
  /*print the characters*/


  for (int n=0; n<16; n++)
  {
    Rough_Text_Size[n]=0;
  }
  /*reset the Rough_Text_Size array to null*/

}/*end of Lcd_Printf function*/


void main(void)
{
   Lcd_Printf("hhhhhh");

   Lcd_Printf("abcdef");

}
/*End of main program*/
 
I've just started to clean up and look at your code and I'm a little scared.
I don't like the fact that you're passing a literal string in with 6 characters and therefore 7 bytes and yet treating it as a 16 char array when seen in the function Lcd_Printf.
What is stopping you using a 16byte char array scoped in your function and just copy the bits across that you need.
I'll keep tinkering and post more soon.


Trojan.
 
Can you just explain what you're trying to get this code to do please?
It looks like you're swapping '*s' sequences for spaces and adding a linefeed and then printf'ing the result.
If you can confirm that this is all you want then I suspect I can write something far simpler for you.


Trojan.
 
I've had a quick go for you.
Let me know if this helps at all:
Code:
#include <stdio.h>
#include <string.h>

#define BLOCKSIZE 16

void Lcd_Printf(char *incoming)
{
        char text_block[BLOCKSIZE+1];
        int  index    = 0;    /* index into text_block               */
        int  starflag = 0;    /* Flag to mark that last char was '*' */

        /* Blank the block with spaces */
        memset(text_block, ' ', BLOCKSIZE);

        /* Walk through all chars from input processing *s as we go        */
        /* Note: by limiting to *incoming AND BLOCKSIZE we can't overshoot */
        while(*incoming && index < BLOCKSIZE) {
                /* Even if we are at the end it's safe to read 1 char past */
                /* since there will be a '\0' terminator there             */
                if(*incoming == '*' && *(incoming+1) == 's') {
                        /* replace '*s' with a space */
                        text_block[index++] = ' ';
                        /* extra increment here cos we're skipping 2 chars */
                        incoming++;
                } else {
                        text_block[index++] = *incoming;
                }
                /* finished with that char so on to the next if there is one */
                incoming++;
        }
        /* Add a terminator AFTER the block to allow printf to use it easily */
        text_block[BLOCKSIZE] = '\0';
        printf("[%s]\n", text_block);
}

int main(int argc, char **argv)
{
        Lcd_Printf("hhhhhh");
        Lcd_Printf("abcdef");
        Lcd_Printf("ab*sef");

        return 0;
}
/*End of main program*/


Trojan.
 
Thank you Trojan, I tried to make use of your program, and it work perfectly for my hardware. Thank you very much! Anyway, my purpose for this function is to display the text in my LCD display! Again, thank you!
 
I guessed it was for an LCD display. I think the function name was a bit of a "give-away".
I put the square brackets in to show you the padding spaces but then I guess you worked that much out.


Trojan.
 
... and just for fun, an alternative solution:
Code:
void Lcd_Printf(char *incoming)
{
  int Counter;
  
  putchar ('[');
  
  for (Counter = 0; *incoming; Counter++, incoming++)
  {
    if (*incoming == '*' && *(incoming+1) == 's')
    {
      putchar(' ');
      incoming++;
    }
    else
    {
      putchar(*incoming);
    }
  }

  printf ("%*s\n", BLOCKSIZE-Counter+1, "]");
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top