Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Correcting the code
#1
The code is intended to work as follows: when only the Enter key is pressed, the variable (H#) cannot be set to zero. However, when the zero (0) key is pressed, the variable (H#) is supposed to have a zero value. But if the entered number is 1,2,3,4,5,6 digits, it is to be confirmed with the Enter key. However, the 7-digit number is to be approved automatically.
To sum up, the entered numbers may have the following values: 0, 11, 232, 1254, 36547, 325478, 3254657.

H$ = "": B$ = ""
WHILE LEN(H$) < 7 AND B$ <> CHR$(13)
    B$ = ""
    WHILE B$ = "": B$ = INKEY$: _LIMIT 30: WEND
    IF B$ = CHR$(27) THEN 4200
    IF B$ = CHR$(32) THEN 8425
    IF B$ = CHR$(45) THEN 6171
    IF B$ = CHR$(43) THEN 6172
    IF B$ = CHR$(81) OR B$ = CHR$(113) THEN 6304
    IF B$ = CHR$(65) OR B$ = CHR$(97) THEN 6305
    IF B$ = CHR$(87) OR B$ = CHR$(119) THEN 6306
    IF B$ = CHR$(83) OR B$ = CHR$(115) THEN 6307
    IF B$ = CHR$(69) OR B$ = CHR$(101) THEN 6308
    IF B$ = CHR$(68) OR B$ = CHR$(100) THEN 6309
    IF B$ = CHR$(82) OR B$ = CHR$(114) THEN 6310
    IF B$ = CHR$(70) OR B$ = CHR$(102) THEN 6311
    IF B$ <> CHR$(13) THEN H$ = H$ + B$
WEND
H# = VAL(H$): PRINT H#

Thank you for your help - regards
Reply
#2
Code: (Select All)
H$ = "": B$ = ""
While Len(H$) < 7
    Cls
    Locate 1, 1: Print Spc(8);
    Locate 1, 1: Print H$

    B$ = ""
    While B$ = ""
        B$ = InKey$
        _Limit 30
    Wend
    If B$ = Chr$(27) Then End
    If B$ = "-" Then
        If H$ = "" Then H$ = "-" Else Beep
    End If
    If InStr("0123456789", B$) Then H$ = H$ + B$
    If B$ = Chr$(13) Then Exit While
    _Limit 30
Wend
Cls
H# = Val(H$): Print "H ="; H#

Also might not allow 0 as first digit but shouldn't matter when do Val.

I think this would be a whole lot easier with just Input "Enter your number ";h# then check it. Not sure if you want to allow floating decimal? h might be better as long integer as H!
b = b + ...
Reply
#3
All those GOTO statements make my teeth crunch up and hurt!  /cry!

Here, try this in a much, much, much, much simpler way:

Code: (Select All)
'The code is intended to work as follows:
'when only the Enter key is pressed, the variable (H#) cannot be set to zero.
'However, when the zero (0) key is pressed, the variable (H#) is supposed to have a zero value.
'But if the entered number is 1,2,3,4,5,6 digits, it is to be confirmed with the Enter key.
'However, the 7-digit number is to be approved automatically.
'To sum up, the entered numbers may have the following values: 0, 11, 232, 1254, 36547, 325478, 3254657.
'
Do
    Print "Input the correct value, by the rules!  =>";
    Do
        a$ = Input$(1)
        Select Case a$
            Case "0" To "7"
                If Len(num$) < 7 Then
                    Print a$;: num$ = num$ + a$
                Else
                    finished = -1 'the 7 digit sequence is to be approved automagically
                End If
                If Len(num$) = 1 And a$ = "0" Then finished = -1 'zero needs no confirmation
                'Is zero an unique case that resets the value to 0, no matter when it appears???
                'In other words, is "10" counted as 10, or does that 0 void it out and make it a 0 automatically?
                'The rules aren't the most specific on this point.
                'If a$ = "0" Then num$ = "0": finished = -1 'this rule makes 0 a pure "reset" value, if it's necessary
            Case Chr$(13) 'enter
                If Len(num$) > 1 Then finished = -1 'if less than 7 digits, enter confirms
        End Select
    Loop Until finished
    Print
    Print "Your number as entereed was: "; num$
    finished = 0: num$ = "" 'reset the variables
Loop 'forever and ever and ever... or until you hit the big red X to close the program.


Looks like a lot of code, but it's mainly just all comments.
Reply
#4
Thank You very much, it works, but a small correction is still needed. As I wrote earlier, after pressing only the Enter key, the H# variable cannot have the value zero (0). After entering the value zero (0) it is ok, after entering any number it is ok, but when I press only the Enter key it takes the value zero (0) and that is not good. I am asking for an amendment so that after pressing only the Enter key, the H# variable takes the value N#, i.e. H# = N#. The value of N# is calculated before the loop. If you fix it, it will be great. I removed unnecessary lines. Thank you.


H$ = "": B$ = ""
WHILE LEN(H$) < 7
        B$ = ""
    WHILE B$ = ""
        B$ = INKEY$
        _LIMIT 30
    WEND
    IF B$ = Chr$(27) THEN END
    IF B$ = "-" THEN
        IF H$ = "" THEN H$ = "-" ELSE BEEP
    ENDIF
    IF INSTR("0123456789", B$) THEN H$ = H$ + B$
  IF B$ = CHR$(13) THEN EXIT WHILE
    _LIMIT 30
WEND
H# = VAL(H$): PRINT H#
Reply
#5
(10-28-2023, 06:34 AM)Chris Wrote: Thank You very much, it works, but a small correction is still needed. As I wrote earlier, after pressing only the Enter key, the H# variable cannot have the value zero (0). After entering the value zero (0) it is ok, after entering any number it is ok, but when I press only the Enter key it takes the value zero (0) and that is not good. I am asking for an amendment so that after pressing only the Enter key, the H# variable takes the value N#, i.e. H# = N#. The value of N# is calculated before the loop. If you fix it, it will be great. I removed unnecessary lines. Thank you.


H$ = "": B$ = ""
WHILE LEN(H$) < 7
        B$ = ""
    WHILE B$ = ""
        B$ = INKEY$
        _LIMIT 30
    WEND
    IF B$ = Chr$(27) THEN END
    IF B$ = "-" THEN
        IF H$ = "" THEN H$ = "-" ELSE BEEP
    ENDIF
    IF INSTR("0123456789", B$) THEN H$ = H$ + B$
  IF B$ = CHR$(13) THEN EXIT WHILE
    _LIMIT 30
WEND
H# = VAL(H$): PRINT H#

This should do what you're asking for.
The only change made was in the 4th line from the bottom.

Code: (Select All)
H$ = "": B$ = ""
WHILE LEN(H$) < 7
        B$ = ""
    WHILE B$ = ""
        B$ = INKEY$
        _LIMIT 30
    WEND
    IF B$ = Chr$(27) THEN END
    IF B$ = "-" THEN
        IF H$ = "" THEN H$ = "-" ELSE BEEP
    ENDIF
    IF INSTR("0123456789", B$) THEN H$ = H$ + B$
  IF B$ = CHR$(13) THEN H$ = STR$(N#): EXIT WHILE
    _LIMIT 30
WEND
H# = VAL(H$): PRINT H#
Reply
#6
Thank You. 
When I press Enter, it now actually gives the correct result of H# = N#. But the rest of the code fell apart. And so: after entering the value zero (0), it should be H# = 0, but it is H# = N#. Also when entering numerical values from 1 to 999999 it gives H# = N#. Only in the case of a 7-digit number it gives the correct result. So one thing got better and the other thing got worse. But there is progress.
Reply
#7
I will add one more thing for clarification. Numbers 1, 2, 3, 4, 5, 6 digits are confirmed with the Enter key. However, 7-digit numbers are entered automatically without using the Enter key. This is how it's supposed to work.
Reply
#8
@Chris what do you want to happen when someone just hits the enter key, no number started?

If you don't want zero what do you want? an error message? and empty string? just ignore? You didn't say.

And really you might just code it yourself, in fact code each scenario for practice.

IF B$ = CHR$(13) AND H$ = "" THEN ' do what you want!

We got most of work done for you already, be a coder yourself Smile

Also YOU CAN make this a little fancier by adding a backspace button so a person can edit es number before it is "returned".

Oh I should warn if H$ = "" empty string then Val(H$) = 0 automatically unless you tell it to do something else for H$ = "" before VAL(H$)


Update I like all if's under one roof, it's more efficient:
Code: (Select All)
H$ = "": B$ = ""
While Len(H$) < 7
    Locate 1, 1: Print Spc(8)
    Locate 1, 1: Print H$
    B$ = ""
    While B$ = ""
        B$ = InKey$
        _Limit 30
    Wend
    If B$ = Chr$(27) Then
        End
    ElseIf B$ = "-" Then
        If H$ = "" Then H$ = "-" Else Beep
    ElseIf InStr("0123456789", B$) Then
        H$ = H$ + B$
    ElseIf B$ = Chr$(13) And H$ = "" Then
        Beep
    ElseIf B$ = Chr$(13) And H$ <> "" Then
        Exit While
    End If
    _Limit 30
Wend
H! = Val(H$): Print "H ="; H!
I chose BEEP when H$="" and B$ = enter
b = b + ...
Reply
#9
(10-28-2023, 06:34 AM)Chris Wrote: Thank You very much, it works, but a small correction is still needed. As I wrote earlier, after pressing only the Enter key, the H# variable cannot have the value zero (0). After entering the value zero (0) it is ok, after entering any number it is ok, but when I press only the Enter key it takes the value zero (0) and that is not good. I am asking for an amendment so that after pressing only the Enter key, the H# variable takes the value N#, i.e. H# = N#. The value of N# is calculated before the loop. If you fix it, it will be great. I removed unnecessary lines. Thank you.


H$ = "": B$ = ""
WHILE LEN(H$) < 7
        B$ = ""
    WHILE B$ = ""
        B$ = INKEY$
        _LIMIT 30
    WEND
    IF B$ = Chr$(27) THEN END
    IF B$ = "-" THEN
        IF H$ = "" THEN H$ = "-" ELSE BEEP
    ENDIF
    IF INSTR("0123456789", B$) THEN H$ = H$ + B$
  IF B$ = CHR$(13) THEN EXIT WHILE
    _LIMIT 30
WEND
H# = VAL(H$): PRINT H#

Ahhh..   I was misunderstanding how you set up the rules to begin with.  See if this isn't what you're looking for:

Code: (Select All)
Do
Print "Input the correct value, by the rules! =>";
Do
a$ = Input$(1)
Select Case a$
Case "0" To "7"
If Len(num$) < 7 Then
Print a$;: num$ = num$ + a$
Else
finished = -1 'the 7 digit sequence is to be approved automagically
End If
Case Chr$(8) 'backspace
If Len(num$) > 0 Then
L = Pos(0) - 1
num$ = Left$(num$, Len(num$) - 1)
Locate , L: Print " "; 'erase the character
Locate , L: 'move the cursor back a spot
End If
Case Chr$(13) 'enter
If Len(num$) Then finished = -1 'if less than 7 digits, enter confirms
Case Chr$(27) 'escape
System
End Select
Loop Until finished
Print
Print "Your number as entereed was: "; num$
finished = 0: num$ = "" 'reset the variables
Loop 'forever and ever and ever... or until you hit the big red X to close the program.

Note, I also added the ability to hit backspace to erase a character, in case you mess up on the entry.
Reply
#10
SMcNeill
Your code doesn't work.
I repeat once again the rules that must be met.

1. If I enter the value zero (0) and confirm with the Enter key, the H# variable should have the value zero (H# = 0)
2. If I enter numbers in the range 1-999999 and confirm with the Enter key, such values should be included in the H# variable.
3. If I enter a 7-digit number, it should be entered automatically without using the Enter key.
4. If I press ONLY the Enter key, the variable should take the value of the variable N#. So H# = N#

My predecessors may not have understood exactly what I meant.
You are experts in this field, so there should be no problem with such a short code
Reply




Users browsing this thread: 3 Guest(s)