locked
how do you have user enter in 3 different integers

    Question

  • How do you ask the user to enter in 3 different integers?
    Wednesday, February 09, 2011 9:32 PM

Answers

  • This is a very basic way to get threee integers from the user with a console application.  Note that the program will fail if the user enters invalid data.

    The three integers are stored in the variables A B and C - the equivalent of your One, Two and Three. I have used the names A B and C to make it clear that the variables are not associated with any particular value of the user's input they are just labels that allow you to refer to what the user entered.

    You can use the Console.Writeline method to 'print' the values the user entered.

    You can use IF statements to test the variables to find the smallest.  I think the exercise is probably to try and work out what the IF statements need to be to find the smallest of three variables, when VB only allows you to compare two at a time.

     

        Sub Main()
            
    Dim Input As String = ""
            Console.WriteLine("First Number")
            Input = Console.ReadLine()
            
    Dim A As Integer = Val(Input)
            Console.WriteLine(
    "Second Number")
            Input = Console.ReadLine()
            
    Dim B As Integer = Val(Input)
            Console.WriteLine(
    "Third Number")
            Input = Console.ReadLine()
            
    Dim C As Integer = Val(Input)
        
    End Sub

    You will detect from the discussion so far that it is much easier for someone to indicate corrections and adjustments to your code than it is to try to solve your problem for you.

    Thursday, February 10, 2011 1:52 AM
  • How do you ask the user to enter in 3 different integers?

    Hi sha10233012,

     

    Add code to the bottom of this code to find the lowest number.

    This console application will keep asking you for numbers until you have 3 numbers that are different .>>

     

    Module Module1
    
      Sub Main()
    
        Dim myNumbers As New List(Of Integer)
    
        Dim inputString As String = ""
    
        Do
          Console.WriteLine("Please type in an INTEGER number then press the ENTER key.")
          Console.WriteLine()
    
          inputString = Console.ReadLine
          If inputString = "" Then Continue Do
    
          If myNumbers.Count = 0 Then
            myNumbers.Add(Val(inputString))
          ElseIf myNumbers.Contains(Val(inputString)) = True Then
            Console.WriteLine("You have already tried that number, please try again.")
            Continue Do
          Else
            myNumbers.Add(Val(inputString))
          End If
    
        Loop Until myNumbers.Count = 3
    
        Console.WriteLine()
        Console.WriteLine("The numbers entered are;")
        For index As Integer = 0 To myNumbers.Count - 1
          Console.Write(myNumbers.Item(index).ToString & ", ")
        Next
    
        'At this point add your code to determine the lowest number.>>
    
    
    
        Console.ReadLine()
      End Sub
    
    End Module

     

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Thursday, February 10, 2011 2:30 AM
  • Would code line

      Console.WriteLine("Third Number")
      Input = Console.ReadLine()
      Dim C As Integer = Val(Input)
    
    

    Cause a problem as you could input 2.3 and the variable C would be stored as an Integer value of 2.

    So the code would work in one respect but additional validation would be required to ensure that the entered string Input is indeed an Integer value.

    I guess it depends upon the exact OP requirement.   As this is a homework item then I guess its down to them to come up with level of validation required.

    Thursday, February 10, 2011 7:14 AM

