Author Topic: help with exercise  (Read 1817 times)

0 Members and 1 Guest are viewing this topic.

Offline Kiara87

  • Forum Regular
  • Posts: 164
help with exercise
« on: December 15, 2020, 04:17:45 pm »
hi all i have an exercise from my book

exercise allows you to first write the code with goto and then write the same code with structured programming

I copy the two codes and the first works

when i copy the one without the goto with structured programming the program doesn't work

I leave the two codes to check if someone can help me


source with goto
Code: QB64: [Select]
  1. 10 lc$ = "abcdefghilmnopqrstuvwxyz"
  2. 20 uc$ = UCASE$(lc$)
  3. 30 nu$ = "0123456789-+/."
  4. 40 valido$ = uc$ + lc$ 'una stringa contenente tutti i caratteri validi
  5. 50 buffer$ = "" ' buffer per il nome
  6. 60 max% = 30 'numero massimo di caratteri
  7. 70 CLS: LOCATE 10, 1, 1: PRINT "inserisci il tuo nome > ";
  8. 80 lunghezza% = 0
  9. 90 tastopremuto$ = INKEY$
  10. 100 IF tastopremuto$ = "" THEN 90 'ripeti fino a quando l'utente inserisce un carattere
  11.  
  12. 110 IF tastopremuto$ = CHR$(13) THEN 230 'termina quando l'utente preme l'invio
  13. 120 IF tastopremuto$ <> CHR$(8) THEN 180 ''salta se l'utente non preme backspace
  14.  
  15. 130 IF lunghezza% = 0 THEN 90
  16. 140 PRINT CHR$(29); " "; CHR$(29);
  17. 150 lunghezza% = lunghezza - 1
  18. 160 buffer$ = LEFT$(buffer$, lunghezza%)
  19. 170 GOTO 90
  20.  
  21. 180 IF lunghezza% = max% OR INSTR(valido$, tastopremuto$) = 0 THEN 90
  22. 190 buffer$ = buffer$ + tastopremuto$
  23. 200 PRINT tastopremuto$;
  24. 210 lunghezza% = lunghezza% + 1
  25. 220 GOTO 90
  26. 230 LOCATE 20, 1: PRINT buffer$
  27. 240 END



structured source
Code: QB64: [Select]
  1. SUB ottieneinput (max%, buffer$) STATIC
  2.     SHARED valido$
  3.     lunghezza% = 0
  4.     DO
  5.         DO
  6.             tastopremuto$ = INKEY$
  7.         LOOP WHILE tastopremuto$ = "" 'ripete fino a quando l'utente inserisce un carattere
  8.  
  9.         IF tastopremuto$ = CHR$(8) THEN ' controlla se l'utente ha premuto backspace
  10.  
  11.             IF lunghezza% > 0 THEN
  12.                 lunghezza% = lunghezza% - 1
  13.                 PRINT CHR$(29); "  "; CHR$(29);
  14.                 buffer$ = LEFT$(buffer$, lunghezza%)
  15.             END IF
  16.         ELSE
  17.             IF lunghezza% < max% AND INSTR(valido$, tastopremuto$) THEN
  18.                 buffer$ = buffer$ + tastopremuto$
  19.                 PRINT tastopremuto$;: lunghezza% = lunghezza% + 1
  20.             END IF
  21.         END IF
  22.     LOOP UNTIL tastopremuto$ = CHR$(13) 'termina quando l'utente preme invio
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: help with exercise
« Reply #1 on: December 15, 2020, 04:42:01 pm »
Funny I put the 2nd code in IDE and nothing, no red lines, nothing...

Do you have demo code that calls up the SUB you made? That might get a response from red lines :-))

I cant read the Italian but it looks like a hand made INPUT$ routine that controls what letters and length of input, correct?

I dont think I've ever seen STATIC on same line as SUB definition and I dont think SHARED works in a SUB, not sure but... my first impressions...

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: help with exercise
« Reply #2 on: December 15, 2020, 04:52:35 pm »
Your SUB needs to know what valido$ is, it's thinking it's "" nothing.

And INSTR does NOT work right with nothing. <<< EDIT left out the NOT, yikes!

Still not right but now you can see something!
Code: QB64: [Select]
  1.  
  2. DIM SHARED valido$
  3. lc$ = "abcdefghilmnopqrstuvwxyz"
  4. uc$ = UCASE$(lc$)
  5. nu$ = "0123456789-+/."
  6. valido$ = uc$ + lc$
  7.  
  8.  
  9. ottieneinput 5, myinput$
  10. PRINT myinput
  11.  
  12. SUB ottieneinput (max%, buffer$)
  13.  
  14.     lunghezza% = 0
  15.     DO
  16.         DO
  17.             tastopremuto$ = INKEY$
  18.         LOOP WHILE tastopremuto$ = "" 'ripete fino a quando l'utente inserisce un carattere
  19.  
  20.         IF tastopremuto$ = CHR$(8) THEN ' controlla se l'utente ha premuto backspace
  21.  
  22.             IF lunghezza% > 0 THEN
  23.                 lunghezza% = lunghezza% - 1
  24.                 PRINT CHR$(29); "  "; CHR$(29);
  25.                 buffer$ = LEFT$(buffer$, lunghezza%)
  26.             END IF
  27.         ELSE
  28.             IF lunghezza% < max% AND INSTR(valido$, tastopremuto$) THEN
  29.                 buffer$ = buffer$ + tastopremuto$
  30.                 PRINT tastopremuto$;: lunghezza% = lunghezza% + 1
  31.             END IF
  32.         END IF
  33.     LOOP UNTIL tastopremuto$ = CHR$(13) 'termina quando l'utente preme invio
  34.  
  35.  
