12 Replies - 13417 Views - Last Post: 29 October 2010 - 08:15 PM Rate Topic: -----

#1 CincinnatiCoralReeves  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 29-October 10

VB Distance Calculator Challenge

Posted 29 October 2010 - 04:45 PM

Hi, I have recently started taking a visual basic class. This is my first glimpse into this world. Go easy on me.
'VB-01
Public Class frmDistanceCalculator

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        Dim decDistanceTravelled As Decimal
        Dim decNumberofHoursInput As Decimal
        Dim decMPHSpeedInput As Decimal
        Dim intCounter As Integer = 1
        Dim decTotalDistance As Decimal = 0
        Dim blnFlag As Boolean
        Try
            For intCounter = 1 To decNumberofHoursInput
                decMPHSpeedInput = InputBox("Please enter your average rate speed in MPH", "Speed")
                decNumberofHoursInput = InputBox("Please enter the number of hours you spent", "Hours")
                If IsNumeric(decMPHSpeedInput) Or decNumberofHoursInput < 0 Then
                    MessageBox.Show("Please enter a number greater than zero.")
                Else
                    'Calculation        
                    decDistanceTravelled = decNumberofHoursInput * decMPHSpeedInput
                    'Listbox
                    blnFlag = True
                    lstOutput.Items.Add("Vehicle Speed:" & decMPHSpeedInput.ToString & "MPH" & "Time Travelled:" & ControlChars.CrLf _
        & decNumberofHoursInput.ToString & "hours" & ControlChars.CrLf & "Hours" & "Distance Travelled" & ControlChars.CrLf _
        & "******************************************")
                    Do While decNumberofHoursInput > 1
                        decNumberofHoursInput = decNumberofHoursInput - 1
                    Loop
                    Do While blnFlag = True
                        lstOutput.Items.Add(decNumberofHoursInput.ToString)
                        blnFlag = False
                    Loop
                End If
                'If IsNumeric(decNumberofHoursInput) And IsNumeric(decMPHSpeedInput) Then
                '    If decNumberofHoursInput < 0 Then
                '        MessageBox.Show("Please enter a number greater than zero.")

                '    Else
                '        decDistanceTravelled = decNumberofHoursInput * decMPHSpeedInput

                '    End If
                'End If
                'Output
                If intCounter = decNumberofHoursInput Then
                    lstOutput.Items.Add("Total Distance: " & decTotalDistance.ToString)


                End If
            Next
        Catch ex As Exception

        End Try


    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
End Class 




(should i take a screen shot instead of dumping the code?)
I am not totally sure if this is formatted correctly...I need some help. My input box stopped showing up even...




Thanks in advance.

This post has been edited by macosxnerd101: 29 October 2010 - 04:59 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: VB Distance Calculator Challenge

#2 CincinnatiCoralReeves  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 29-October 10

Re: VB Distance Calculator Challenge

Posted 29 October 2010 - 04:57 PM

I think I need to use If Not Double.TryParse
Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,919
  • Joined: 25-September 09

Re: VB Distance Calculator Challenge

Posted 29 October 2010 - 05:35 PM

The problem with the inputbox is your for...next statement.
For intCounter = 1 To decNumberofHoursInput


Since you have only initialized decNumberofHoursInput and not given it a value it is 0 so your for...next is basically
For intCounter = 1 to 0


Try setting decNumberofHoursInput = {something greater than 1}
Was This Post Helpful? 0
  • +
  • -

#4 CincinnatiCoralReeves  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 29-October 10

Re: VB Distance Calculator Challenge

Posted 29 October 2010 - 05:44 PM

I just turned option strict on and I have a bunch of conversion errors now.

I am going to turn it back off, and try to set the decNumberofHours to 10?

just for testing?
Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,919
  • Joined: 25-September 09

Re: VB Distance Calculator Challenge

Posted 29 October 2010 - 05:49 PM

One thing to keep in mind is that the inputbox returns a string. Option Strict will throw an error since you are placing the result into a decimal variable.

What I would do personally is to create 2 new variables (strMPH, strHours) or similar and assign them to the inputbox result. Then use decimal.tryparse with your decimal variables to check for the validity of the input.

