none
Number digits program RRS feed

  • Question

  • Hi,

    Create a program that asks the user to enter a number from 1 to 5000. The program will then separate each digit using MODULUS and show each separated digit on the screen. If no digit was entered, then the program should say "None", otherwise it should display the digit it has separated.

    The program should add the detected digits together and display the answer. 

    Here is the problem: if i placed a number less that 4 digits, it moves each digit to the next digit and so on and i am also having trouble placing the word "none"  in the labels. Appreciate your help.

    Dim digit1 As Integer
            Dim digit2 As Integer
            Dim digit3 As Integer
            Dim digit4 As Integer
            Dim inputnumber As Integer
            inputnumber = txtnumber.Text

            digit1 = (inputnumber \ 1000) Mod 10
            digit2 = (inputnumber \ 100) Mod 10
            digit3 = (inputnumber \ 10) Mod 10
            digit4 = (inputnumber \ 1) Mod 10

            lbldigit1.Text = digit1
            lbldigit2.Text = digit2
            lbldigit3.Text = digit3
            lbldigit4.Text = digit4

    If digit2 = Nothing Then
                lbldigit2.Text = "None"
            End If
            If digit3 = Nothing Then
                lbldigit3.Text = "None"
            End If
            If digit4 = Nothing Then
                lbldigit4.Text = " None"
            End If

            Dim r As Integer

            Dim sum As Integer

            While (inputnumber <> 0)
                r = inputnumber Mod 10
                sum = sum + r
                inputnumber = inputnumber \ 10
            End While

            lblsumtotal.Text = sum

        End Sub

        Private Sub txtnumber_TextChanged(sender As Object, e As EventArgs) Handles txtnumber.TextChanged
            Dim tb As TextBox = DirectCast(sender, TextBox)
            Dim v As Integer = 0
            If Integer.TryParse(tb.Text, v) Then
                If v < 1 Or v > 5000 Then
                    tb.Clear()
                End If
            Else
                tb.Clear()
            End If

    Wednesday, May 23, 2018 11:40 PM

Answers

  • Hi Jazee123,

    Let's step back and review this part of the code:

    Dim digit1 As Integer
    Dim digit2 As Integer
    Dim digit3 As Integer
    Dim digit4 As Integer
    Dim inputnumber As Integer
    inputnumber = txtnumber.Text
    
    digit1 = (inputnumber \ 1000) Mod 10
    digit2 = (inputnumber \ 100) Mod 10
    digit3 = (inputnumber \ 10) Mod 10
    digit4 = (inputnumber \ 1) Mod 10

    In order to arrive at the value of each digit, you are dividing the inputnumber by an appropriate amount.  This is OK, and gives you the correct number, but it does require ensuring that the inputnumber is large enough for the division.

    One option would be to test the inputnumber to see if it is greater than or equal to the divisor.  If so, calculate the digit value and display the result in the appropriate label.  If it is not greater or equal than the divisor then you can set the label text to a different value.

    Another option would be to consider that a string is also an array of characters.  When thinking this way, you can consider the length of the string as the count of characters and access each available character by its index in the string.  This gives you a single digit to work with and the index in the string tells you which label should hold the output.  If the string is too short to provide a digit, then the output is "none".

    If you adjust the code above to suit one of these two options, it should be pretty easy to get the correct output in each label.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Jazee123 Friday, June 15, 2018 12:37 AM
    Thursday, May 24, 2018 7:11 PM
    Moderator

