Jump to content
xisto Community
Sign in to follow this  
iGuest

Oh Man, Need Help Making Basic Calc In Console App

Recommended Posts

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 :huh: 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.

Share this post


Link to post
Share on other sites

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 :huh:  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.

1064332465[/snapback]


 

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 :) So let me know..

 

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 SubEnd 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.

Share this post


Link to post
Share on other sites
       '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 !!

Share this post


Link to post
Share on other sites

Awesome :huh: 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.

Share this post


Link to post
Share on other sites

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 :huh:

Share this post


Link to post
Share on other sites

Needed;3 Textbox'sNames:Txt1.TextTxt2.TextTxt3.TextCommand ButtonName it anythingCOMMAND BUTTON SUB HERE OR W/EDim FirstNum As IntegerDim Cond as StringDim SecNum As IntegerDim Answer As StringDim Message As StringMessage = "Your Result Is: " FirstNum = txt1.TextCond = txt2.TextSecNum = txt3.TextIf Cond = "+" Then Answer = FirstNum + SecNumMsgBox(Message + Answer)EndIfIf Cond = "/" ThenAnswer = FirstNum / SecNumMsgBox(Message + Answer)EndIfSo on? got the idea?-Joe Edwards

Notice from Approved by BH:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.