Jump to content

Visual Basic Programs Problem

- - - - -

  • Please log in to reply
4 replies to this topic

#1
oneebmalik

oneebmalik

    Newbie

  • Members
  • Pip
  • 1 posts
Hey all. I'm quite new to programming and I've got a couple of questions for homework. Two of them are causing me a lot of trouble. Could someone tell me what I'm doing wrong? (I'm using Visual Basic 6.0)

Q-1) Input three numbers and print the largest.

Here's the program I've written.


Private Sub Command1_Click()


  Form1.Cls

  

  a = InputBox("Enter the First Number")

  

  b = InputBox("Enter the Second Number")

  

  c = InputBox("Enter the Third Number")

  

  If c > b Then

  

    If c > a Then

    

     largest = c

     

    End If

    

  ElseIf b > a Then


    largest = b

    

  Else

  

    largest = a

    

End If


Print "The Largest Number is " & largest

    

End Sub

I've done something wrong with this. With some inputs, it works, with others it doesn't :/

Q-2) There are two types of tickets issued by an airline; 'Business' and 'Economy'. Both are allowed to carry a limited luggage weight with them. Write a program to input the ticket type and weight of the luggage of the passenger. Calculate his fine according to the info given below:

'Business' : Allowed Weight: 50kg. Fine: $200/kg for extra weight

'Economy' : Allowed Weight: 30kg. Fine: $400/kg for extra weight


I managed to write it with an "IF within IF structure" but I couldn't manage it with logical operators (AND,OR,NOT)



Any help with these would be greatly appreciated.

#2
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 232 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
For one, the scenario ( c>b and a>c ) is not covered, and will never give a result.

Another thing is that you should declare all your variables.

#3
BenHopper

BenHopper

    Newbie

  • Members
  • Pip
  • 1 posts
Hi oneebmalik,

So, the purpose of the program is to test and find the largest number
etc?

#4
DaFatalGigabyte

DaFatalGigabyte

    Newbie

  • Members
  • Pip
  • 1 posts
Like the adage "If A = B and B = C, then A = C":

	If A > B Then
		If B > C Then
			Largest = A
		End If
	ElseIf B > A Then
		If A > C Then
			Largest = B
		End If
	ElseIf C > A Then
		If A > B Then
			Largest = C
		End If

Or

Largest = 0

If A > Largest
	Largest = A
End If

If B > Largest
	Largest = B
End If

If C > Largest
	Largest = C
End If


#5
AceInfinity

AceInfinity

    Advanced Member

  • Members
  • PipPipPip
  • 35 posts
Question 1.
Dim arr() As Integer = {CInt(InputBox("#1")), _
CInt(InputBox("#2")), _
CInt(InputBox("#3"))}

MsgBox(arr.Max())

Assuming you can do your own check on these inputs so that it doesn't raise an exception if it's an invalid integer value for an input, you can probably guess to use IsNumeric().

Question 2.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
	'200/kg Business (50kg)
	'400/kg Economy (30kg)

	MessageBox.Show(CheckTicketFine(TicketType.Economy, 31.23))
End Sub

Private Shared Function CheckTicketFine(Ticket As TicketType, TicketValue As Double) As String
	Select Case Ticket
		Case TicketType.Business
			If TicketValue > 50 Then
				Return String.Format("${0:00}", If(TicketValue > 50, (TicketValue - 50) * 200, 0))
			End If
		Case TicketType.Economy
			Return String.Format("${0:00}", If(TicketValue > 30, (TicketValue - 30) * 400, 0))
	End Select
End Function

Microsoft MVP (2012) - My MSDN Profile
~ "The universe is an intelligence test." - Timothy Leary ~





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users