Something like:
    strMPH = InputBox("Please enter your average rate speed in MPH", "Speed")
    strHours = InputBox("Please enter the number of hours you spent", "Hours")
       If Not Decimal.TryParse(strMPH, decMPHSpeedInput) Or Not Decimal.TryParse(strHours, decNumberofHoursInput) Then
          MessageBox.Show("Please enter a number greater than zero.
       Else
...


This should appease Option Strict also.
Was This Post Helpful? 1
  • +
  • -

#6 CincinnatiCoralReeves  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 29-October 10

Re: VB Distance Calculator Challenge

Posted 29 October 2010 - 06:06 PM

I am trying to get the hang of this.

First of all Thank you Thank you Thank you for the help.


what you suggested worked!


It builds...
The inputbox prompts...
The string even lists in the list box...


but


the loop for prompt keeps looping...

and the text displayed comes out in a clumped, bunched line instead of ControlChar.CrlF working like <br />
in HTML?

This is what i have now. Is tryparse in the right place?
Option Strict On
Public Class frmDistanceCalculator

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        Dim decDistanceTravelled As Decimal
        Dim decNumberofHoursInput As Decimal = 10
        Dim decMPHSpeedInput As Decimal
        Dim intCounter As Integer = 1
        Dim decTotalDistance As Decimal = 0
        Dim blnFlag As Boolean
        Dim strSpeed As String
        Dim strHours As String
        Try
            For intCounter = 1 To CInt(decNumberofHoursInput)
                strSpeed = InputBox("Please enter your average rate speed in MPH", "Speed")
                strHours = InputBox("Please enter the number of hours you spent", "Hours")
                If Not Decimal.TryParse(strSpeed, decMPHSpeedInput) Or Not Decimal.TryParse(strHours, decNumberofHoursInput) Then
                    MessageBox.Show("Values must be numberic.")

                ElseIf decMPHSpeedInput < 0 Then
                    MessageBox.Show("Values must be greater than zero.")
                Else
                    'Calculation        
                    decDistanceTravelled = decNumberofHoursInput * decMPHSpeedInput
                    'Output
                    blnFlag = True
                    lstOutput.Items.Add("Vehicle Speed:" & strSpeed & "MPH" & "Time Travelled:" & ControlChars.CrLf _
        & strHours & "hours" & ControlChars.CrLf & "Hours" & "Distance Travelled" & ControlChars.CrLf _
        & "******************************************")
                End If
            Next

            Do While decNumberofHoursInput >= 1
                decNumberofHoursInput = decNumberofHoursInput - 1
            Loop
            Do While blnFlag = False
                lstOutput.Items.Add(decNumberofHoursInput.ToString)
                blnFlag = False
            Loop
            'Total
            If intCounter = decNumberofHoursInput Then
                lstOutput.Items.Add("Total Distance: " & decTotalDistance.ToString)


            End If
        Catch ex As Exception

        End Try

Was This Post Helpful? 0
  • +
  • -

#7 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,919
  • Joined: 25-September 09

Re: VB Distance Calculator Challenge

Posted 29 October 2010 - 06:13 PM

I'm pretty sure CRLF won't work in a listbox. Instead of the newline, you would just add a new listitem.
Or, you could create a textbox, set it to multiline and see if that gives you the multiline result you're wanting.
Was This Post Helpful? 1
  • +
  • -

#8 CincinnatiCoralReeves  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 29-October 10

Re: VB Distance Calculator Challenge

Posted 29 October 2010 - 06:22 PM

AH!
So i need to items.add each one individually...

The assignment calls for list box, and a specific format...

I must obey.

this is the format:

Quote

Vehicle speed: 40
Time traveled: 7

Hours Distance traveled <<<<< this is used as a heading...
__________________________
1 40 |
2 80 |
3 120 |
4 160
5 200 |
6 240 |
7 280 |
8 320 |
__________________________|

This post has been edited by CincinnatiCoralReeves: 29 October 2010 - 06:28 PM

Was This Post Helpful? 0
  • +
  • -

#9 CincinnatiCoralReeves  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 29-October 10

Re: VB Distance Calculator Challenge

Posted 29 October 2010 - 06:50 PM

Welp.


It is formatted correctly, but my inputbox prompt is an infinite loop, and I am not sure how to list my ascending loop points (at one hour you have gone 40 miles) 1, 40 2,80 etc.

I know I am asking for a lot.


Thanks again.
Was This Post Helpful? 0
  • +
  • -

#10 emerald_eyes_67  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 29-October 10

Re: VB Distance Calculator Challenge

Posted 29 October 2010 - 06:51 PM

You did this assignment different than I did. how did you load the screen shot of your program. I could show you what mine looked like.

Public Class frmDistanceCalculator

    'comment: when the inputbox asking for the time comes up, 
    'there is a number 1 at the end of the line of dialogue that I cant get to go away.  


    'Programmer:    Martanna Lesley
    'Date:          10/21/10
    'Description:   Distance Calculator
    'Assignment:    Chapter 5 Programming Challenge 2

    Private Sub frmDistanceCalculator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

        'Close the program
        Dim button As DialogResult
        button = MessageBox.Show("Do you want to exit?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If button = Windows.Forms.DialogResult.Yes Then
            Me.Close()
        End If
    End Sub

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

        'speed of the vehicle in MPH
        Dim intSpeed As Integer
        Dim strSpeed As String

        'Time in hours
        Dim intTime As Integer
        Dim strTime As String

        'distance
        Dim intTotal As Integer

        'user input
        Dim strInput As String

        'counter variable 
        Dim intCount As Integer = 1

        'output
        Dim strDistance As String


        'prompt the user to enter speed
        strInput = InputBox("What is the speed in miles-per-hour?", "Enter Speed")
        'Convert the input to a decimal
        If Decimal.TryParse(strInput, intSpeed) Then

            'prompt the user to enter hours
            strInput = InputBox("What is the amount of time in hours?" & intCount.ToString(), "Enter Time")

            'Convert the input to a decimal
            If Decimal.TryParse(strInput, intTime) Then

                'create a string to display the speed and time
                strSpeed = "Vehicle Speed: " & intSpeed.ToString() & " MPH"
                strTime = "Time Traveled: " & intTime.ToString() & " hours"

                'insert speed and time to list box
                lstCalculations.Items.Insert(0, strSpeed)
                lstCalculations.Items.Insert(1, strTime)

                'create a heading
                lstCalculations.Items.Insert(2, "Hours" & "     " & "Distance Traveled")

                'insert a line
                lstCalculations.Items.Insert(3, "______________________")



                'Calculate the distance
                Do Until intCount > intTime
                    intTotal = intSpeed * intCount

                    'create a string to display the distance
                    strDistance = intCount.ToString() & "             " & intTotal.ToString()


                    'add the string to the list box
                    lstCalculations.Items.Add(strDistance)

                    'add 1 to the counter
                    intCount += 1
                Loop

                'add total distance
                lstCalculations.Items.Add("Total Distance: " & intSpeed * intTime)

            End If

        End If


    End Sub
End Class


Was This Post Helpful? 1
  • +
  • -

#11 CincinnatiCoralReeves  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 29-October 10

Re: VB Distance Calculator Challenge

Posted 29 October 2010 - 07:23 PM

Well I didn't use a screen shot...I was asking if i should?
You can capture the image of your screen using print screen key...you know this right?
I am not sure if that is what you were asking?

This stuff takes me so long...
Was This Post Helpful? 0
  • +
  • -

#12 emerald_eyes_67  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 29-October 10

Re: VB Distance Calculator Challenge

Posted 29 October 2010 - 07:44 PM

you posted a the format, and I was going to post what my program looked like when the distance was calculated.

I posted the code I used for that assignment, but thought it might help if you saw what it looked like when executed.
Was This Post Helpful? 0
  • +
  • -

#13 CincinnatiCoralReeves  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 29-October 10

Re: VB Distance Calculator Challenge

Posted 29 October 2010 - 08:15 PM

I actually figured it out...





Finally!


For the format "drawing" i just used the bb quote code and underscores and "|"


Like ASCII art
LOL




if you want to Post an image in the forum window


use "URL
For example,
this is my finished program

*************************************************
Posted Image



THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU
THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1