11 Replies - 981 Views - Last Post: 25 August 2013 - 09:42 AM Rate Topic: -----

#1 vendicore  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-August 13

Tips about floating points while add

Posted 15 August 2013 - 03:45 PM

Hello,this is my first post and this is my first time programming
i have run into some problems with how to make this one work.

The FloatingNumbersWhileAdd Class (while-statement) "The name "

In this section, the program lets the user feed in values that can be real numbers or integers.

The program does not require the user to specify the number of values to be read. "How do i tell the program this?"

It stops reading when the user writes a zero value." I cant get this to work?"

A while statement is the best choice when the number of iterations is not known. "I cant get it to stop? it just continues in an endless loop".

There are two important requirements.

1:Use double data type for the numeric values.
2:Use a while statement to perform the iteration.

Attached Image "This is the code i had from the beginning."
Attached Image "This is what it should do."

Now i know that the code i have now is a mess and i need to clean it up but at this point im just testing as much as i can and i feel like my head is about to explode.

Hope anyone have some ideas on what i am doing wrong
/vendi

Public Class FloatingpointsNumberWhileAdd
    Dim done As Boolean = False
    Dim index As Double = 0
    Dim endNumber As Double = 0
    
    Private summation As Double
 

    Public Sub start()
        writeprograminfo()
        readvaluesandsumnumbers()
        writeresult()

    End Sub
    Private Sub writeprograminfo()
        Console.WriteLine("To finish input write a zero. ")
        Console.Write("enter double: ")

        index = Integer.Parse(Console.ReadLine())

    End Sub
    Private Sub readvaluesandsumnumbers()


        input.readDoubleConsole()
        input.readDoubleConsole()
        While Not done
            If Not done Then Console.Write("enter double")

        End While
     
   End Sub
    Private Sub writeresult()
        summation = index + index
        Console.Write("summation: " & summation)

    End Sub

End Class


Is This A Good Question/Topic? 0
  • +

Replies To: Tips about floating points while add

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Tips about floating points while add

Posted 15 August 2013 - 04:01 PM

You are asking for two numbers at a time, not doing anything with them, not checking for zero, and not setting the variable done to True.
Was This Post Helpful? 0
  • +
  • -

#3 vendicore  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-August 13

Re: Tips about floating points while add

Posted 16 August 2013 - 06:26 AM

Hello and thank you for your answer.
i have changed the code a bit but it still dont do it correctly.

It does not end the question "enter double" when i press zero "0" and i dont know how i sum up the integers they are unknown i mean, i can as user put in as many numbers i want until i press "0"

any more hints?
best regards /vendi


Public Class FloatingpointsNumberWhileAdd
    Private summation As Double
    Dim index As Double
    Public Sub start()
        writeprograminfo()
        readvaluesandsumnumbers()
        writeresult()
    End Sub
    Private Sub writeprograminfo()
        Console.WriteLine("To finish input write a zero. ")
        Console.Write("enter double: ")
        index = Console.ReadLine
    End Sub
    Private Sub readvaluesandsumnumbers()
        Dim done As Boolean = False
        summation = 0
        While Not done
            If done = False Then
                Console.Write("enter double: ")
                index = Console.ReadLine
            ElseIf done = True Then
                Exit While
            ElseIf done = True Then
                summation()

            End If
            'If index >= 1 And index <= 1 Then
            'Continue While
            ' If done = 0 Then
            'Exit While
            'End If


        End While
    End Sub
    Private Sub writeresult()

        Console.Write("summation: " & summation)
    End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

#4 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Tips about floating points while add

Posted 16 August 2013 - 09:31 AM

Have a look at this code. It's partly what you supplied, and partly just an explanation of what needs to be done. If you have any further questions, or are wondering why some of the code was eliminated, please feel free to ask.


Public Class FloatingpointsNumberWhileAdd
    Private Sub readvaluesandsumnumbers()
        Dim done As Boolean = False

