Problem with code!

VB Program incorrect results

Page 1 of 1

3 Replies - 1984 Views - Last Post: 13 January 2010 - 03:51 AM Rate Topic: -----

#1 bvelez78  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-January 10

Problem with code!

Post icon  Posted 12 January 2010 - 09:33 PM

Here is my code and I have tried to fix it and got a lot of help but I have not been able to fix it. I am going to post my code and hope someone can fix what is wrong and post the working code. This is what the program should do"Write a program to estimate how much a young worker will make before retiring at age 65. Request the worker's name, age, and starting salary as input. Assume the worker receives a 5 percent raise each year. For example, if the user enters Helen, 25, and 20000, then the text box should display the following:



Helen will earn about $2, 415, 995"
Thanks in advance for any help.

Public Class Form1
	Dim age As Double

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

		Dim Salary As Double = CDbl(GetSalary())
		Dim Age As Double = CDbl(GetAge())
		Dim Name As String = GetName()
		MsgBox(Salary.ToString() + " " + Age.ToString)
		Do While Age <= 65

			Dim PER As Integer = Salary * 0.05
			Salary += Salary + PER

			Age += 1
			MsgBox(Salary.ToString() + " " + Age.ToString)
		Loop

		MessageBox.Show(" " & Name & " will earn about " & FormatCurrency(Salary))
	End Sub

	Public Function GetSalary()
		Dim Salary As String = InputBox("Input your starting salary (Numeric Only)")

		If IsNumeric(Salary) Then
			Return Salary
		Else
			MessageBox.Show("Error")
			Me.Close()
		End If
	End Function
	Public Function GetAge()
		Dim Age As String = InputBox("Input your age(Numeric Only)")

		If IsNumeric(age) Then
			Return age
		Else
			MessageBox.Show("Error")
			Me.Close()
		End If
	End Function
	Public Function GetName()
		Dim Name As String = InputBox("Input your name")

		Return Name

	End Function
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with code!

#2 LeisureProgrammer  Icon User is offline

  • N00by VB.NET Developer
  • member icon

Reputation: 110
  • View blog
  • Posts: 588
  • Joined: 06-September 09

Re: Problem with code!

Posted 12 January 2010 - 09:56 PM

A for loop would be much simpler to implement... I had to simplify your code a bit to think about it, but now you can "expand" it back and add sanity check thingies if you want.


		Dim name As String = InputBox("name")
		Dim startingSalary As Double = InputBox("starting salary")
		Dim age As Integer = InputBox("age")

		Dim endSalary As Integer

		For aaa As Integer = age To 64
			endSalary += startingSalary
			startingSalary += (startingSalary * 0.05)
		Next

		Msgbox(" " & name & " will earn about " & endSalary)



So what you want to do is just eliminate all the other useless vars you have, and just focus on these four. Feel free to ask if you have any questions. Start out with replacing all the form load event with the code above, and then gradually implement your GetName() and GetSalary() functions... etc...

This post has been edited by LeisureProgrammer: 12 January 2010 - 09:59 PM

Was This Post Helpful? 0
  • +
  • -

#3 bvelez78  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-January 10

Re: Problem with code!

Posted 12 January 2010 - 11:06 PM

I am new to this but what should I do exactly? I know tha it said you wont post the code but can you? I am not a programmer but am trying to learn and received a half credit for this program. So, I do not need it for my class but am just curious what code will work. I thought I had it working and when I debugged I got no errors but never got the correct answer.




View PostLeisureProgrammer, on 12 Jan, 2010 - 08:56 PM, said:

A for loop would be much simpler to implement... I had to simplify your code a bit to think about it, but now you can "expand" it back and add sanity check thingies if you want.


		Dim name As String = InputBox("name")
		Dim startingSalary As Double = InputBox("starting salary")
		Dim age As Integer = InputBox("age")

		Dim endSalary As Integer

		For aaa As Integer = age To 64
			endSalary += startingSalary
			startingSalary += (startingSalary * 0.05)
		Next

		Msgbox(" " & name & " will earn about " & endSalary)



So what you want to do is just eliminate all the other useless vars you have, and just focus on these four. Feel free to ask if you have any questions. Start out with replacing all the form load event with the code above, and then gradually implement your GetName() and GetSalary() functions... etc...

Was This Post Helpful? 0
  • +
  • -

#4 LeisureProgrammer  Icon User is offline

  • N00by VB.NET Developer
  • member icon

Reputation: 110
  • View blog
  • Posts: 588
  • Joined: 06-September 09

Re: Problem with code!

Posted 13 January 2010 - 03:51 AM

I did post the code.... Just replace all of your code in form 1 with this one, and try it out :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1