Calculations Help!
-
I am writing a code for my CIS-115 class. I have all of the information that I need in it (I think!), but I cannot get ALL of my calculations to execute correctly; only 2 of 5 come out right. Am I allowed to post it here to get help?
-
I am writing a code for my CIS-115 class. I have all of the information that I need in it (I think!), but I cannot get ALL of my calculations to execute correctly; only 2 of 5 come out right. Am I allowed to post it here to get help?
Yes, but don't ask for anyone to write your code for you. That's the restriction.
A guide to posting questions on CodeProject
How to debug small programs
Dave Kreskowiak -
Yes, but don't ask for anyone to write your code for you. That's the restriction.
A guide to posting questions on CodeProject
How to debug small programs
Dave KreskowiakYou will need to design an application that will receive the customer type and its units of utility used for the billing duration. The application will calculate and display the cost per unit based on the customer type and the total charges for that billing cycle. Calculate the charges using the following data. Customer Type Units Consumed Cost per unit ($) Commercial <= 1,000 $0.50 per unit Commercial 1,000 < $0.50 per unit Industrial < 800 $0.65 per unit Industrial 800 <= 2,000 $0.55 per unit Industrial 2,000 < $0.50 per unit Residential < 500 $0.85 per unit Residential 500 <= $0.75 per unit Make sure that the units consumed entered is a positive number and the customer Type is valid; otherwise your program should display an error message and ask for the data to be entered again until the user enters valid data. Test your algorithm with the following three sets of data. Test case 1: Units used of 799, Customer Type is Industrial Test case 2: Units used 500, Customer Type is Residential Test case 3: Units used 800, Customer Type is Industrial Test case 4: Units used 1000, Customer Type is Commercial Test case 5: Units used 499, Customer Type is Residential
-
You will need to design an application that will receive the customer type and its units of utility used for the billing duration. The application will calculate and display the cost per unit based on the customer type and the total charges for that billing cycle. Calculate the charges using the following data. Customer Type Units Consumed Cost per unit ($) Commercial <= 1,000 $0.50 per unit Commercial 1,000 < $0.50 per unit Industrial < 800 $0.65 per unit Industrial 800 <= 2,000 $0.55 per unit Industrial 2,000 < $0.50 per unit Residential < 500 $0.85 per unit Residential 500 <= $0.75 per unit Make sure that the units consumed entered is a positive number and the customer Type is valid; otherwise your program should display an error message and ask for the data to be entered again until the user enters valid data. Test your algorithm with the following three sets of data. Test case 1: Units used of 799, Customer Type is Industrial Test case 2: Units used 500, Customer Type is Residential Test case 3: Units used 800, Customer Type is Industrial Test case 4: Units used 1000, Customer Type is Commercial Test case 5: Units used 499, Customer Type is Residential
Sub Main()
Dim customerType As String
Dim unitsConsumed As Decimal = 0
Dim costPerUnit As Decimal = 0
Dim totalCharges As Decimal = 0
Do
Console.Write("Enter the customer type as commercial, industrial, or residential: ")
customerType = Console.ReadLine()
While customerType <> "commercial" And customerType <> "industrial" And customerType <> "residential"
Console.WriteLine("ERROR - Enter commerical, industrial, or residential.")
Console.Write("Enter the customer type as commercial, industrial, or residential: ")
customerType = Console.ReadLine()
End While
Console.Write("Enter the units consumed: ")
unitsConsumed = Console.ReadLine()
While unitsConsumed < 0
Console.WriteLine("ERROR - Please enter a value greater than zero.")
Console.Write("Enter the units consumed. ")
unitsConsumed = Console.ReadLine()
End While
If unitsConsumed <= 1000 Then
costPerUnit = 0.5
If unitsConsumed < 1000 Then
costPerUnit = 0.5
If unitsConsumed < 800 Then
costPerUnit = 0.65
If (unitsConsumed > 800) And (unitsConsumed <= 2000) Then
costPerUnit = 0.55
If unitsConsumed > 2000 Then
costPerUnit = 0.5
If unitsConsumed < 500 Then
costPerUnit = 0.85
If unitsConsumed >= 500 Then
costPerUnit = 0.75
End If
End If
End If
End If
End If
End If
End If
totalCharges = unitsConsumed * costPerUnit
Console.WriteLine("The total charges are: " & totalCharges)
Console.ReadLine()
Loop
End Sub -
Sub Main()
Dim customerType As String
Dim unitsConsumed As Decimal = 0
Dim costPerUnit As Decimal = 0
Dim totalCharges As Decimal = 0
Do
Console.Write("Enter the customer type as commercial, industrial, or residential: ")
customerType = Console.ReadLine()
While customerType <> "commercial" And customerType <> "industrial" And customerType <> "residential"
Console.WriteLine("ERROR - Enter commerical, industrial, or residential.")
Console.Write("Enter the customer type as commercial, industrial, or residential: ")
customerType = Console.ReadLine()
End While
Console.Write("Enter the units consumed: ")
unitsConsumed = Console.ReadLine()
While unitsConsumed < 0
Console.WriteLine("ERROR - Please enter a value greater than zero.")
Console.Write("Enter the units consumed. ")
unitsConsumed = Console.ReadLine()
End While
If unitsConsumed <= 1000 Then
costPerUnit = 0.5
If unitsConsumed < 1000 Then
costPerUnit = 0.5
If unitsConsumed < 800 Then
costPerUnit = 0.65
If (unitsConsumed > 800) And (unitsConsumed <= 2000) Then
costPerUnit = 0.55
If unitsConsumed > 2000 Then
costPerUnit = 0.5
If unitsConsumed < 500 Then
costPerUnit = 0.85
If unitsConsumed >= 500 Then
costPerUnit = 0.75
End If
End If
End If
End If
End If
End If
End If
totalCharges = unitsConsumed * costPerUnit
Console.WriteLine("The total charges are: " & totalCharges)
Console.ReadLine()
Loop
End SubThis is the code that I have, but only Test Case 1 and 4 execute correctly. I have sat here for hours looking through my book and lectures, but I'm just blind to my problem. Thank you, in advance, if you are able to help me!
-
Sub Main()
Dim customerType As String
Dim unitsConsumed As Decimal = 0
Dim costPerUnit As Decimal = 0
Dim totalCharges As Decimal = 0
Do
Console.Write("Enter the customer type as commercial, industrial, or residential: ")
customerType = Console.ReadLine()
While customerType <> "commercial" And customerType <> "industrial" And customerType <> "residential"
Console.WriteLine("ERROR - Enter commerical, industrial, or residential.")
Console.Write("Enter the customer type as commercial, industrial, or residential: ")
customerType = Console.ReadLine()
End While
Console.Write("Enter the units consumed: ")
unitsConsumed = Console.ReadLine()
While unitsConsumed < 0
Console.WriteLine("ERROR - Please enter a value greater than zero.")
Console.Write("Enter the units consumed. ")
unitsConsumed = Console.ReadLine()
End While
If unitsConsumed <= 1000 Then
costPerUnit = 0.5
If unitsConsumed < 1000 Then
costPerUnit = 0.5
If unitsConsumed < 800 Then
costPerUnit = 0.65
If (unitsConsumed > 800) And (unitsConsumed <= 2000) Then
costPerUnit = 0.55
If unitsConsumed > 2000 Then
costPerUnit = 0.5
If unitsConsumed < 500 Then
costPerUnit = 0.85
If unitsConsumed >= 500 Then
costPerUnit = 0.75
End If
End If
End If
End If
End If
End If
End If
totalCharges = unitsConsumed * costPerUnit
Console.WriteLine("The total charges are: " & totalCharges)
Console.ReadLine()
Loop
End SubLook at your nested if statement 100 meets these criteria 1000, 1000, 800 500 so which value will be returned, the first of course.
Never underestimate the power of human stupidity RAH
-
Sub Main()
Dim customerType As String
Dim unitsConsumed As Decimal = 0
Dim costPerUnit As Decimal = 0
Dim totalCharges As Decimal = 0
Do
Console.Write("Enter the customer type as commercial, industrial, or residential: ")
customerType = Console.ReadLine()
While customerType <> "commercial" And customerType <> "industrial" And customerType <> "residential"
Console.WriteLine("ERROR - Enter commerical, industrial, or residential.")
Console.Write("Enter the customer type as commercial, industrial, or residential: ")
customerType = Console.ReadLine()
End While
Console.Write("Enter the units consumed: ")
unitsConsumed = Console.ReadLine()
While unitsConsumed < 0
Console.WriteLine("ERROR - Please enter a value greater than zero.")
Console.Write("Enter the units consumed. ")
unitsConsumed = Console.ReadLine()
End While
If unitsConsumed <= 1000 Then
costPerUnit = 0.5
If unitsConsumed < 1000 Then
costPerUnit = 0.5
If unitsConsumed < 800 Then
costPerUnit = 0.65
If (unitsConsumed > 800) And (unitsConsumed <= 2000) Then
costPerUnit = 0.55
If unitsConsumed > 2000 Then
costPerUnit = 0.5
If unitsConsumed < 500 Then
costPerUnit = 0.85
If unitsConsumed >= 500 Then
costPerUnit = 0.75
End If
End If
End If
End If
End If
End If
End If
totalCharges = unitsConsumed * costPerUnit
Console.WriteLine("The total charges are: " & totalCharges)
Console.ReadLine()
Loop
End SubAnother day, another hint.
If x < 1000 Then
If x < 800 Then
If x > 2000 Then
' when does this line get evaluated?
' A: if x < 1000 AND x < 800 AND x > 2000
End If
End If
End IfFind me a number smaller than 1000, but larger than 2000. There's another problem, take a look at the example-rules;
Member 10728667 wrote:
Commercial <= 1,000 $0.50 per unit Commercial 1,000 < $0.50 per unit
If think the first price is for commercial stuff if you buy 1000 items or less, and the second price would be if you buy MORE than 1000 items. That's prolly what the position of the operator should imply. Back to the code;
If unitsConsumed <= 1000 Then
costPerUnit = 0.5
If unitsConsumed > 1000 ThenNow that I switched the operator to what it probably should be, does it help to diagnose the error?
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
-
Sub Main()
Dim customerType As String
Dim unitsConsumed As Decimal = 0
Dim costPerUnit As Decimal = 0
Dim totalCharges As Decimal = 0
Do
Console.Write("Enter the customer type as commercial, industrial, or residential: ")
customerType = Console.ReadLine()
While customerType <> "commercial" And customerType <> "industrial" And customerType <> "residential"
Console.WriteLine("ERROR - Enter commerical, industrial, or residential.")
Console.Write("Enter the customer type as commercial, industrial, or residential: ")
customerType = Console.ReadLine()
End While
Console.Write("Enter the units consumed: ")
unitsConsumed = Console.ReadLine()
While unitsConsumed < 0
Console.WriteLine("ERROR - Please enter a value greater than zero.")
Console.Write("Enter the units consumed. ")
unitsConsumed = Console.ReadLine()
End While
If unitsConsumed <= 1000 Then
costPerUnit = 0.5
If unitsConsumed < 1000 Then
costPerUnit = 0.5
If unitsConsumed < 800 Then
costPerUnit = 0.65
If (unitsConsumed > 800) And (unitsConsumed <= 2000) Then
costPerUnit = 0.55
If unitsConsumed > 2000 Then
costPerUnit = 0.5
If unitsConsumed < 500 Then
costPerUnit = 0.85
If unitsConsumed >= 500 Then
costPerUnit = 0.75
End If
End If
End If
End If
End If
End If
End If
totalCharges = unitsConsumed * costPerUnit
Console.WriteLine("The total charges are: " & totalCharges)
Console.ReadLine()
Loop
End SubThe easy thing to do is put a breakpoint and then step through your code and you'll see exactly what is happening. It should be very easy to solve at that point.
There are only 10 types of people in the world, those who understand binary and those who don't.