' The following line will reset summation to zero, which will cause
' you to end up with only the last number entered in summation.
' Summation is initialized to zero already, in the declaration
        summation = 0

        While Not done

' The folowing line is redundant. Your code would not be executing
' this statement if done is False, so you can remove it
            'If done = False Then

' Here, you are asking for input, but you already asked for input
' before calling this routine. The previous one can be removed.
                Console.Write("enter double: ")
                index = Console.ReadLine

' At this point, you should have an input, so what you want to do is
' to add the input to summation. Then you want to check to see if the
' input = 0. If so, set done to True and put in an End While. 
' This is the basic operation. Later, you might want to think about
' what happens if a user enter something that is not a double, like "Fred",
' Or "help!"
        End While
    End Sub


Was This Post Helpful? 0
  • +
  • -

#5 vendicore  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-August 13

Re: Tips about floating points while add

Posted 17 August 2013 - 01:54 PM

Ok.. so i have changed the bits u told me about and also added some bits and well it seems to be working..
I want to thank u for the help and the hints u gave me . thank u m8.
I have no idea on how to enter the code if someone writes help or some other wrong input.
Here is the new code


Public Class FloatingpointsNumberWhileAdd
    Private summation As Double
    Dim index As Double
    Dim tal1 As Double
    Public Sub start()
        writeprograminfo()
        readvaluesandsumnumbers()
        writeresult()
    End Sub
    Private Sub writeprograminfo()
        Console.WriteLine("To finish input write a zero. ")
    End Sub
    Private Sub readvaluesandsumnumbers()
        Dim done As Boolean = False
        summation = 0
        tal1 = 0
        While Not done
            Console.Write("enter double: ")
            index = Console.ReadLine
            tal1 = tal1 + index
            If done = True Then
                Exit Sub
            End If
            If index = 0 Then done = True
        End While
    End Sub
    Private Sub writeresult()
        summation = index + tal1
        Console.Write("summation: " & summation)
    End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,589
  • Joined: 12-December 12

Re: Tips about floating points while add

Posted 17 August 2013 - 03:30 PM

Quote

I have no idea on how to enter the code if someone writes help or some other wrong input.

Are you asking for more help? If so, then you'll need to state your question more clearly.

However, if you "have no idea" then you need to continue studying and, if necessary, speak to your tutor for guidance.
Was This Post Helpful? 0
  • +
  • -

#7 vendicore  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-August 13

Re: Tips about floating points while add

Posted 17 August 2013 - 03:46 PM

Hi again.
yes i HAD no idea but i actually found something in a book called visual basic.net (in a nutshell).
A good example that seems to be working.
also added an if statement that compares 2 floating point numbers in 7 decimal position.


My question now is is this code good or is it sloppy?

Option Explicit On
Option Strict On

Public Class FloatingpointsNumberWhileAdd
    Dim summation As Double
    
    Public Sub start()
        writeprograminfo()
        readvaluesandsumnumbers()
        writeresult()
    End Sub
    Private Sub writeprograminfo()
        Console.WriteLine("To finish input, write a zero. ")
    End Sub
    Private Sub readvaluesandsumnumbers()
        Dim done As Boolean = False
        Dim numbersDbl As Integer = 0
        Dim ReadDblValue As Double = 0.0
        Dim srtInput As String
        summation = 0
        While Not (done)
            If (numbersDbl <= 0) Then
                done = False
                If ((Math.Round(numbersDbl, 7) = 0.0)) Then
Top:
                    Console.Write("Enter Double: ")
                    srtInput = Console.ReadLine
                    Try
                        ReadDblValue = Convert.ToDouble(srtInput)
                    Catch ex As Exception
                        Console.WriteLine("That is not a valid double try again...")
                        GoTo Top
                    End Try
                    summation += ReadDblValue
                End If
            Else
            End If
            If ReadDblValue = 0 Then done = True
        End While
    End Sub
    Private Sub writeresult()

        Console.Write("summation: " & summation)
    End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,589
  • Joined: 12-December 12

