|
|
|
|
![]() ![]() |
Dec 10 2005, 10:12 PM
Post
#1
|
|
|
Premium Member Group: Banned Posts: 213 Joined: 2-July 05 Member No.: 6,781 |
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 I can get the average, but the scores and total numbers won't go sometimes. |
|
|
|
Dec 11 2005, 05:11 AM
Post
#2
|
|
|
PsYcheDeLiC dR3aMeR Group: Admin Posts: 2,242 Joined: 29-January 05 From: Nakorn Chaisri, Thailand Member No.: 2,411 |
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 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 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. |
|
|
|
Dec 11 2005, 11:03 AM
Post
#3
|
|
|
Premium Member Group: Banned Posts: 213 Joined: 2-July 05 Member No.: 6,781 |
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 !! |
|
|
|
Dec 11 2005, 02:06 PM
Post
#4
|
|
|
PsYcheDeLiC dR3aMeR Group: Admin Posts: 2,242 Joined: 29-January 05 From: Nakorn Chaisri, Thailand Member No.: 2,411 |
Awesome
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. |
|
|
|
Dec 11 2005, 08:34 PM
Post
#5
|
|
|
Premium Member Group: Banned Posts: 213 Joined: 2-July 05 Member No.: 6,781 |
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
|
|
|
|
Dec 17 2007, 10:18 PM
Post
#6
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 0 Joined: 1-November 07 Member No.: 25,869 |
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:
|
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 5th September 2008 - 09:26 AM |