+ 1

[SOLVED] Get that discount

im currently attempting the get the discount challenge. after a day or so of attempts i think im close but ive hit a snag. the challenge is part of the methods section and reads: A store is running a promotion. If the total purchase price is equal to or exceeds 10000, the price will be discounted by 20% The program you are given takes the total purchase price as as input. Complete the given method to take the total purchase price as an argument, and calculate and return the discounted price if the campaigns requirement is satisfied. The method should return the same price if the discount is not given. Sample input 13000 Sample output 10400 Explanation 13000 > 10000, so the discount is applied: 13000 - (0.2*13000) = 10400 Hint you need to use if else statements inside the method. Dont forget to mention the value type for the parameter before its name. Notice that method returns a value so you will need to the the Console.WriteLine() method to execute the output. the unedited template code can be found here: https://code.sololearn.com/cGbtHmzaV3Y4 my current version here: https://code.sololearn.com/c9a134a18a28 the issue i seem to be having is passing the input value into the method. any help would be appreciated. thank you.

6th May 2021, 5:53 AM
Andrew Hall
Andrew Hall - avatar
12 odpowiedzi
+ 4
Here's what drives me absolutely crazy about Sololearn. Over and over again, the challenges involve using techniques that the training never prepared us for. In the modules before this challenge, we learned how to write a simple method. But there was no lesson on how to properly incorporate complex multi-line if-else statements into a method. Instead of puzzling out the answer, and probably failing a few times, a lot of us spend way too much time and effort trying to get to the answer ourselves, but the problem is we don't have the correct tools (yet) to solve the puzzle. Usually, with a great amount of time, and referring to various modules and google searches for guidance, we eventually get to it, but this is terribly inefficient and frustrating.
18th Nov 2021, 4:03 PM
Jesse Clark
Jesse Clark - avatar
+ 1
1. Your method doesn't return, so mark your method return type void. 2. You didn't pass the input to your method. Use Discount(totalPrice);
6th May 2021, 6:14 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
for anyone doing this task in future, i solved it today sticking within the parameters of the challenge with the code I've saved under "my current version" in the original post. Thanx for the help Carrie.
8th May 2021, 5:22 PM
Andrew Hall
Andrew Hall - avatar
+ 1
I think you need to declare parameters inside your method parentheses. Try static int Discount(int totalPrice)
5th Aug 2021, 5:32 AM
Andrew Hall
Andrew Hall - avatar
+ 1
Hey, I checked your solution and I'm wondering what does the (int) after the return and why it doesn't work without it. (Googled it and couldn't find anything). Also, why did you declared the result variable before the if instead of using totalPrice? Thanks in advance!
28th Aug 2021, 9:38 PM
Francisco
+ 1
Yes, youre right jesse. Sometimes sololearn is exactly as you describe it. However, this isnt as bad a thing as it initially seems, because it actually teaches a very valuable skill in programming - it forces you to imorove your google fu. The ability to find answers on search engines, which is a skill used a lot in programming.
27th Feb 2022, 9:00 AM
Andrew Hall
Andrew Hall - avatar
+ 1
Francisco without the (int) the compiler outputs the following error: “./Playground/file0.cs(17,13): error CS0266: Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)” I think what that means is that the complier cant deal with the difference between doubles (from the 0.8 in my function) and integers without being told to convert the value to an integer. Im not sure what you mean in the second part of your question, but it occured to me maybe my variable names might be a bit confusing so ive changed them slightly and what was totalPrice, is now initialPrice, and result is now totalPrice. Hipefully that makes it a little easier to read.
27th Feb 2022, 11:15 AM
Andrew Hall
Andrew Hall - avatar
0
thanx for your answer carrie. i think the method actually IS supposed to return a value (unless im misunderstanding the term return here) i dont think the challenge is expecting me to change the method declartion beyond adding parameters. at this point in the syllabus, i dont think the void keyword has been covered yet. does this mean i need to use the return keyword somewhere in my method body? ill have to go have a look at how to incorporate that into the code ive written. thanx again for your input.
6th May 2021, 9:25 AM
Andrew Hall
Andrew Hall - avatar
0
Andrew Hall yes, the challenge expects that, but the fact is that you don't have to follow. If you want to do it in the expected way, it's basically like: int totalPrice = Convert.ToInt32(Console.ReadLine()); int result = Discount(totalPrice); Console.WriteLine(result); Whereas Discount() returns the discounted price. This is considered a better practice because you can use the discounted price for the later code without having to put everything in Discount(). But the fact is the goal of the challenge doesn't need to use the discounted price for further code and is simple enough. It doesn't really matter. Moreover, you can do this in ONE line. Console.WriteLine(Convert.ToInt32(Console.ReadLine())*0.8); Though, it's more unreadable compare to your code.
6th May 2021, 10:03 AM
你知道規則,我也是
你知道規則,我也是 - avatar
0
Hi guys, can you help me with this code, what am I missing here? I had a feeling that I'm on the right path, but got many errors. namespace SoloLearn { class Program { static void Main(string[] args) { int totalPrice = Convert.ToInt32(Console.ReadLine()); //call the method Discount; } //complete the method declaration static int Discount() { //complete the method body if (totalPrice >= 10000) { totalPrice = totalPrice -(0.2*totalPrice); Console.WriteLine(totalPrice); }else { Console.WriteLine(totalPrice); } } } }
3rd Aug 2021, 11:26 PM
Ivan
Ivan - avatar
0
If you want to get latest deal and coupon codes, you can visit this website: https://freecouponsaz.com/
14th Jun 2024, 6:40 AM
L Janice
L Janice - avatar
- 1
Possible, thanks for advice. I'll certainly have to go back to some earlier lessons and then get back to this task.
5th Aug 2021, 6:48 PM
Ivan
Ivan - avatar