quick Visual Studio (console app) question

GJSNeptune

[H]F Junkie
Joined
Mar 31, 2004
Messages
12,372
My class is finally done with logic planning, and now we're touching on VB. My homework doesn't require this simple program to actually continue, but it bugs me that it wants my output to include Press the ENTER key to continue, yet it simply exits.

Is there a relatively simply way for this to reset when the user hits ENTER to continue?

Code:
    Sub Main()
        Dim decCustSubTotal As Decimal
        Dim decCustSalesTax As Decimal
        Dim decCustNetTotal As Decimal

        ' Accept the subtotal of the order
        Console.Write("Enter the order amount: ")
        decCustSubTotal = Console.ReadLine()

        ' Calculate sales tax and net total
        decCustSalesTax = decCustSubTotal * 0.06
        decCustNetTotal = decCustSubTotal + decCustSalesTax

        ' Write results to the console
        Console.WriteLine()
        Console.WriteLine("Subtotal : " + decCustSubTotal.ToString("F2"))
        Console.WriteLine("Sales Tax: " + decCustSalesTax.ToString("F2"))
        Console.WriteLine("----------")
        Console.WriteLine("Net Total: " + decCustNetTotal.ToString("F2"))
        Console.WriteLine()
        Console.Write("Press the ENTER key to continue.")
        Console.Read()
    End Sub
 
it exists because thats the end of the program after it reads the user input
if you want it to start over just do a loop. I'm assuming your class hasnt covered this yet so just read ahead in your book or google loops for VB if you want to still do it now.
 
I know it exits because it's all sequence. I looked ahead in the book, but this class isn't a programming class. It's mostly logic. I know about loops, but I'm not familiar with Visual Basic yet, and I'm not sure how to loop it unconditionally (if possible). Otherwise I'd have to check which key the user entered or something.
 
Code:
        [...]
        Console.Write("Press the ENTER key to continue.")
        Console.ReadLine()
        Main()
End Sub
?
 
Tried that, as well as some other things. I keep getting this error on decCustSubTotal = Console.ReadLine()

Code:
Conversion from string "" to type 'Decimal' is not valid.

Not entirely sure why. Probably the variable being empty because it isn't hanging at the Console.Write. Even if I do decCustSubTotal = 0, I still get the error.
 
You hit enter twice, thus decCustSubTotal being an empty string.
Code:
        [...]
        Console.WriteLine()
        Console.Write("Press the ENTER key to continue.")
        While Console.ReadKey.Key <> ConsoleKey.Enter
        End While
        Console.Clear()
        Main()
    End Sub

Ctrl+C to break.
 
You hit enter twice, thus decCustSubTotal being an empty string.
Code:
        [...]
        Console.WriteLine()
        Console.Write("Press the ENTER key to continue.")
        While Console.ReadKey.Key <> ConsoleKey.Enter
        End While
        Console.Clear()
        Main()
    End Sub

Ctrl+C to break.
Sweet. Thanks.
 
"simply calling main" is not a proper method. Sure it will work for a cheesy commandline application, but if you use that to loop for a long time you will have a stack overflow.

while() loops were created for a reason.
 
but if you use that to loop for a long time you will have a stack overflow.

Sure, after about 3000 iterations, which is probably a figure that the OP would never get to. But then again, if you don't do it right, why do it at all. :p
 
Heh. Professor isn't even going to run the program. Wants a printout of the source and screenshots of the output.
 
Back
Top