Re: Tips about floating points while add

Posted 17 August 2013 - 04:04 PM

It looks reasonable to me :) for a beginner.

However

  • Use capitals: change writeprograminfo() to WriteProgramInfo(), etc..
  • Separate each Sub with a blank line - the code is difficult to read without these separators
  • Get rid of the Goto :helpsmilie: and its label. GoTo is not necessary and is a poor practice. (I hope your teacher didn't encourage this practice?!) Using TryParse, and another loop, is one way to avoid this.

This post has been edited by andrewsw: 17 August 2013 - 04:07 PM

Was This Post Helpful? 0
  • +
  • -

#9 vendicore  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-August 13

Re: Tips about floating points while add

Posted 18 August 2013 - 04:23 AM

Hello and thanks for all the help u guys have provided.

I have been in contact with my teacher and i did not ask him about "GoTo" or "TryParse", but the loop shall be "WHILE". the next part of the assignment "DO WHILE".

So why is "GoTo" bad practice? seems perfectly resonable to me ;).

TryParse i dont get how i shall use that in the code instead of GoTo, what i can see they dont do the same thing? or do they?.

I must also say i have never ever before tryed my hand at any sort of programing, and english is not my naitive language so please bare with me if i dont spell correctly.

I have changed the code a bit again just to make it look better by your suggestion(it looks better).
i have not changed GoTo part .


Option Explicit On
Option Strict On

Public Class FloatingpointsNumberWhileAdd

    Dim Summation As Double

    Public Sub Start()

        WriteProgramInfo()
        ReadValuesAndSumNumbers()
        WriteResult()

    End Sub
    Private Sub WriteProgramInfo()

        Console.WriteLine("********Calculate Floating Points******** ")
        Console.WriteLine("")
        Console.WriteLine("To finish input, write a zero. ")
        Console.WriteLine("")

    End Sub
    Private Sub ReadValuesAndSumNumbers()

        Dim done As Boolean = False
        Dim numbersDbl As Integer = 0
        Dim ReadDblValue As Double = 0.0
        Dim srtInput As String
        Summation = 0
        While Not (done)
            If (numbersDbl <= 0) Then
                done = False
                If ((Math.Round(numbersDbl, 7) = 0.0)) Then
Top:
                    Console.Write("Enter Double: ")
                    srtInput = Console.ReadLine
                    Try
                        ReadDblValue = Convert.ToDouble(srtInput)
                    Catch ex As Exception
                        Console.WriteLine("That is not a valid double. Please try again...")
                        GoTo Top
                    End Try
                    Summation += ReadDblValue
                End If
            Else

            End If
            If ReadDblValue = 0 Then done = True

        End While

    End Sub
    Private Sub WriteResult()

        Console.Write("summation: " & Summation)

    End Sub

End Class

Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,589
  • Joined: 12-December 12

Re: Tips about floating points while add

Posted 18 August 2013 - 07:29 AM

TryParse on its own is not a replacement for GoTo, but using it you could construct another loop that would repeatedly ask for a Double, and not require the error handling code.

Anyway, there are a number of ways that the loop can be created.

Dim blnIsDouble As Boolean = False
Do
    'get a value and check if it is a Double
    'set blnIsDouble to True or False
Loop Until blnIsDouble

Overuse, abuse and misuse, of GoTo can lead to hard to understand code and can make error-tracing more difficult. However, at your early stage you don't need to be concerned, I'm just surprised that your tutor might be encouraging its use.

Anyway, if you have a few hours to spare :online2long:, and are interested enough, you could read this extended SO discussion on GoTo. It goes on to a second page, which is rare at SO.

Yes, the code looks a lot more legible now, although.. I meant to have a blank line after each End Sub so that one Sub does not abut on another. You can have a blank line before End Sub as well if you want - I often do.