All replies

  • Hi

    The only thing I needed to do to get your code to do what you say you want, was to allow for text to numeric  and  numeric to string.

    For example, the input in the textbox is a string and before you can do anything math/numerical with it it needs to be changed to a valid numerical type (I used Integer).

    inputnumber = CInt(txtnumber.Text)

    And likewise, to display a numeric type in a Label, it needs to be changed to a string.

    lbldigit1.Text = digit1.ToString
    repeated this convention throughout and it seems to produce the requested result. You could simplify your code to limit the input as well, but it does fine as it is.


    Regards Les, Livingston, Scotland



    • Edited by leshay Thursday, May 24, 2018 12:48 AM
    Thursday, May 24, 2018 12:46 AM
  • Try placing all of your code in a Code Block. Not just a part of it. Nobody can tell what subs your code is in or how they are called except a text changed event.

    And as leshay mentions you are using Object Oriented Programming or OOP. A string is that type of Object just as an integer is that type of Object. You need to convert objects from one type to another for use unless narrowing of an object, integer to decimal perhaps, may work. If you place Option Strict On at the top of your code window it will warn you where errors are not performing conversions of types from one to the other.

    Your Integer.TryParse(tb.Text, v) could just be Integer.TryParse(txtnumber.Text, v) with no direct cast necessary although it works of course.


    La vida loca

    Thursday, May 24, 2018 3:50 AM
  • Your problem getting None to show up is...

    If digit2 = Nothing Then

    Once you declare digiit2 as an Integer (a value type) it immediately has a value of 0. Odd as it may seem, zero is not Nothing. Perhaps:

    If digit2 = 0 Then


    Mary

    Thursday, May 24, 2018 4:45 AM
  • Odd as it may seem, zero is not Nothing.

    For a value variable (such as digit2) Nothing is equal to the default initialization value of the variable, which in this case is zero.  So this test behaves as expected.

    Thursday, May 24, 2018 7:06 AM
  • Odd as it may seem, zero is not Nothing


    Mary

     

    In addition to Acamar, = is not the same in VB as for instance in C#

    = has in VB two meanings

    1. Comparing to equal content (watch what Acamar wrote about defaults)
    2. Setting something 

    What you describe is the Is keyword in VB.

    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/is-operator


    Success
    Cor



    Thursday, May 24, 2018 10:43 AM
  • Jazee,

    Your problem is typical to investigate using Debug. I assume you don't know how that goes. 

    Click on the line in the editor before the code (it has to be an expression). 

    Then click on Start or F5 

    The program stops on the point it reaches the dot. Then you can step through your code using the F10 or F11 


    Success
    Cor

    Thursday, May 24, 2018 10:53 AM
  • Hi, 

    Appreciate all the suggestions and the tip on the debug. I think the problem is with my mod expression. 

    The code works as long as it is a4 digit number. l=Let's say my input number is 1234.

    digit1 will show 1, digit2 will show 2,digit3 will show 3 and so on. When I say 123, then digit1 will be 0, digit2 will be 1 digit 3 will be 2 and digit4 is 3. Where it should be:

    digit1:  1

    digit2: 2

    digit3: 3

    digit4: none

    I can modify the mod expression to show the word "none" but will affect the other digits. like this will work for 1 digit input only. 

    digit1 = (inputnumber\ 1) Mod 10

      If digit2 = Nothing Then
                lbldigit2.Text = "none"

            End If
            If digit3 = Nothing Then
                lbldigit3.Text = "none

    and so on...."

    Appreciate all your help.




    • Edited by Jazee123 Thursday, May 24, 2018 6:03 PM
    Thursday, May 24, 2018 5:59 PM
  • Hi

    Sorry, but your post is just a jumbled mess - please edit to make it readable.


    Regards Les, Livingston, Scotland

    Thursday, May 24, 2018 6:04 PM
  • Hi Jazee123,

    Let's step back and review this part of the code:

    Dim digit1 As Integer
    Dim digit2 As Integer
    Dim digit3 As Integer
    Dim digit4 As Integer
    Dim inputnumber As Integer
    inputnumber = txtnumber.Text
    
    digit1 = (inputnumber \ 1000) Mod 10
    digit2 = (inputnumber \ 100) Mod 10
    digit3 = (inputnumber \ 10) Mod 10
    digit4 = (inputnumber \ 1) Mod 10

    In order to arrive at the value of each digit, you are dividing the inputnumber by an appropriate amount.  This is OK, and gives you the correct number, but it does require ensuring that the inputnumber is large enough for the division.

    One option would be to test the inputnumber to see if it is greater than or equal to the divisor.  If so, calculate the digit value and display the result in the appropriate label.  If it is not greater or equal than the divisor then you can set the label text to a different value.

    Another option would be to consider that a string is also an array of characters.  When thinking this way, you can consider the length of the string as the count of characters and access each available character by its index in the string.  This gives you a single digit to work with and the index in the string tells you which label should hold the output.  If the string is too short to provide a digit, then the output is "none".

    If you adjust the code above to suit one of these two options, it should be pretty easy to get the correct output in each label.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Jazee123 Friday, June 15, 2018 12:37 AM
    Thursday, May 24, 2018 7:11 PM
    Moderator
  • I got the codes to work, appreciate all the suggestions and tips. I had to declare the digits as strings and edit the mod expression based on the number of digits inputted, using if and then conditions and it all worked well. Thanks to everyone!!!
    Monday, May 28, 2018 6:33 PM
  • I got the codes to work, appreciate all the suggestions and tips.
    Please select one or more of the above answers that assisted you with finding a solution and mark them as answers.  This indicates that the thread is complete, and assists others who may have a similar problem.
    Monday, May 28, 2018 9:54 PM