Menu

printing string array

Theo
2014-09-07
2014-09-10
  • Theo

    Theo - 2014-09-07

    Hello, i'm Theo and i am using GC Basic now for a few months.
    It's a great program and i would like to say thanks to all the people involved in developing and testing.

    But now I have a small problem. Printing stringarray's goes wrong, the output is not as expected; the output is a number.
    See listing:

    ;-------------------------------------------------

    chip mega8,8

    'Use LCD in 4 pin mode and define LCD pins

    define LCD_IO 4

    define LCD_DB4 PORTD.0 ; 11

    define LCD_DB5 PORTD.1 ; 12

    define LCD_DB6 PORTD.2 ; 13

    define LCD_DB7 PORTD.3 ; 14

    define LCD_RS PORTD.4 ; 4

    define LCD_Enable PORTD.5 ; 6

    define LCD_NO_RW

    lcdcmd 1
    wait 1 s

    dim test as string

    test(1) = "ab"
    test(2) = "ab"

    if test(1) = test(2) then
    locate 0,0
    print "Equal."
    end if
    locate 1,0
    print test(1)
    print " "
    print test(2)

    ;--------------------------------------------

    GCBasic version (0.9 11/5/2014)
    lcd.h 08-17-2014, but also with older versions.

    Is this a bug or am I doing something wrong?

    Kind regards.

     
  • Anobium

    Anobium - 2014-09-08

    Welcome.

    A nice posting as you told us your versions. A great help. :-)

    Let me try to help.

    String definition is correct - 'dim test as string'. This creates a string. A string is typically 16 bytes long. To make shorter or longer use 'dim test as string * 2' or 'dim test as string * 32' are examples not the * n where n is the length of the string.

    You assignment of values to a string needs to be changed. You are not using the string correctly. A string is essentially a type of array and you are addressing the elements of the string array but not using a valid method.

    To assign a string, use
    test = "AB"
    To assign a specific character to a specific position in the string, use the ASCII value (two examples below, write to a specific position in the string the values shown).
    test(1) = 65
    test(2) = 98

    So, if you want to test two strings you need to use two string definitions.

    ' I had a GLCD attached.... please change to a LCD.  :-)
    dim test1 as string
    dim test2 as string
    
    test1 = "string1"
    test2 = "string2"
    
    if test1 = test2 then
     GLCDDrawString(0,0,"Equal.")
    else
     GLCDDrawString(0,0,"!Equal.")
    end if
    

    Hope this helps.

     
    • Theo

      Theo - 2014-09-08

      Thank you for your support Anobium.

      I was trying to create some shorter code by changing your program: DS1307 with LCD demonstration.

      sub printDate
      select case DOW
      etc

      into something like this:

      weekday(1) = "Su"
      weekday(2) = "Mo"
      etc

      sub printDate
      print weekDay(DOW)
      print ". "

      It's clear to me now that string handling in GCbasic differs from other programming languages.


      So I started with a new approach

      Dim weekday as string * 15
      weekday = "SuMoTuWeThFrSa"

      Sub printDate
      print mid(weekday,DOW,2)
      print ". "

      But using mid(or left/right) wasn't a success because the compiler started looping.
      Maybe you can help me again.

      Kind regards.

       
  • Anobium

    Anobium - 2014-09-09

    May I ask that you post your complete code? I need to see the complete code.

    Thank you.

     
  • Theo

    Theo - 2014-09-09

    The program I am using is your program: DS1307 with LCD Demonstration.gcb
    These 2 lines, added by

    ;----- Variables

    dim weekday as string * 15
    weekday = "SuMoTuWeThFrSa"

    and these 2 lines and changes by

    sub printDate

    print mid(weekday,DOW,2)
    print ". "
    
    ;select case DOW                   ;day of the week
    ;  case 1:
    ;    print "Sun. "
    ;  case 2:
    ;    print "Mon. "
    ;  case 3:
    ;    print "Tue. "
    ;  case 4:
    ;    print "Wed. "
    ;  case 5:
    ;    print "Thu. "
    ;  case 6:
    ;    print "Fri. "
    ;  case 7:
    ;    print "Sat. "
    ;end select
    

    Started compiling; after 3 minutes this message.

    20:55:34 G@Stool-COMPILE/ASSEMBLE, processing D:\GCB@Syn\G@Stools\makeHEX.bat
    Source-File = D:\GCB@Syn_Demos\DS1307\DS1307 with LCD Demonstration.gcb

    Aborting due to runtime error 12 ("segmentation violation" signal) in gcbasic.bas::MESSAGE()

    176.0 Sec. <<< WARNINGs / ERRORs while compiling!

     
  • Anobium

    Anobium - 2014-09-09

    As Hugh would say 'that's not right'. We need to sort the root cause of the issue as the most simplest of program has the same error.

    A workaround add this code to your program in the meanwhile.

    #define mid midd
    Function Midd(SysInString(), SysCharStart, Optional SysCharCount = 255) As String
      'Empty input?
      If SysInString(0) = 0 Then
        Midd(0) = 0
        Exit Function
      End If
    
      'Starting position too low?
      If SysCharStart < 1 Then SysCharStart = 1
    
      'Starting position too high?
      If SysCharStart > SysInString(0) Then
        Midd(0) = 0
        Exit Function
      End If
    
      'Input length too high?
      SysCharStart -= 1
      SysStringTemp = SysInString(0) - SysCharStart 'Max number of characters
      If SysCharCount > SysStringTemp Then
        SysCharCount = SysStringTemp
      End If
    
      'Copy characters
      For SysStringTemp = 1 To SysCharCount
        Midd(SysStringTemp) = SysInString(SysCharStart + SysStringTemp)
      Next
      Midd(0) = SysCharCount
    End Function
    
     
  • Theo

    Theo - 2014-09-10

    Thanks for your fast response, compiling worked.

     
  • Anobium

    Anobium - 2014-09-10

    This will be fixed in the next release, so, please replace this workaround when you see the next release.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.