none
If value is number or letter RRS feed

  • Question

  • This is a small portion of the code ive been working on. I followed a tutorial to an extent, but I'm trying to build on what I've already made.

    However the problem is the program will find tell me if the user input is even/odd EVEN if a letter is typed in (ex: the letter k shows up as even)

    How can I say

    If the value = "m" go to the menu

    If the value = numerical/a whole number find out if it is even or odd

    If the value = anything else, do nothing and wait for either a Number or M.

    I apologize in advance if the answer is something really simple/if this is a stupid question. I'm quite new to SB,

    but I find all the things you can make with this program fascinating.

    'even or odd calculator
    Evenodd1:
    TextWindow.Clear()
    TextWindow.WriteLine("Welcome to the Even-Odd determiner.")
    TextWindow.WriteLine("Type in M to go back to the menu")
    TextWindow.WriteLine("Type in a number:")
    numbr1 = TextWindow.ReadKey()
    If numbr1 = "m" Then
      Goto Choose1
    EndIf
    remainder = Math.Remainder(numbr1, 2)
    Eventodd1B:
    If (remainder = 0) Then
      TextWindow.WriteLine("That number is obviously even.")
      Else
        TextWindow.WriteLine("Seems odd to me!")
      EndIf


    Friday, October 11, 2013 5:09 AM

Answers

  • '-----------------------------------------------------------------------------------------------'
    ' http://social.msdn.microsoft.com/Forums/en-US/
    ' 868a0fe7-ba8a-4dd4-b527-432011154137/if-value-is-number-or-letter
    '-----------------------------------------------------------------------------------------------'
    
    '-----------------------------------------------------------------------------------------------'
    TextWindow.Title = "Even or Odd?"
    TextWindow.BackgroundColor = "Black"
    TextWindow.ForegroundColor = "Yellow"
    '-----------------------------------------------------------------------------------------------'
    Ask:
    
    TextWindow.WriteLine("Type in a digit...")
    CheckEvenOrOdd()
    TextWindow.Write("'"+ k + "' is ")
    
    If isNumber Then
      If isEven Then
        TextWindow.WriteLine("even.")
      Else
        TextWindow.WriteLine("odd.")
      EndIf
      
    Else
      TextWindow.WriteLine("not a digit!")
    EndIf
    
    If k = Text.GetCharacter(27) Then
      Program.End()
    EndIf
    
    TextWindow.WriteLine("")
    Goto Ask
    '-----------------------------------------------------------------------------------------------'
    Sub CheckEvenOrOdd
      
      k = TextWindow.ReadKey()
      
      If k+1 > 0 Then
        isNumber = "True"
      Else
        isNumber = "False"
      EndIf
      
      If Math.Remainder(k, 2) = 0 Then
        isEven = "True"
      Else
        isEven = "False"
      EndIf
      
    EndSub
    '-----------------------------------------------------------------------------------------------'

    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Friday, October 11, 2013 7:38 AM
    Answerer
  • Hello Sharmster,

    yes your right the solution is easy. ;)

    I fixed it for you in the codeblock below. I would suggest that you take a look at the SmallBasic Curriculum to learn something about subroutines. This will open many new ways for your programs.

    'even or odd calculator
    Start()
    Sub Start
      TextWindow.Clear()
      TextWindow.WriteLine("Welcome to the Even-Odd determiner.")
      TextWindow.WriteLine("Type in M to go back to the menu")
      TextWindow.WriteLine("Type in a number:")
      numbr1 = TextWindow.Read()
      CheckEntry()
    EndSub
    
    Sub CheckEntry
      If Text.ConvertToLowerCase(numbr1) = "m" Then 'With this code your program accepts capital letters, too.
        Start()
      ElseIf numbr1 = 0 or numbr1 = 1 or numbr1 = 2 or numbr1 = 3 or numbr1 = 4 or numbr1 = 5 or numbr1 = 6 or numbr1 = 7 or numbr1 = 8 or numbr1 = 9 Then
        'The code line above is a bit long, but it is more easy for you to understand. Otherwise you must youse arrays and for-loops. You can have a look on this later and update the code yourself. :)
        Even_Odd()
      Else
        Start()
        'the program will do nothing
      EndIf
    EndSub
    
    Sub Even_Odd
      remainder = Math.Remainder(numbr1, 2)
      'Eventodd1B: 'If you use this, you should call the subroutine with:  Even_Odd()
      If (remainder = 0) Then
        TextWindow.WriteLine("That number is obviously even.")
      Else
        TextWindow.WriteLine("Seems odd to me!")
      EndIf
      numbr1 = TextWindow.Read()
      CheckEntry()
    EndSub


    Mit freundlichen Grüßen Timo Greetings Timo


    Friday, October 11, 2013 9:29 AM

All replies

  • '-----------------------------------------------------------------------------------------------'
    ' http://social.msdn.microsoft.com/Forums/en-US/
    ' 868a0fe7-ba8a-4dd4-b527-432011154137/if-value-is-number-or-letter
    '-----------------------------------------------------------------------------------------------'
    
    '-----------------------------------------------------------------------------------------------'
    TextWindow.Title = "Even or Odd?"
    TextWindow.BackgroundColor = "Black"
    TextWindow.ForegroundColor = "Yellow"
    '-----------------------------------------------------------------------------------------------'
    Ask:
    
    TextWindow.WriteLine("Type in a digit...")
    CheckEvenOrOdd()
    TextWindow.Write("'"+ k + "' is ")
    
    If isNumber Then
      If isEven Then
        TextWindow.WriteLine("even.")
      Else
        TextWindow.WriteLine("odd.")
      EndIf
      
    Else
      TextWindow.WriteLine("not a digit!")
    EndIf
    
    If k = Text.GetCharacter(27) Then
      Program.End()
    EndIf
    
    TextWindow.WriteLine("")
    Goto Ask
    '-----------------------------------------------------------------------------------------------'
    Sub CheckEvenOrOdd
      
      k = TextWindow.ReadKey()
      
      If k+1 > 0 Then
        isNumber = "True"
      Else
        isNumber = "False"
      EndIf
      
      If Math.Remainder(k, 2) = 0 Then
        isEven = "True"
      Else
        isEven = "False"
      EndIf
      
    EndSub
    '-----------------------------------------------------------------------------------------------'

    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Friday, October 11, 2013 7:38 AM
    Answerer
  • Hello Sharmster,

    yes your right the solution is easy. ;)

    I fixed it for you in the codeblock below. I would suggest that you take a look at the SmallBasic Curriculum to learn something about subroutines. This will open many new ways for your programs.

    'even or odd calculator
    Start()
    Sub Start
      TextWindow.Clear()
      TextWindow.WriteLine("Welcome to the Even-Odd determiner.")
      TextWindow.WriteLine("Type in M to go back to the menu")
      TextWindow.WriteLine("Type in a number:")
      numbr1 = TextWindow.Read()
      CheckEntry()
    EndSub
    
    Sub CheckEntry
      If Text.ConvertToLowerCase(numbr1) = "m" Then 'With this code your program accepts capital letters, too.
        Start()
      ElseIf numbr1 = 0 or numbr1 = 1 or numbr1 = 2 or numbr1 = 3 or numbr1 = 4 or numbr1 = 5 or numbr1 = 6 or numbr1 = 7 or numbr1 = 8 or numbr1 = 9 Then
        'The code line above is a bit long, but it is more easy for you to understand. Otherwise you must youse arrays and for-loops. You can have a look on this later and update the code yourself. :)
        Even_Odd()
      Else
        Start()
        'the program will do nothing
      EndIf
    EndSub
    
    Sub Even_Odd
      remainder = Math.Remainder(numbr1, 2)
      'Eventodd1B: 'If you use this, you should call the subroutine with:  Even_Odd()
      If (remainder = 0) Then
        TextWindow.WriteLine("That number is obviously even.")
      Else
        TextWindow.WriteLine("Seems odd to me!")
      EndIf
      numbr1 = TextWindow.Read()
      CheckEntry()
    EndSub


    Mit freundlichen Grüßen Timo Greetings Timo


    Friday, October 11, 2013 9:29 AM