Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Oh Man, Need Help Making Basic Calc In Console App
broli
post Dec 10 2005, 10:12 PM
Post #1


Premium Member
Group Icon

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 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.
Go to the top of the page
 
+Quote Post
miCRoSCoPiC^eaRt...
post Dec 11 2005, 05:11 AM
Post #2


PsYcheDeLiC dR3aMeR
Group Icon

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 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.
Go to the top of the page
 
+Quote Post
broli
post Dec 11 2005, 11:03 AM
Post #3


Premium Member
Group Icon

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 !!
Go to the top of the page
 
+Quote Post
miCRoSCoPiC^eaRt...
post Dec 11 2005, 02:06 PM
Post #4


PsYcheDeLiC dR3aMeR
Group Icon

Group: Admin
Posts: 2,242
Joined: 29-January 05
From: Nakorn Chaisri, Thailand
Member No.: 2,411



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.
Go to the top of the page
 
+Quote Post
broli
post Dec 11 2005, 08:34 PM
Post #5


Premium Member
Group Icon

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 smile.gif
Go to the top of the page
 
+Quote Post
iGuest
post Dec 17 2007, 10:18 PM
Post #6


Newbie [ Level 1 ]
Group Icon

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:
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Stop anything from making it ur homepage!(52)
  2. Basic Tips and Tricks in HTML(15)
  3. Basic Tutorial: PHP GD(15)
  4. Visual Basic Help(5)
  5. Making A Logo In Paint(20)
  6. (Nearly) Ultimate Music Posting Guide(11)
  7. Flash Site Software(11)
  8. Visual Basic 6 + Crystal Reports 9(6)
  9. Turn Your Gaming Console Into A Media Center Pc(3)
  10. Advice About Making A Text Based Game(9)
  11. Programming In Glut (lesson 4)(7)
  12. C++: Basic Classes(5)
  13. Asterisknow Pbx (voip Telephony)(1)
  14. Making A Turn Based Game Like Ogame(9)
  15. [photoshop] Making An Orb(1)
  1. Making Xp Starts 60% Faster(3)
  2. My Guide On Runescape Making Millions(4)
  3. Need Help Making My Ftp Work(14)
  4. Some Usefull Linux Basic Commands And Utilities. Please Add To This List If You Know One.(0)
  5. I Really Need Help(5)
  6. Linux Basic Command - For Storing Compilation Error To File(1)
  7. Help With Making A Textbased Game(2)
  8. Basic Css(5)
  9. Help On Calc Date Issues !(4)
  10. Help With Making A Textbased Game(6)
  11. Making Xp Look Like Vista(3)
  12. An Absolute Basic Guide To Algorithms For Dummies(0)
  13. Making Educational Game(3)


 



- Lo-Fi Version Time is now: 5th September 2008 - 09:26 AM