All replies


  • In a windows forms application or in a console application?
     
    Window Forms: I would use a lable and three text boxes.
    Console App:
      I would use console.writeline ("Please enter three values (space separated):")
      Then use:
        dim input as string = Console.ReadLine()
        dim words () as string = input.split(" "c, StringSplitOptions.RemoveEmptyEntries)
     
    Now words has the three integers as strings, which you can convert to integers.

    --
    Mike
    Wednesday, February 09, 2011 9:38 PM
  • I think you need to be more specific on what your problem is as there are numerous ways of doing this.

    • Are they 3 values in 1 textbox on a form
    • Are they 3 values in 3 different textbox on a form
    • Are they 3 values from the command line

    Is the problem entering them or validating them.

     

    Wednesday, February 09, 2011 10:10 PM
  • Ok sorry about that this is in vb console i have to prompt users to enter 3 different integers and assume each integer is different. I also have to print the 3 integers. I also have to use the if states to find the smallest integer.
    Wednesday, February 09, 2011 11:33 PM
  • The following is an console application and validates that 3 unique integers have been provided as arguments

    Module Module1
    
      Sub Main()
        Dim cmdlineargs() As String = Environment.GetCommandLineArgs
    
        If cmdlineargs.Count <> 4 Then
          Console.WriteLine("Did not enter correct number of arguments")
          Exit Sub
        End If
    
        Dim FirstArg = IsInteger(cmdlineargs(1))
        Dim SecondArg = IsInteger(cmdlineargs(2))
        Dim ThirdArg = IsInteger(cmdlineargs(3))
    
        If FirstArg = False OrElse SecondArg = False OrElse ThirdArg = False Then
          Console.WriteLine("All three values are not integers")
          Exit Sub
        End If
    
        Dim UniqueList As New List(Of String) '
        For i = 1 To 3
          Dim added = AddIfNotExist(UniqueList, cmdlineargs(i))
          If added = False Then
            Console.WriteLine("All three values are not Unique")        
            Exit Sub
          End If
        Next
        Console.WriteLine("Success - values are:")
        For i = 1 To 3
          Console.WriteLine(cmdlineargs(i))
        Next
    
      End Sub
    
      Function IsInteger(x As String) As Boolean
        Dim IsIntegerValue = False
        If x = CInt(x).ToString Then
          IsIntegerValue = True
        End If
        Return IsIntegerValue
      End Function
    
      'Used to detect duplicates
      Function AddIfNotExist(x As List(Of String), arg As String) As Boolean
        If (From i In x Where i = arg Select i).Count = 0 Then
          x.Add(arg)
          Return True '//Added
        Else
          Return False '//Dis Not add
        End If
      End Function
    
    
    End Module
    
    
    Numerous ways of doing it but its just one.
    Thursday, February 10, 2011 12:51 AM
  • Wow you are good..Ok this is kinda hard to explain i am only in my fourth week of school. This is the a little two advance i am going over it again in my book. It looks simple but to write it down is something else.

    I am trying to write is as

    dim one as integer

    dim two as integer

    dime three as integer

    but its not working. what you wrote up there is not in my book yet. I am trying to find maybe videos on the computer to maybe should. I know what now the chapter we are working on is control structures: selection so.

    Prompt the user to enter 3 different integers.  This time only, assume the 3 integers are different.

    2.  Print the 3 integers.

    3.  Use IF statements to find the smallest integer.

    4.  This should work for any numbers that are entered. Enter them in different orders.

         Sample test data:  1, 2, 3   or  1, 3, 2   or   2, 1, 3   or  2, 3, 1   or  3, 1, 2  or  3, 2, 1

         Sample Output:  The integers are:  1, 2, 3

                                  The smallest is:  1

    THis is what she is asking me to do I kinda understand but it is just trying to figure out how to write to prompt user to enter 3 different integers.

    Thursday, February 10, 2011 1:02 AM
  • OK, as its a homework question I'm not going to do it for you but I'll provide some pointers.

    It sounds like you are going to to be doing a windows form application

    1. To display an item you probably are going to use the .Text property of a control such as Label1.Text = "1" Or textbox1.text = "2"

    2. In you example one, two and three are variables

    3.  You are going to need to use the IF statement to construct some logic to find the smallest.

    The general construct for IF Statements is

    IF one < two Then

         'Two is larger

    ELSEIF one = two Then

         'Both are the same

    ELSE

         'One is larger

    END IF

     

    Sounds like you need to read the book a little as this should be explained fairly early.

    Thursday, February 10, 2011 1:33 AM
  • Well we are using the console and i am not asking no one to do my homework maybe just to make pointers.
    Thursday, February 10, 2011 1:39 AM
  • This is a very basic way to get threee integers from the user with a console application.  Note that the program will fail if the user enters invalid data.

    The three integers are stored in the variables A B and C - the equivalent of your One, Two and Three. I have used the names A B and C to make it clear that the variables are not associated with any particular value of the user's input they are just labels that allow you to refer to what the user entered.

    You can use the Console.Writeline method to 'print' the values the user entered.

    You can use IF statements to test the variables to find the smallest.  I think the exercise is probably to try and work out what the IF statements need to be to find the smallest of three variables, when VB only allows you to compare two at a time.

     

        Sub Main()
            
    Dim Input As String = ""
            Console.WriteLine("First Number")
            Input = Console.ReadLine()
            
    Dim A As Integer = Val(Input)
            Console.WriteLine(
    "Second Number")
            Input = Console.ReadLine()
            
    Dim B As Integer = Val(Input)
            Console.WriteLine(
    "Third Number")
            Input = Console.ReadLine()
            
    Dim C As Integer = Val(Input)
        
    End Sub

    You will detect from the discussion so far that it is much easier for someone to indicate corrections and adjustments to your code than it is to try to solve your problem for you.

    Thursday, February 10, 2011 1:52 AM
  • Well we are using the console and i am not asking no one to do my homework maybe just to make pointers.

    I've made pointers on what you should be looking at.

    To write to the Console use

    Console.Writeline("Test String")

    or

    Console.Write("Test String")

    IF ... ELSEIF .... ELSE .... END IF    construct

    As well as in previous example - lists  /array s and logic to determine if 3 unique integer values are entered.

    So everything is there to achieve your result - all you need to do is apply it to your actual application.

    Others have provided alternatives as well.   As with many things there are many ways to achieve things and the homework is probably there to determine what you know or have researched and have applied.

    With all the code here I can see some potential problems (mine included).    If you start experimenting and find problems then you should be able to provide you code and your problem and people can point you in the right direction to fix your problem.

     

    Thursday, February 10, 2011 2:29 AM
  • How do you ask the user to enter in 3 different integers?

    Hi sha10233012,

     

    Add code to the bottom of this code to find the lowest number.

    This console application will keep asking you for numbers until you have 3 numbers that are different .>>

     

    Module Module1
    
      Sub Main()
    
        Dim myNumbers As New List(Of Integer)
    
        Dim inputString As String = ""
    
        Do
          Console.WriteLine("Please type in an INTEGER number then press the ENTER key.")
          Console.WriteLine()
    
          inputString = Console.ReadLine
          If inputString = "" Then Continue Do
    
          If myNumbers.Count = 0 Then
            myNumbers.Add(Val(inputString))
          ElseIf myNumbers.Contains(Val(inputString)) = True Then
            Console.WriteLine("You have already tried that number, please try again.")
            Continue Do
          Else
            myNumbers.Add(Val(inputString))
          End If
    
        Loop Until myNumbers.Count = 3
    
        Console.WriteLine()
        Console.WriteLine("The numbers entered are;")
        For index As Integer = 0 To myNumbers.Count - 1
          Console.Write(myNumbers.Item(index).ToString & ", ")
        Next
    
        'At this point add your code to determine the lowest number.>>
    
    
    
        Console.ReadLine()
      End Sub
    
    End Module

     

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Thursday, February 10, 2011 2:30 AM
  • How do you ask the user to enter in 3 different integers?

    I think OP means 'distinct' rather than 'different' - at least for the moment.

    "Prompt the user to enter 3 different integers.  This time only, assume the 3 integers are different."

    Thursday, February 10, 2011 3:21 AM
  • How do you ask the user to enter in 3 different integers?

    I think OP means 'distinct' rather than 'different' - at least for the moment.

    "Prompt the user to enter 3 different integers.  This time only, assume the 3 integers are different."

    Hi Acamar,

    It is not like you to be pedantic.

    See these alternative synonyms.>>

    http://www.onlinethesaurus.co.uk/reference/thesaurus.jsp?q=different

    Distinct can be taken to mean different as in dissimilar.

    I.E:

    2 <> 3

    but

    2 = 2

    as opposed to meaning distinct as in "distinctly separate values" or instances of an Integer.

    The separate values here {2,2,2} are distinct from one another in terms of set-position but similar in value.

    The values {1,2,3} are also distinct from one another in terms of set-position but dissimilar in value.

    I guess it all depends on how far you want to define your definition of different .

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Thursday, February 10, 2011 7:08 AM
  • Would code line

      Console.WriteLine("Third Number")
      Input = Console.ReadLine()
      Dim C As Integer = Val(Input)
    
    

    Cause a problem as you could input 2.3 and the variable C would be stored as an Integer value of 2.

    So the code would work in one respect but additional validation would be required to ensure that the entered string Input is indeed an Integer value.

    I guess it depends upon the exact OP requirement.   As this is a homework item then I guess its down to them to come up with level of validation required.

    Thursday, February 10, 2011 7:14 AM
  • I guess it depends upon the exact OP requirement. 

    As this is a homework item then I guess its down to them to come up with level of validation required.

    Hi spotty,

    VAL

    could cause a problem, depending on OPTION settings as it returns a DOUBLE.

    Additionally I did not want to use

    Integer.TryParse

    as I don't want to confuse the OP too much.



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Thursday, February 10, 2011 7:21 AM
  • Agreed - I think there is more than enough information and examples provided here for the OP to piece together there exact requirement along with there textbook they mentioned.

    There are limitations to the code shown in my samples as well.   The only way the OP will learn is to try the examples and debug them to meet there application specification.

    As I'd stated there are numerous ways to solve the problem.

     

    Thursday, February 10, 2011 7:57 AM
  • Thank you all who explain, i really appericate it.
    Thursday, February 10, 2011 2:19 PM
  • The qualification "assume the 3 integers are different" is the reason that I read that OP had used 'different' in two different ways.  The statement only makes sense if the first use of 'different' is actually 'distinct'.  That is, OP had a problem undertanding how the user can enter threee numbers so that the program knows there are threee of them and can handle them separately - as distinct numbers, or numbers that can be differentiated from each other otherwise than by the fact that they have different values.  The purpose of using three numbers is to find the smallest, and of course there is the problem that if two or more of these distinct numbers have the same value, how do you define 'smallest"?  OP was told, for the moment, assume that the numbers that the user enters will be different from eachother - that is, ignore any problem associated with finding the smaller of two equal numbers.  Eventually, that problem will have to be addressed, but whether it is addressed in the comparison, or by excluding the input of numbers with a value already entered, is not clear.

    Thursday, February 10, 2011 9:19 PM