« Last Edit: December 15, 2020, 10:54:25 pm by bplus »

Offline Kiara87

  • Forum Regular
  • Posts: 164
Re: help with exercise
« Reply #3 on: December 15, 2020, 05:17:36 pm »
Funny I put the 2nd code in IDE and nothing, no red lines, nothing...

Do you have demo code that calls up the SUB you made? That might get a response from red lines :-))

I cant read the Italian but it looks like a hand made INPUT$ routine that controls what letters and length of input, correct?

I dont think I've ever seen STATIC on same line as SUB definition and I dont think SHARED works in a SUB, not sure but... my first impressions...

https://imgur.com/eqSIdPm

https://imgur.com/VU1MNzK

I don't know I'm copying from this book


MS-DOS QBasic. Guida del programmatore


the two programs should give the same output

to me the second program does not give output like the first
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline Kiara87

  • Forum Regular
  • Posts: 164
Re: help with exercise
« Reply #4 on: December 15, 2020, 05:32:32 pm »
Your SUB needs to know what valido$ is, it's thinking it's "" nothing.

And INSTR does work right with nothing.

Still not right but now you can see something!
Code: QB64: [Select]
  1.  
  2. DIM SHARED valido$
  3. lc$ = "abcdefghilmnopqrstuvwxyz"
  4. uc$ = UCASE$(lc$)
  5. nu$ = "0123456789-+/."
  6. valido$ = uc$ + lc$
  7.  
  8.  
  9. ottieneinput 5, myinput$
  10. PRINT myinput
  11.  
  12. SUB ottieneinput (max%, buffer$)
  13.  
  14.     lunghezza% = 0
  15.     DO
  16.         DO
  17.             tastopremuto$ = INKEY$
  18.         LOOP WHILE tastopremuto$ = "" 'ripete fino a quando l'utente inserisce un carattere
  19.  
  20.         IF tastopremuto$ = CHR$(8) THEN ' controlla se l'utente ha premuto backspace
  21.  
  22.             IF lunghezza% > 0 THEN
  23.                 lunghezza% = lunghezza% - 1
  24.                 PRINT CHR$(29); "  "; CHR$(29);
  25.                 buffer$ = LEFT$(buffer$, lunghezza%)
  26.             END IF
  27.         ELSE
  28.             IF lunghezza% < max% AND INSTR(valido$, tastopremuto$) THEN
  29.                 buffer$ = buffer$ + tastopremuto$
  30.                 PRINT tastopremuto$;: lunghezza% = lunghezza% + 1
  31.             END IF
  32.         END IF
  33.     LOOP UNTIL tastopremuto$ = CHR$(13) 'termina quando l'utente preme invio
  34.  
  35.  


this from a different output from the old code should give the same output

Code: QB64: [Select]
  1. 10 lc$ = "abcdefghilmnopqrstuvwxyz"
  2. 20 uc$ = UCASE$(lc$)
  3. 30 nu$ = "0123456789-+/."
  4. 40 valido$ = uc$ + lc$ 'una stringa contenente tutti i caratteri validi
  5. 50 buffer$ = "" ' buffer per il nome
  6. 60 max% = 30 'numero massimo di caratteri
  7. 70 CLS: LOCATE 10, 1, 1: PRINT "inserisci il tuo nome > ";
  8. 80 lunghezza% = 0
  9. 90 tastopremuto$ = INKEY$
  10. 100 IF tastopremuto$ = "" THEN 90 'ripeti fino a quando l'utente inserisce un carattere
  11.  
  12. 110 IF tastopremuto$ = CHR$(13) THEN 230 'termina quando l'utente preme l'invio
  13. 120 IF tastopremuto$ <> CHR$(8) THEN 180 ''salta se l'utente non preme backspace
  14.  
  15. 130 IF lunghezza% = 0 THEN 90
  16. 140 PRINT CHR$(29); " "; CHR$(29);
  17. 150 lunghezza% = lunghezza - 1
  18. 160 buffer$ = LEFT$(buffer$, lunghezza%)
  19. 170 GOTO 90
  20.  
  21. 180 IF lunghezza% = max% OR INSTR(valido$, tastopremuto$) = 0 THEN 90
  22. 190 buffer$ = buffer$ + tastopremuto$
  23. 200 PRINT tastopremuto$;
  24. 210 lunghezza% = lunghezza% + 1
  25. 220 GOTO 90
  26. 230 LOCATE 20, 1: PRINT buffer$
  27. 240 END
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline Kiara87

  • Forum Regular
  • Posts: 164
Re: help with exercise
« Reply #5 on: December 15, 2020, 05:45:04 pm »

thanks to bplus

i changed the variables and it works from same output

but now it needs to be solved when I click backspace which doesn't work well
in fact if you try to delete the text with the backspace key it doesn't work

Code: QB64: [Select]
  1. DIM SHARED valido$
  2. lc$ = "abcdefghilmnopqrstuvwxyz"
  3. uc$ = UCASE$(lc$)
  4. nu$ = "0123456789-+/."
  5. valido$ = uc$ + lc$
  6. max% = 30
  7.  
  8. LOCATE 10, 1, 1: PRINT "inserisci il tuo nome > ";
  9.  
  10. ottieneinput max%, buffer$
  11. LOCATE 20, 10: PRINT buffer$
  12.  
  13. SUB ottieneinput (max%, buffer$)
  14.  
  15.     lunghezza% = 0
  16.     DO
  17.         DO
  18.             tastopremuto$ = INKEY$
  19.         LOOP WHILE tastopremuto$ = "" 'ripete fino a quando l'utente inserisce un carattere
  20.  
  21.         IF tastopremuto$ = CHR$(8) THEN ' controlla se l'utente ha premuto backspace
  22.  
  23.             IF lunghezza% > 0 THEN
  24.                 lunghezza% = lunghezza% - 1
  25.                 PRINT CHR$(29); "  "; CHR$(29);
  26.                 buffer$ = LEFT$(buffer$, lunghezza%)
  27.             END IF
  28.         ELSE
  29.             IF lunghezza% < max% AND INSTR(valido$, tastopremuto$) THEN
  30.                 buffer$ = buffer$ + tastopremuto$
  31.                 PRINT tastopremuto$;: lunghezza% = lunghezza% + 1
  32.             END IF
  33.         END IF
  34.     LOOP UNTIL tastopremuto$ = CHR$(13) 'termina quando l'utente preme invio
  35.  
  36.  
  37.  
  38.  
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: help with exercise
« Reply #6 on: December 15, 2020, 06:35:44 pm »
This line defines the SUB and its parameters (variables for now):
Code: QB64: [Select]
  1. SUB ottieneinput (max%, buffer$)

IMPORTANT: You do not have to use the same variable names as you used defining the SUB, ie

In main code you don't have to say:
Code: QB64: [Select]
  1. ottieneinput max%, buffer$

You could say:
Code: QB64: [Select]
  1. ottieneinput charactersLimit%, myInputString$

Here is the SUB fixed perfecto! (Just kidding, millions of ways to write this.)
I got rid of the SHARED valido$ and made it part of the input to the SUB.
Code: QB64: [Select]
  1. ''DIM SHARED valido$   >>>>>>>>>>>>> there is another way to tell the SUB what valido$ =
  2. lc$ = "abcdefghijklmnopqrstuvwxyz" ' missing jk
  3. uc$ = UCASE$(lc$)
  4. nu$ = "0123456789-+/."
  5. valido$ = lc$ + uc$ + nu$ ' <<< lower case, uppercase and digits
  6. CharacterLimit% = 5 ' <<<<<<<<<<<<<<< limit 5
  7. anyString$ = "" 'nothing ' best to start with blank
  8. LOCATE 10, 1, 1: PRINT "Please enter up to 5 letters or digits > ";
  9. ottieneinput CharacterLimit%, anyString$, valido$
  10. LOCATE 20, 10: PRINT anyString$
  11.  
  12. SUB ottieneinput (max%, buffer$, Good$)
  13.     buffer$ = "" 'just in case it was something
  14.     column = POS(0) 'current column for cursor
  15.     row = CSRLIN 'current row for cursor
  16.     '   LOCATE 1, 1: PRINT max%, buffer$, Good$ ' <<< debugging
  17.     DO
  18.         DO
  19.             tastopremuto$ = INKEY$
  20.         LOOP WHILE tastopremuto$ = "" 'ripete fino a quando l'utlocate ente inserisce un carattere
  21.  
  22.         IF tastopremuto$ = CHR$(8) THEN ' controlla se l'utente ha premuto backspace
  23.             IF LEN(buffer$) > 1 THEN
  24.                 buffer$ = LEFT$(buffer$, LEN(buffer$) - 1)
  25.                 LOCATE row, column: PRINT buffer$
  26.             ELSEIF LEN(buffer$) = 1 THEN
  27.                 buffer$ = ""
  28.             ELSE
  29.                 BEEP ' it's already empty
  30.             END IF
  31.         ELSE ' << if good letter or digit add it onto buffer$
  32.             IF LEN(buffer$) < max% AND INSTR(Good$, tastopremuto$) THEN
  33.                 buffer$ = buffer$ + tastopremuto$
  34.             ELSE
  35.                 BEEP 'signal buffer$ is full
  36.             END IF
  37.         END IF
  38.         LOCATE row, column: PRINT SPACE$(max%);  'show what's going on blank out line
  39.         LOCATE row, column: PRINT buffer$; '             and rewrite it on screen
  40.     LOOP UNTIL tastopremuto$ = CHR$(13) 'termina quando l'utente preme invio
  41.  
