CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2012
    Posts
    2

    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.
    Last edited by GremlinSA; October 27th, 2012 at 05:54 AM. Reason: added code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    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!
    Last edited by dglienna; October 25th, 2012 at 04:40 PM.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Oct 2012
    Posts
    2

    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.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured