Help with basic console application
hello-
I need a little help finishing a program for school - I missed class last week and am a little stuck with one of the request from the professor.
I'm not sure about how to get the code to display invalid integer when you choose option 4 and you try to divide by 0 as of now it just displays nan = which i know is visual basic stating infinity. If you could point me in the right direction that would be great - as I want to get a + on this assignment.
This is what i have so far.
Thank You
Code:
Module Module1
Sub Main()
'Store needed Variables
Dim num1, num2 As Integer
Dim opt1, opt2, opt3, opt4 As String
Dim choice As Integer
'Prompt user for 2 Integers
Console.WriteLine("Please enter 2 numbers?")
num1 = Console.ReadLine
Console.WriteLine("Please enter 2nd number")
num2 = Console.ReadLine
Console.WriteLine("Please Select a following option:")
Console.WriteLine("Option 1 = Add both integers")
Console.WriteLine("Option 2 = Subtract 2nd integer from 1st integer")
Console.WriteLine("Option 3 = Multiply Integers")
Console.WriteLine("Option 4 = Divide 1st integer by 2nd integer")
Console.WriteLine("What is your Choice?")
choice = Console.ReadLine
opt1 = num1 + num2
opt2 = num1 - num2
opt3 = num1 * num2
opt4 = num1 / num2
If choice = "1" Then
Console.WriteLine("Option 1=" & opt1)
ElseIf choice = "2" Then
Console.WriteLine("Option 2=" & opt2)
ElseIf choice = "3" Then
Console.WriteLine("Option 3=" & opt3)
ElseIf choice = "4" Then
Console.WriteLine("Option 4=" & opt4)
ElseIf choice = "4" And num1 <= 0 Or num2 <= 0 Then
Console.WriteLine("Error Invalid Integer")
Else : Console.WriteLine("Invalid Choice")
End If
Kind of stuck at the last part.
Re: Help with basic console application
You'd need ERROR CHECKING at some point. Do you want to check if the number is zero? That would be easiest.
Also, an IF statement allows the ERROR to be trapped, with an ELSE. Do you want to display an ERROR MESSAGE or just silently fail to print anything?
Also, put the calcs WITHIN the the IF statements rather than doing them FIRST.
Code:
opt1 = num1 + num2
opt2 = num1 - num2
opt3 = num1 * num2
opt4 = num1 / num2
SELECT CASE is better than IF/THEN, btw! Look up what you missed, or look into the NEXT CHAPTER OF THE BOOK!
Re: Help with basic console application
Thank You for the tips, I got it working well enough before, someone else also suggested moving the calcs as well and that is what i did-
Thank you for the help.
Re: Help with basic console application
This is the VISUAL BASIC 6 ( SIX ) Forum, which is over 12 years old. your code is VB.NET, here is the VB.NET Forum :
http://forums.codeguru.com/forumdisp...ual-Basic-.NET