Author Topic: Strangest Error. Anyone know why?  (Read 4068 times)

0 Members and 1 Guest are viewing this topic.

Offline Jaze

  • Newbie
  • Posts: 86
    • View Profile
Strangest Error. Anyone know why?
« on: March 27, 2022, 12:54:18 am »
Code: QB64: [Select]
  1. invalidEntry:
  2. xPos = Center("h:mm")
  3. COLOR 30, 1: LOCATE 34, xPos: PRINT "H": LOCATE 34, xPos + 2: PRINT "MM": COLOR 14, 1: LOCATE 34, xPos + 1: PRINT ":"
  4. h$ = INPUT$(1): IF ASC(h$) < 48 OR ASC(h$) > 57 THEN GOTO invalidEntry
  5. COLOR 14, 1: LOCATE 34, xPos: PRINT h$
  6. m1$ = INPUT$(1): IF ASC(m1$) < 48 OR ASC(m1$) > 53 THEN GOTO invalidEntry
  7. COLOR 14, 1: LOCATE 34, xPos + 2: PRINT m1$
  8. m2$ = INPUT$(1): IF ASC(m2$) < 48 OR ASC(m2$) > 57 THEN GOTO invalidEntry
  9. COLOR 14, 1: LOCATE 34, xPos + 3: PRINT m2$
  10. searchMinutes = (VAL(h$) * 60) + VAL(m1$ + m2$)
  11. PRINT "searchMinutes: " + S$(searchMinutes): ll$ = P$
  12. a$ = "Is " + MinutesToHoursPassed$(searchMinutes) + " correct?": LOCATE 38, Center(a$): PRINT a$: w$ = INPUT$(1): IF UCASE$(w$) = "N" THEN GOTO invalidEntry
  13. PRINT "searchMinutes: " + S$(searchMinutes): ll$ = P$
  14.  
  15.  
  16.  
  17. FUNCTION MinutesToHoursPassed$ (minutes AS SINGLE)
  18.   'I'm not going to worry about days... yet
  19.   rtn$ = ""
  20.   hours = INT(minutes / 60)
  21.   minutes = Round(minutes - (hours * 60))
  22.   IF hours <> 0 THEN
  23.     rtn$ = S$(hours) + " hour"
  24.     IF hours <> 1 THEN rtn$ = rtn$ + "s"
  25.     IF minutes <> 0 THEN rtn$ = rtn$ + " and "
  26.   END IF
  27.   IF minutes <> 0 THEN
  28.     rtn$ = rtn$ + S$(minutes) + " minute"
  29.     IF minutes <> 1 THEN rtn$ = rtn$ + "s"
  30.   END IF
  31.   MinutesToHoursPassed$ = rtn$
  32.  
  33. FUNCTION Center (text$): Center = INT((80 - LEN(text$)) / 2): END FUNCTION
  34.  
  35. FUNCTION S$ (number): S$ = LTRIM$(STR$(number)): END FUNCTION
  36.  
  37.   pause$ = INPUT$(1)
  38.   IF pause$ = CHR$(27) THEN
  39.     '        SYSTEM
  40.     END
  41.   END IF
  42.   P$ = pause$

The print statement just before passing it to MinutesToHoursPassed$ reports the variable as intended.
The second print statement reports searchMinutes as 0 after using the function. I copied the variable and only sent the copy to MinutesToHoursPassed$ and that worked, but shouldn't it have worked without having to do that?

I mnust've checkled the spelling 50 times. I put the number in a seconed variable not used for the call and that worked. Just wondering what's going on

Marked as best answer by Jaze on March 27, 2022, 03:20:28 am

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Strangest Error. Anyone know why?
« Reply #1 on: March 27, 2022, 05:48:37 am »
You're return passing values via the Function, and you don't want to.

FUNCTION MinutesToHoursPassed$ (TEMPminutes AS SINGLE)
minutes = TEMPminutes


Don't use the reference variable in your function and you won't corrupt its value.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Strangest Error. Anyone know why?
« Reply #2 on: March 27, 2022, 02:57:30 pm »
And be sure to...

$INCLUDE Steve

...in your next program. I know this forum wouldn't FUNCTION very well without him.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/