C# Help

H.Cornerstone

CAGiversary!
Feedback
1 (100%)
If anyone with C# could help me on this problem, It would be awesome. I have to create a program that will accept a input value and then use if else statements to calculate the total amount based on discounts depending on the input value.

class Program
{
static void Main()
{
int purchAmount;
int discount;
InputValues(out purchAmount);
discount = DetermineDiscount(purchAmount);
PrintResults(discount, purchAmount);

}
public static void InputValues(out int purch)
{
string inputValue;
Console.WriteLine("Please Enter Purchased Amount");
// prompt user to enter purchased amount
inputValue = Console.ReadLine();
purch = Convert.ToInt32(inputValue);
}

public static int DetermineDiscount(int purchAmount)
{
int discount;

if (purchAmount > 200)
{
discount = 20;
}
else if (purchAmount > 150)
{
discount = 15;
}
else if (purchAmount > 100)
{
discount = 10;
}
else if (purchAmount > 50)
{
discount = 5;
}
else if (purchAmount > 0)
{
discount = 0;
}
else
{
Console.WriteLine("The entered purchase amount is negative. It's discount amount cannot be computed.");
}
return discount;

}
public static void PrintResults(int discount, int purchAmount)
{
Console.WriteLine("The Discount is: " + discount);
Console.WriteLine("The Total Amount is: " + (purchAmount - DetermineDiscount(purchAmount)));
Console.ReadLine();
}


}
}
 
...................


Err.....

img2085hn7.jpg
 
I've not coded in C# myself, but in other C languages. It's been a while, but I don't ever remember an "else if" statement. It was always an "if" and then "else" which you can reiterate as many times as you need.

It's pretty hard to read the code, especially on a forum, but make sure the open/close brackets correspond and that the "if/else" brackets are nested within each other. I don't know how the problem you're trying to solve is worded, but with your logic, the even amounts (200, 150, 100, etc.) are ignored so you may want to change your condition statements to "PurchaseAmount >=150".
 
[quote name='H.Cornerstone']Says use of unassigned local variable discount with the return discount in the determine discount.[/quote]Check the variables you're referencing between your main function and the functions you're calling.

Edit: I think you have to reference the "discount" variable in your determine discount function since "discount" is created in the main function.
 
[quote name='SteveMcQ']Check the variables you're referencing between your main function and the functions you're calling.

Edit: I think you have to reference the "discount" variable in your determine discount function since "discount" is created in the main function.[/quote]

I posted it on neogaf and they helped me figure it out. Apperently, you have to assign Discount = 0.
 
[quote name='H.Cornerstone']I posted it on neogaf and they helped me figure it out. Apperently, you have to assign Discount = 0.[/quote]Out of curiosity, why is that? Didn't you already initialize Discount to be zero?
 
I don't see an initial value for discount, so yeah it would need an initial value.

Also, yes "else if" statements do exist in C#.

Something like:

If this = that
then do this
else if this = something else
then do this

And so on like in his code.
 
[quote name='SteveMcQ']Out of curiosity, why is that? Didn't you already initialize Discount to be zero?[/quote]


A. I have absolutely no freakin idea. Thats what some guy said, I did it, now it works.

B. No, I never set it to zero. Just the fact that it's an integer.
 
[quote name='JolietJake']Just out of curiosity, what book are you using? I just finished my C# class last semester.[/quote]

C# Programming: From Problem Analysis to Program Design by Barbara Doyle.
 
[quote name='H.Cornerstone']A. I have absolutely no freakin idea. Thats what some guy said, I did it, now it works.

B. No, I never set it to zero. Just the fact that it's an integer.[/quote]

Well, you have to assign it, because it's possible that you return never hitting any of the assignments. C# doesn't like it if the only assignment is part of an if/else block, because for all it knows, you fucked up and it will never get assigned.
 
bread's done
Back
Top