Note that these blank lines are a matter of personal taste and habit, although I think having a blank line between each End Sub/Sub is sensible.

This post has been edited by andrewsw: 18 August 2013 - 07:36 AM

Was This Post Helpful? 0
  • +
  • -

#11 vendicore  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-August 13

Re: Tips about floating points while add

Posted 18 August 2013 - 08:14 AM

Hi again!

Well I really dont know if my teacher say its ok to use GoTo or not, doesent say not to use it anywhere in the assignment.

I continued on to the next part in the assignment and in that part its "do while" there are more examples to find on that one.

In this part it should work the same but after the summation it asks what currency I want to exchange to and later what the exchange rate is and finally a new summation. "it should look kinda like this"
"summation"= 21,1 "converted to (euro, us , etc) = (amount "summation * exchangerate") "symbol" for the currency i used
example :21,1 converted to eur = 192,01 E


my problem on this one is that i cant get the summation code on the right place i want it in "Private Sub ForeingAmount()"
but it dont want to summerize . but if i have the code in "Private Sub ExchangeRate()" then it works WHY??

The other problem is i want after the summation i want to have the correct symbol for the currencys is that possible?

Here is the code:
Option Explicit On
Option Strict On

Public Class Currencyconverterdowhile

    Dim InputExchangeRate As Double = 0
    Dim Summation As Double = 0
    Dim ReadDblValue As Double = 0.0
    Dim InputCurrencyName As String

    Public Sub Start()

        WriteProgramInfo()
        ReadValuesAndSumNumbers()
        CurrencyName()
        ExchangeRate()
        ForeingAmount()

    End Sub

    Private Sub WriteProgramInfo()

        Console.WriteLine()
        Console.WriteLine("******** Currency Converter ********")
        Console.WriteLine()
        Console.WriteLine("To finish input, write a zero.")
        Console.WriteLine()

    End Sub

    Private Sub ReadValuesAndSumNumbers()

        Dim done As Boolean = False
        Dim numbersDbl As Integer = 0
        Dim srtInput As String
        summation = 0
        Do While (Not done)
            If ((Math.Round(numbersDbl, 7) = 0.0)) Then
top:
                Console.Write("Enter a double value: ")
                srtInput =
    Console.ReadLine
                Try
                    ReadDblValue =
    Convert.ToDouble(srtInput)
                Catch ex As Exception
                    Console.WriteLine("Please try again itīs not a valid double value")
                    GoTo Top 'This prevent the stored values that etc strings have to interfere with the summation.
                End Try
            End If
            If ReadDblValue = 0 Then
                done =
                True
            End If
            Summation += ReadDblValue
        Loop
        Console.WriteLine()
        Console.WriteLine("Summation: " & summation)
        Console.WriteLine()

    End Sub


    Private Sub CurrencyName()

        Console.Write("What is the name of the currency?: ")
        InputCurrencyName =
        Console.ReadLine()
        Console.ReadLine()
        Console.WriteLine("The name of the currency is " & InputCurrencyName)
        Console.ReadLine()

    End Sub

    Private Sub ExchangeRate()

        Dim InputExchangeRate As Double = 0
        Console.Write("Enter the exchange rate: ")
        InputExchangeRate =
        Convert.ToDouble(Console.ReadLine)

        Console.Write(Summation & " is converted to " & InputCurrencyName & (" = " & Summation * InputExchangeRate))

    End Sub

    Private Sub ForeingAmount()

        'Console.Write(summation & " Is converted to " & InputCurrencyName & (" = " & summation * InputExchangeRate))
        ' summation = ReadDblValue * InputExchangeRate
        'Console.WriteLine(summation * InputExchangeRate)

    End Sub

End Class

Was This Post Helpful? 0
  • +
  • -

#12 vendicore  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-August 13

Re: Tips about floating points while add

Posted 25 August 2013 - 09:42 AM

i actually got it to work.. tnx guys..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1