VB.Net Console Application

Posted on 2011-10-11
Last Modified: 2012-06-21
I am trying to create a console application that will take an inputted dollar amount and tell you how much of each item you can by for that amount. like this example:

 Enter dollar amount: $10
You can buy any one of the following items with $10.

22.7273 pounds of Bananas
10.101 pounds of Apples
8.4034 pounds of Cucumbers
11.236 pounds of Carrots
12.6582 pounds of Oranges

Do you want to try for another amount? ...

Here is what I have so far.

Open in new window

'Project:       How Much Can I Buy Console App

    Private promptMessage As String = "Enter a dollar amount"
    Private welcomeMessage As String = " Welcome to How Much Can I Buy"

    Sub Main()

        Dim strInput As String
        Dim DollarAmt As Double = 0
        Dim ProductPrice As Double
        Dim ProductAmt As Double
        Dim ProductName As String


    ProductPrice = _{{0.44, 0.99, 1.19, 0.89, 0.79}}
        'ProductName =_ {{Bananas, Apples, Cucumbers, Carrots, Oranges}}
            ProductAmt = DollarAmt / ProductPrice

    then print strInput


                Console.WriteLine("You can buy any one of the following items with" & DollarAmt)
                strInput = Console.ReadLine()
        case bananas
                Console.WriteLine(ProductAmt & "pounds of Bananas")
        case apples
                Console.WriteLine(ProductAmt & "pounds of Apples")
        case cucumbers
                Console.WriteLine(ProductAmt & "pounds of Cucumbers")
        case carrots
                Console.WriteLine(ProductAmt & "pounds of Carrots")
        case oranges
                Console.WriteLine(ProductAmt & "pounds of Oranges")
End Module

Open in new window

Question by:IchigoMD
    LVL 38

    Expert Comment

    by:Tom Beck
    Your code seems overly complicated. Why the rectangular arrays? Why the "case"? Is this a student assignment?

    Author Comment

    Yes I have trouble starting but once i know how to get it started i think I know the correct direction to go.
    LVL 38

    Accepted Solution

    Ok, student assignment. I can provide suggestions. You write all the code.

    Your code seems unnecessarily complex. Unless the assignment calls for the use of rectangular arrays or Switch statements, I would take those components out. As I understand the assignment, you need four variables with assigned values (essentially constants) and one variable for user input that actually is "variable".

    On a side note, since you only have one sub, it will not matter if the variables are declared globally (at the class level, outside of any sub routine) or locally (inside a sub routine).

    You have two strings declared globally and this is working.

    You need to store the products and prices in variables, and arrays are a good choice, but rectangular arrays are not necessary. As long as you keep the Product Prices and the Product Names in the same order in two separate, simple (one dimensional) arrays, the indexes will always be the same. In other words ProductPrice(0) will be the price per pound for ProductName(0), ProductPrice(1) will be the price per pound for ProductName(1), etc.

    Here's some more information on the different types of arrays:

    If you want to allow the user to repeatedly try different dollar amounts to see the different results, then the While loop was a good choice. While loops continue until the condition being evaluated becomes "true". However inside the while loop you simply need to write to the console the result of the calculation and the product name for each product and each price per pound in the arrays, one result per line. No need for "case" statements. But what condition should evaluate for the While loop? If you want the application to continue until the user closes it, then you essentially want an endless loop. Create a condition to evaluate based on that.

    Feel free to post the latest version of you code if you get stuck.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now