« Last Edit: December 15, 2020, 06:46:30 pm by bplus »

Offline Kiara87

  • Forum Regular
  • Posts: 164
Re: help with exercise
« Reply #7 on: December 15, 2020, 06:47:31 pm »
I got rid of the shared and made valido$ part of the input.

This line defines the SUB and its parameters (variables for now):
Code: QB64: [Select]
  1. SUB ottieneinput (max%, buffer$)

IMPORTANT: You do not have to use the same variable names as you used defining the SUB, ie

In main code you don't have to say:
Code: QB64: [Select]
  1. ottieneinput max%, buffer$

You could say:
Code: QB64: [Select]
  1. ottieneinput charactersLimit%, myInputString$

Here is the SUB fixed perfecto! (Just kidding, millions of ways to write this.)
Code: QB64: [Select]
  1. ''DIM SHARED valido$   >>>>>>>>>>>>> there is another way to tell the SUB what valido$ =
  2. lc$ = "abcdefghijklmnopqrstuvwxyz" ' missing jk
  3. uc$ = UCASE$(lc$)
  4. nu$ = "0123456789-+/."
  5. valido$ = lc$ + uc$ + nu$ ' <<< lower case, uppercase and digits
  6. CharacterLimit% = 5 ' <<<<<<<<<<<<<<< limit 5
  7. anyString$ = "" 'nothing ' best to start with blank
  8. LOCATE 10, 1, 1: PRINT "Please enter up to 5 letters or digits > ";
  9. ottieneinput CharacterLimit%, anyString$, valido$
  10. LOCATE 20, 10: PRINT anyString$
  11.  
  12. SUB ottieneinput (max%, buffer$, Good$)
  13.     buffer$ = "" 'just in case it was something
  14.     column = POS(0) 'current column for cursor
  15.     row = CSRLIN 'current row for cursor
  16.     '   LOCATE 1, 1: PRINT max%, buffer$, Good$ ' <<< debugging
  17.     DO
  18.         DO
  19.             tastopremuto$ = INKEY$
  20.         LOOP WHILE tastopremuto$ = "" 'ripete fino a quando l'utlocate ente inserisce un carattere
  21.  
  22.         IF tastopremuto$ = CHR$(8) THEN ' controlla se l'utente ha premuto backspace
  23.             IF LEN(buffer$) > 1 THEN
  24.                 buffer$ = LEFT$(buffer$, LEN(buffer$) - 1)
  25.                 LOCATE row, column: PRINT buffer$
  26.             ELSEIF LEN(buffer$) = 1 THEN
  27.                 buffer$ = ""
  28.             ELSE
  29.                 BEEP ' it's already empty
  30.             END IF
  31.         ELSEIF ASC(tastopremuto$) = 13 THEN ' enter key press, time to go
  32.             EXIT SUB
  33.         ELSE ' << if good letter or digit add it onto buffer$
  34.             IF LEN(buffer$) < max% AND INSTR(Good$, tastopremuto$) THEN
  35.                 buffer$ = buffer$ + tastopremuto$
  36.             ELSE
  37.                 BEEP 'signal buffer$ is full
  38.             END IF
  39.         END IF
  40.         LOCATE row, column: PRINT SPACE$(max%);  'show what's going on blank out line
  41.         LOCATE row, column: PRINT buffer$; '             and rewrite it on screen
  42.     LOOP UNTIL tastopremuto$ = CHR$(13) 'termina quando l'utente preme invio
  43.  

thanks bplus
this also does the same job of the old code very well


I was inspired by how you created earlier


old language code
Code: QB64: [Select]
  1. 10 lc$ = "abcdefghilmnopqrstuvwxyz"
  2. 20 uc$ = UCASE$(lc$)
  3. 30 nu$ = "0123456789-+/."
  4. 40 valido$ = uc$ + lc$ 'una stringa contenente tutti i caratteri validi
  5. 50 buffer$ = "" ' buffer per il nome
  6. 60 max% = 30 'numero massimo di caratteri
  7. 70 CLS: LOCATE 10, 1, 1: PRINT "inserisci il tuo nome > ";
  8. 80 lunghezza% = 0
  9. 90 tastopremuto$ = INKEY$
  10. 100 IF tastopremuto$ = "" THEN 90 'ripeti fino a quando l'utente inserisce un carattere
  11.  
  12. 110 IF tastopremuto$ = CHR$(13) THEN 230 'termina quando l'utente preme l'invio
  13. 120 IF tastopremuto$ <> CHR$(8) THEN 180 ''salta se l'utente non preme backspace
  14.  
  15. 130 IF lunghezza% = 0 THEN 90
  16. 140 PRINT CHR$(29); " "; CHR$(29);
  17. 150 lunghezza% = lunghezza - 1
  18. 160 buffer$ = LEFT$(buffer$, lunghezza%)
  19. 170 GOTO 90
  20.  
  21. 180 IF lunghezza% = max% OR INSTR(valido$, tastopremuto$) = 0 THEN 90
  22. 190 buffer$ = buffer$ + tastopremuto$
  23. 200 PRINT tastopremuto$;
  24. 210 lunghezza% = lunghezza% + 1
  25. 220 GOTO 90
  26. 230 LOCATE 20, 1: PRINT buffer$
  27. 240 END
  28.  


