0

I'm pretty new in the environment of VB.NET that uses only consoles. I have a problem regarding my program, the console get quickly terminated. How can I my console from doing that? Thanks.

heres my code:

Module Module1
 Dim num1 As Integer = 0
 Dim num2 As Integer = 0
 Dim ans As Decimal = 0
 Dim choice As String
 Sub Main()
 Console.WriteLine("First Number: ")
 num1 = Console.ReadLine
 Console.WriteLine("Second Number: ")
 num2 = Console.ReadLine
 Console.WriteLine("[A] Addition")
 Console.WriteLine("[S] Subtraction")
 Console.WriteLine("[M] Multiplication")
 Console.WriteLine("[D] Division")
 Console.WriteLine("Enter your choice: ")
 choice = Console.ReadLine
 If (choice.ToUpper() = "A") Then
 ans = num1 + num2
 Console.WriteLine(ans.ToString)
 ElseIf (choice.ToUpper() = "S") Then
 ans = num1 - num2
 Console.WriteLine(ans.ToString)
 ElseIf (choice.ToUpper() = "M") Then
 ans = num1 * num2
 Console.WriteLine(ans.ToString)
 ElseIf (choice.ToUpper() = "D") Then
 ans = num1 / num2
 Console.WriteLine(ans.ToString)
 End If
 End Sub
End Module
asked Jun 13, 2014 at 12:52
6
  • 2
    Insert Console.ReadKey() at the end just before the End Sub line. Commented Jun 13, 2014 at 12:54
  • At what point in your code does it get terminated? What does the debugger tell you? Commented Jun 13, 2014 at 12:54
  • 2
    Please search how to use Option Strict On. Executing mathematical operations with strings is a really bad thing Commented Jun 13, 2014 at 12:57
  • Console.ReadKey() pretty works. :) thanks. Even I said im new in the VB.NET environment, people keeps downgrading my question. :/ Commented Jun 13, 2014 at 12:57
  • 3
    @akoDwin your question may have been downvoted, as you didn't explain how the behaviour differed from what you expected, so not even "it quits immediately after printing the answer" - similarly, there's no demonstration of what you've tried to try and resolve the issue yourself Commented Jun 13, 2014 at 13:02

3 Answers 3

1

Console.ReadKey() should do what you are asking

put it before End Sub

refer here

Obtains the next character or function key pressed by the user. The pressed key is displayed in the console window.

answered Jun 13, 2014 at 12:56
Sign up to request clarification or add additional context in comments.

Comments

1

The console program has reached the end of its routine causing it to terminate. You need to put a Console.ReadLine or Console.ReadKey in the main method. This will wait for user input.

I'll usually do something like this

Sub Main()
 Console.WriteLine("Press any key to exit")
 Console.ReadKey()
 Console.WriteLine("Press enter to exit")
 Console.ReadLine()
End Sub
answered Jun 13, 2014 at 12:56

Comments

0

This is normally only an issue when debugging, so I add:

If Debugger.IsAttached Then _
 Console.ReadLine()

at the end of my console program Main routines.

answered May 12, 2015 at 16:52

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.