Nov 22, 2009

Oh Man, Need Help Making Basic Calc In Console App

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General > BASIC / Visual Basic (.NET)

Oh Man, Need Help Making Basic Calc In Console App

broli
I'm stressing out here >_< I'm like so new to vb and console applications. I'm trying to make a calculator in console applications that
can keep adding when i type numbers in until i type '-1' AFter that -1 it would:
give me the total number of entered(+1 each time a number is added),
total scores combined, and
an average of total scores/total numbers entered.

Sad thing is, I'm much better on GUI sleep.gif I made one in GUI so easily, I guess console is that different.
I can get the average, but the scores and total numbers won't go sometimes.

Comment/Reply (w/o sign-up)

miCRoSCoPiC^eaRthLinG
QUOTE(broli @ Dec 11 2005, 05:12 AM)
I'm stressing out here >_<  I'm like so new to vb and console applications.  I'm trying to make a calculator in console applications that
can keep adding when i type numbers in until i type '-1' AFter that -1 it would:
give me the total number of entered(+1 each time a number is added),
total scores combined, and
an average of total scores/total numbers entered.

Sad thing is, I'm much better on GUI sleep.gif  I made one in GUI so easily, I guess console is that different.
I can get the average, but the scores and total numbers won't go sometimes.
*





It's fairly easy - am pasting the working code below. This employes the Console.ReadLine and Console.WriteLine methods to get input and print output respectively. However, ReadLine accepts a string only and I'm using CType to type cast it to an Integer. If you enter an Alphabetic character or a Punctuation mark in the input, this will cause the code to crash. If you want the input checking to be implemented at every step, that can be done too wink.gif So let me know..

CODE

Module ConsoleCalc

   Sub Main()

       'Variables
       Dim Num, TotalNums, Sum As Integer

       'Print out info and prompt
       Console.WriteLine("Enter a series of numbers below, one in each line.")
       Console.WriteLine("When you want to end, enter -1")

       'Init Vars
       TotalNums = 0
       Sum = 0

       'Loop till -1 is entered
       Do

           'Accept a number
           Num = CType(Console.ReadLine, Integer)

           'Check if number is not -1, then add to sum and increment turn
           'If you don't introduce this second check, even the -1 will be counted as a number
           'is the series and added to it - also the total numbers entered will be wrong
           'and throw the average off
           If Num <> -1 Then

               'Add to sum
               Sum += Num

               'Increment Turn
               TotalNums += 1

           End If

       Loop While Num <> -1

       'Print the results
       Console.WriteLine("Total numbers entered: " & TotalNums)
       Console.WriteLine("Sum of all numbers: " & Sum)
       Console.WriteLine("Average: " & Sum / TotalNums)

   End Sub

End Module



I wrote it off in 5 mins and tried it out - fully funtional. If you have any problems running it let me know immediately by return post.

 

 

 


Comment/Reply (w/o sign-up)

broli
CODE
     
 'Print results
       Console.WriteLine("Total Numbers: " & TotalNumbers)
       Console.WriteLine("Sum of scores: " & Sum)
       Console.WriteLine("Average of scores: " & Sum / TotalNumbers)
       Console.WriteLine("")
       Console.WriteLine("Press return to continue...")
       Console.ReadLine()


Alright that really helped me out. Somehow when I tried to do that like &Sum/ TotalNumbers, I got build errors. What I didn't check was my Dim. I got Sum mixed with Add. Don't know why I called it Add so that messed me up. I didn't notice it because I think I was going crazy trying to figure out what things did like console.writeline and etc.

So I fixed it up, and checked all my other codes, and I was suprised mine was like yours, just excluding those minor build errors and less commenting on mine. I'm shy on commenting, but getting better. Also, on my code, I added a few extra lines as you can see. That way when -1 is entered, it'll freeze on the results.

I gotta say this. YOU HELPED ME BIG TIME !! Thanks so much M^E . I'm just beginning my first semister as a college student doing this, and console applications was just random to me. Feels like a GUI, but coding is slightly different, and enough to make me ask for help. So yeah, THANKS !!

Comment/Reply (w/o sign-up)

miCRoSCoPiC^eaRthLinG
Awesome smile.gif Glad that helped.. Any problems about VB/C# feel free to ask. Also the build error u got might have risen out of the fact you did &Sum / TotalNumbers.

See VB is a language that doesn't need you to explicitly declare the variables before you start using them. So even if you used Dim to define Add on the top and then used Sum instead, in the code - no errors will be detected. However, I've noticed in VS.NET editor, sometimes, when you type a "&" symbol and a variable name together - it doesn't autoformat correctly and separate them out...

Your &Sum / TotalNums should get formatted instantly to & Sum / TotalNums.. but that doesn't always happen. If the & sticks to the variable name, it will surely produce build errors.

Comment/Reply (w/o sign-up)

broli
Oh now that you mention it, that's right haha. Hm... Yeah usually the autocorrect does the small things for me too, just not this time... Another thing I better keep my guard up smile.gif

Comment/Reply (w/o sign-up)

FeedBacker
Needed;

3 Textbox's
Names:
Txt1.Text
Txt2.Text
Txt3.Text
Command Button
Name it anything

COMMAND BUTTON SUB HERE OR W/E

Dim FirstNum As Integer
Dim Cond as String
Dim SecNum As Integer
Dim Answer As String
Dim Message As String
Message = "Your Result Is: "
FirstNum = txt1.Text
Cond = txt2.Text
SecNum = txt3.Text

If Cond = "+" Then
Answer = FirstNum + SecNum
MsgBox(Message + Answer)
EndIf

If Cond = "/" Then
Answer = FirstNum / SecNum
MsgBox(Message + Answer)
EndIf

So on? got the idea?

-Joe Edwards

Notice from Approved by BH:

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)


See Also,

*SIMILAR VIDEOS*
Searching Video's for man, making, basic, calc, console, app
advertisement



Oh Man, Need Help Making Basic Calc In Console App

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com