more modern language
Code: QB64: [Select]
  1. DIM SHARED valido$
  2. lc$ = "abcdefghilmnopqrstuvwxyz"
  3. uc$ = UCASE$(lc$)
  4. nu$ = "0123456789-+/."
  5. valido$ = uc$ + lc$
  6. max% = 30
  7.  
  8. LOCATE 10, 1, 1: PRINT "inserisci il tuo nome > ";
  9.  
  10. ottieneinput max%, buffer$
  11. LOCATE 20, 10: PRINT buffer$
  12.  
  13. SUB ottieneinput (max%, buffer$)
  14.  
  15.     lunghezza% = 0
  16.     DO
  17.         DO
  18.             tastopremuto$ = INKEY$
  19.         LOOP WHILE tastopremuto$ = "" 'ripete fino a quando l'utente inserisce un carattere
  20.  
  21.         IF tastopremuto$ = CHR$(8) THEN ' controlla se l'utente ha premuto backspace
  22.  
  23.             IF lunghezza% > 0 THEN
  24.                 lunghezza% = lunghezza% - 1
  25.                 PRINT CHR$(29); " "; CHR$(29);
  26.                 buffer$ = LEFT$(buffer$, lunghezza%)
  27.             END IF
  28.         ELSE
  29.             IF lunghezza% < max% AND INSTR(valido$, tastopremuto$) THEN
  30.                 buffer$ = buffer$ + tastopremuto$
  31.                 PRINT tastopremuto$;: lunghezza% = lunghezza% + 1
  32.             END IF
  33.         END IF
  34.     LOOP UNTIL tastopremuto$ = CHR$(13) 'termina quando l'utente preme invio
  35.  

this shows how to avoid goto thanks for your help
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: help with exercise
« Reply #8 on: December 15, 2020, 06:54:05 pm »
@Kiara87   your alphabet is missing jk but looks like you've got it working. :)

Offline Kiara87

  • Forum Regular
  • Posts: 164
Re: help with exercise
« Reply #9 on: December 15, 2020, 07:03:36 pm »
@Kiara87   your alphabet is missing jk but looks like you've got it working. :)

Code: QB64: [Select]
  1. DIM SHARED valido$
  2. lc$ = "abcdefghijklmnopqrstuvwxyz"
  3. uc$ = UCASE$(lc$)
  4. nu$ = "0123456789-+/."
  5. valido$ = uc$ + lc$
  6. max% = 30
  7.  
  8. LOCATE 10, 1, 1: PRINT "inserisci il tuo nome > ";
  9.  
  10. ottieneinput max%, buffer$
  11. LOCATE 20, 10: PRINT buffer$
  12.  
  13. SUB ottieneinput (max%, buffer$)
  14.  
  15.     lunghezza% = 0
  16.     DO
  17.         DO
  18.             tastopremuto$ = INKEY$
  19.         LOOP WHILE tastopremuto$ = "" 'ripete fino a quando l'utente inserisce un carattere
  20.  
  21.         IF tastopremuto$ = CHR$(8) THEN ' controlla se l'utente ha premuto backspace
  22.  
  23.             IF lunghezza% > 0 THEN
  24.                 lunghezza% = lunghezza% - 1
  25.                 PRINT CHR$(29); " "; CHR$(29);
  26.                 buffer$ = LEFT$(buffer$, lunghezza%)
  27.             END IF
  28.         ELSE
  29.             IF lunghezza% < max% AND INSTR(valido$, tastopremuto$) THEN
  30.                 buffer$ = buffer$ + tastopremuto$
  31.                 PRINT tastopremuto$;: lunghezza% = lunghezza% + 1
  32.             END IF
  33.         END IF
  34.     LOOP UNTIL tastopremuto$ = CHR$(13) 'termina quando l'utente preme invio
  35.  
« Last Edit: December 15, 2020, 07:09:31 pm by Kiara87 »
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
Re: help with exercise
« Reply #10 on: December 16, 2020, 05:11:41 am »
Ciao Kiara87

simpatico esercizio, ma sul mio pc i conti non tornano

il primo esempio quello spaghetti code con GOTO funziona bene
ecco la sua analisi di flusso
  [ You are not allowed to view this attachment ]  

il secondo programma NON è completo, manca del main
ecco la sua analisi
  [ You are not allowed to view this attachment ]  
ecco qui è integrata e dovrebbe funzionare
Code: QB64: [Select]
  1. ' area di inizializzazione dei dati / area of initializing data
  2. 10 lc$ = "abcdefghilmnopqrstuvwxyz"
  3. 20 uc$ = UCASE$(lc$)
  4. 30 nu$ = "0123456789-+/."
  5. 40 valido$ = uc$ + lc$ 'una stringa contenente tutti i caratteri validi
  6. 50 buffer$ = "" ' buffer per il nome
  7. 60 max% = 30 'numero massimo di caratteri
  8. 70 CLS: LOCATE 10, 1, 1: PRINT "inserisci il tuo nome > ";
  9.  
  10. ottieneinput max%, buffer$ ' chiama o salta alla SUB e poi torna qui
  11. END ' termina il programma
  12.  
  13. '<----------------------------this is the main of program that lacks into second code that you posted ----------->
  14.  
  15.  
  16. ' area SUB / FUNCTION---------------
  17. SUB ottieneinput (max%, buffer$) STATIC
  18.     SHARED valido$
  19.     lunghezza% = 0
  20.     DO
  21.         DO
  22.             tastopremuto$ = INKEY$
  23.         LOOP WHILE tastopremuto$ = "" 'ripete fino a quando l'utente inserisce un carattere
  24.  
  25.         IF tastopremuto$ = CHR$(8) THEN ' controlla se l'utente ha premuto backspace
  26.  
  27.             IF lunghezza% > 0 THEN
  28.                 lunghezza% = lunghezza% - 1
  29.                 PRINT CHR$(29); "  "; CHR$(29);
  30.                 buffer$ = LEFT$(buffer$, lunghezza%)
  31.             END IF
  32.         ELSE
  33.             IF lunghezza% < max% AND INSTR(valido$, tastopremuto$) THEN
  34.                 buffer$ = buffer$ + tastopremuto$
  35.                 PRINT tastopremuto$;: lunghezza% = lunghezza% + 1
  36.             END IF
  37.         END IF
  38.     LOOP UNTIL tastopremuto$ = CHR$(13) 'termina quando l'utente preme invio
  39.  
ma oltre al difetto di mancare JK dall'alfabeto (prova a scrivere judo e hacker) come ha detto Bplus
ma prova a cancellare più di una lettera scritta... esso ti cancella solo la prima.
Bisogna capire dov'è il problema...

Ti consiglio di seguire questo thread per SHARED
https://www.qb64.org/forum/index.php?topic=3367.0
Saluti
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
Re: help with exercise
« Reply #11 on: December 16, 2020, 09:59:46 am »
riguardo al bug del backspace esso è dovuto alla parte di output del programma e non ai calcoli interni
se cancelli la linea
Code: QB64: [Select]
  1.  PRINT CHR$(29); "  "; CHR$(29);
e prima di
Code: QB64: [Select]
  1.       lunghezza% = lunghezza% - 1
inserisci
Code: QB64: [Select]
  1. LOCATE 10, 25: PRINT SPACE$(LEN(buffer$));
mentre dopo
Code: QB64: [Select]
  1.   buffer$ = LEFT$(buffer$, lunghezza%)
aggiungi
Code: QB64: [Select]
  1.    LOCATE 10, 25: PRINT buffer$;
e l'errore di output è sistemato... ma non so perchè nella versione spaghetticode questa linea di codice
Code: QB64: [Select]
  1.  PRINT CHR$(29); "  "; CHR$(29);
funziona mentre in quella modulare no!

English version:
About the bug of backspace it is in the output part of the program and not in the part of internal calculation/computation,
if you cancel the line of code
Code: QB64: [Select]
  1.  PRINT CHR$(29); "  "; CHR$(29);
and before 
Code: QB64: [Select]
  1.       lunghezza% = lunghezza% - 1
  you insert 
Code: QB64: [Select]
  1. LOCATE 10, 25: PRINT SPACE$(LEN(buffer$));
and after
 
Code: QB64: [Select]
  1.   buffer$ = LEFT$(buffer$, lunghezza%)
you add
Code: QB64: [Select]
  1.    LOCATE 10, 25: PRINT buffer$;
the output part is fixed.... but I dunno why in the spaghetti code version this line of code
Code: QB64: [Select]
  1.  PRINT CHR$(29); "  "; CHR$(29);
works while in the modular one it doesn't!

PS Hey don't you hurt all these alternating white, grey, black rows ?

Codice intero /whole code
Code: QB64: [Select]
  1. ' area di inizializzazione dei dati / area of initializing data
  2. 10 lc$ = "abcdefghilmnopqrstuvwxyz"
  3. 20 uc$ = UCASE$(lc$)
  4. 30 nu$ = "0123456789-+/."
  5. 40 valido$ = uc$ + lc$ 'una stringa contenente tutti i caratteri validi
  6. 50 buffer$ = "" ' buffer per il nome
  7. 60 max% = 30 'numero massimo di caratteri
  8. 70 CLS: LOCATE 10, 1, 1: PRINT "inserisci il tuo nome > ";
  9. ottieneinput max%, buffer$ ' chiama o salta alla SUB e poi torna qui
  10. END ' termina il programma
  11.  
  12. '<----------------------------this is the main of program that lacks into second code that you posted ----------->
  13.  
  14.  
  15. ' area SUB / FUNCTION---------------
  16. SUB ottieneinput (max%, buffer$) STATIC
  17.     SHARED valido$
  18.     lunghezza% = 0
  19.     DO
  20.         DO
  21.             tastopremuto$ = INKEY$
  22.         LOOP WHILE tastopremuto$ = "" 'ripete fino a quando l'utente inserisce un carattere
  23.  
  24.         IF tastopremuto$ = CHR$(8) THEN ' controlla se l'utente ha premuto backspace
  25.  
  26.             IF lunghezza% > 0 THEN
  27.                 LOCATE 10, 25: PRINT SPACE$(LEN(buffer$));
  28.                 lunghezza% = lunghezza% - 1
  29.                 buffer$ = LEFT$(buffer$, lunghezza%)
  30.                 LOCATE 10, 25: PRINT buffer$;
  31.             END IF
  32.         ELSE
  33.             IF lunghezza% < max% AND INSTR(valido$, tastopremuto$) THEN
  34.                 buffer$ = buffer$ + tastopremuto$
  35.                 PRINT tastopremuto$;: lunghezza% = lunghezza% + 1
  36.             END IF
  37.         END IF
  38.     LOOP UNTIL tastopremuto$ = CHR$(13) 'termina quando l'utente preme invio
  39.  
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: help with exercise
« Reply #12 on: December 16, 2020, 12:42:53 pm »
I dont like STATIC for the SUB, it is not needed here, you only use the SUB once and it seems dangerous to preserve values as you are likely to assume they are cleared with each call to SUB. One of SUBs strengths is independence to main module, if you want to preserve values use GOSUB.

I don't know, maybe it's just programming style, I can't imagine anyone in habit of using STATIC for SUBs.
I am sure there is a case for using it, maybe with recursion?

So SHARED can be used in SUB, somehow I learned it couldn't maybe it was with OPTION _EXPLICIT, the variable has to be declared in main to be able to use SHARED in SUB, something like that.

Offline Kiara87

  • Forum Regular
  • Posts: 164
Re: help with exercise
« Reply #13 on: December 16, 2020, 05:43:23 pm »
riguardo al bug del backspace esso è dovuto alla parte di output del programma e non ai calcoli interni
se cancelli la linea
Code: QB64: [Select]
  1.  PRINT CHR$(29); "  "; CHR$(29);
e prima di
Code: QB64: [Select]
  1.       lunghezza% = lunghezza% - 1
inserisci
Code: QB64: [Select]
  1. LOCATE 10, 25: PRINT SPACE$(LEN(buffer$));
mentre dopo
Code: QB64: [Select]
  1.   buffer$ = LEFT$(buffer$, lunghezza%)
aggiungi
Code: QB64: [Select]
  1.    LOCATE 10, 25: PRINT buffer$;
e l'errore di output è sistemato... ma non so perchè nella versione spaghetticode questa linea di codice
Code: QB64: [Select]
  1.  PRINT CHR$(29); "  "; CHR$(29);
funziona mentre in quella modulare no!

English version:
About the bug of backspace it is in the output part of the program and not in the part of internal calculation/computation,
if you cancel the line of code
Code: QB64: [Select]
  1.  PRINT CHR$(29); "  "; CHR$(29);
and before 
Code: QB64: [Select]
  1.       lunghezza% = lunghezza% - 1
  you insert 
Code: QB64: [Select]
  1. LOCATE 10, 25: PRINT SPACE$(LEN(buffer$));
and after
 
Code: QB64: [Select]
  1.   buffer$ = LEFT$(buffer$, lunghezza%)
you add
Code: QB64: [Select]
  1.    LOCATE 10, 25: PRINT buffer$;
the output part is fixed.... but I dunno why in the spaghetti code version this line of code
Code: QB64: [Select]
  1.  PRINT CHR$(29); "  "; CHR$(29);
works while in the modular one it doesn't!

PS Hey don't you hurt all these alternating white, grey, black rows ?

Codice intero /whole code
Code: QB64: [Select]
  1. ' area di inizializzazione dei dati / area of initializing data
  2. 10 lc$ = "abcdefghilmnopqrstuvwxyz"
  3. 20 uc$ = UCASE$(lc$)
  4. 30 nu$ = "0123456789-+/."
  5. 40 valido$ = uc$ + lc$ 'una stringa contenente tutti i caratteri validi
  6. 50 buffer$ = "" ' buffer per il nome
  7. 60 max% = 30 'numero massimo di caratteri
  8. 70 CLS: LOCATE 10, 1, 1: PRINT "inserisci il tuo nome > ";
  9. ottieneinput max%, buffer$ ' chiama o salta alla SUB e poi torna qui
  10. END ' termina il programma
  11.  
  12. '<----------------------------this is the main of program that lacks into second code that you posted ----------->
  13.  
  14.  
  15. ' area SUB / FUNCTION---------------
  16. SUB ottieneinput (max%, buffer$) STATIC
  17.     SHARED valido$
  18.     lunghezza% = 0
  19.     DO
  20.         DO
  21.             tastopremuto$ = INKEY$
  22.         LOOP WHILE tastopremuto$ = "" 'ripete fino a quando l'utente inserisce un carattere
  23.  
  24.         IF tastopremuto$ = CHR$(8) THEN ' controlla se l'utente ha premuto backspace
  25.  
  26.             IF lunghezza% > 0 THEN
  27.                 LOCATE 10, 25: PRINT SPACE$(LEN(buffer$));
  28.                 lunghezza% = lunghezza% - 1
  29.                 buffer$ = LEFT$(buffer$, lunghezza%)
  30.                 LOCATE 10, 25: PRINT buffer$;
  31.             END IF
  32.         ELSE
  33.             IF lunghezza% < max% AND INSTR(valido$, tastopremuto$) THEN
  34.                 buffer$ = buffer$ + tastopremuto$
  35.                 PRINT tastopremuto$;: lunghezza% = lunghezza% + 1
  36.             END IF
  37.         END IF
  38.     LOOP UNTIL tastopremuto$ = CHR$(13) 'termina quando l'utente preme invio
  39.  

ciao @TempodiBasic
il programma funziona
che l'avevo ripostato più sotto nel post
che sarebbe questo

Code: QB64: [Select]
  1. DIM SHARED valido$
  2. lc$ = "abcdefghijklmnopqrstuvwxyz"
  3. uc$ = UCASE$(lc$)
  4. nu$ = "0123456789-+/."
  5. valido$ = uc$ + lc$
  6. max% = 30
  7.  
  8. LOCATE 10, 1, 1: PRINT "inserisci il tuo nome > ";
  9.  
  10. ottieneinput max%, buffer$
  11. LOCATE 20, 10: PRINT buffer$
  12.  
  13. SUB ottieneinput (max%, buffer$)
  14.  
  15.     lunghezza% = 0
  16.     DO
  17.         DO
  18.             tastopremuto$ = INKEY$
  19.         LOOP WHILE tastopremuto$ = "" 'ripete fino a quando l'utente inserisce un carattere
  20.  
  21.         IF tastopremuto$ = CHR$(8) THEN ' controlla se l'utente ha premuto backspace
  22.  
  23.             IF lunghezza% > 0 THEN
  24.                 lunghezza% = lunghezza% - 1
  25.                 PRINT CHR$(29); " "; CHR$(29);
  26.                 buffer$ = LEFT$(buffer$, lunghezza%)
  27.             END IF
  28.         ELSE
  29.             IF lunghezza% < max% AND INSTR(valido$, tastopremuto$) THEN
  30.                 buffer$ = buffer$ + tastopremuto$
  31.                 PRINT tastopremuto$;: lunghezza% = lunghezza% + 1
  32.             END IF
  33.         END IF
  34.     LOOP UNTIL tastopremuto$ = CHR$(13) 'termina quando l'utente preme invio
  35.  

ho corretto anche il problema che cancellava solamente un carattere
il problema stava qui
PRINT CHR$(29); " "; CHR$(29);
un errore nella battitura della tastiera avevo messo due spazi ecco perche nel codice spaghetti funzionava e nel nuovo codice no
perche nel codice spaghetti avevo messo giusto
PRINT CHR$(29); " "; CHR$(29);
invece sul altro codice ho messo doppio spazio PRINT CHR$(29); "  "; CHR$(29);

PRINT CHR$(29); " "; CHR$(29);
PRINT CHR$(29); "  "; CHR$(29);
poi per sbaglio mentre cancellavo alcuna parte del codice che ho spagliato avevo rimosso le due lettere j e k
ma dopo ho corretto
grazie per aver segnalato l'errore
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
Re: help with exercise
« Reply #14 on: December 16, 2020, 07:58:18 pm »
@Kiara87
Good Job!

per esercizio potresti spezzettare il codice spaghetti in diversi blocchi SUB ognuno per una azione compiuta, in modo da allenarti a tenere separate per quanto è possibile le funzioni i Input dal calcolo interno e dall'output.
per esempio
Quote


' main

inizialize
AskInput
Loop
     GetInput
     IF... BackSpace
     IF .....AddNewCharacter
     IF... END
     AdjournOutput

SUB inizialize

END SUB

SUB GetInput

End SUB

SUB AskInput

END SUB

SUB BackSpace

END SUB

SUB AddNewCharacter

END SUB

SUB AdjournOutput

END SUB


for further exercise you can chunk down the original spaghetti code into different SUBs, each for a specific action (input, output, internal calculation)
Programming isn't difficult, only it's  consuming time and coffee