Problem solving Final Project C# 81 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Problem solving Final Project C# 81

Hello ! I feel like I cheated in this last Project. I was stuck at the discount part of the problem. Was there any other way to apply the discount than using a cast to double ? :/ I had to look up on the web for a solution. At first, without the cast, only 0 was return for all the prices. Which makes sense to me because it's all int. But I would never had found otherwise, I think. namespace SoloLearn { class Program { static void Main(string[] args) { int discount = Convert.ToInt32(Console.ReadLine()); Dictionary<string, int> coffees = new Dictionary<string, int>(); coffees.Add("Americano", 50); coffees.Add("Latte", 70); coffees.Add("Flat White", 60); coffees.Add("Espresso", 60); coffees.Add("Cappuccino", 80); coffees.Add("Mocha", 90); //your code goes here foreach (string coffee in coffees.Keys.ToArray()) { coffees[coffee] -= (int)((double)coffees[coffee] / 100 * discount); Console.WriteLine(coffee + ": " + coffees[coffee]); } } } }

10th Sep 2021, 9:59 PM
Loup-Venant
Loup-Venant - avatar
5 Answers
+ 1
Loup-Venant No need to do Keys.ToArray(), Keys is enough You can not modify collection like that in foreach loop so store discounted value in a variable. You can do like this: foreach (string coffee in coffees.Keys) { int res = (coffees[coffee] * discount / 100); res = coffees[coffee] - res; Console.WriteLine(coffee + ": " + res); }
11th Sep 2021, 3:30 AM
A͢J
A͢J - avatar
+ 2
Looks like your code is big enough. So please post it in the code playground and attach the code with a description.If you don't know how to do it then you can have a look at it: https://www.sololearn.com/post/75089/?ref=app
11th Sep 2021, 2:48 AM
Pariket Thakur
Pariket Thakur - avatar
+ 2
Loup-Venant You can use both with foreach loop. Both will give same result. Explanation is here about Keys and Keys.ToArray() https://code.sololearn.com/c98LlNXa332K/?ref=app
11th Sep 2021, 8:06 AM
A͢J
A͢J - avatar
+ 1
In my case, 66.5 is rounded off to 66 while using Convert.ToInt32() and Math.Round() function. So I created my own RoundOFF function. Please have a look! class Program { static void Main(string[] args) { int discount = Convert.ToInt32(Console.ReadLine()); Dictionary<string, int> coffee = new Dictionary<string, int>(); coffee.Add("Americano", 50); coffee.Add("Latte", 70); coffee.Add("Flat White", 60); coffee.Add("Espresso", 60); coffee.Add("Cappuccino", 80); coffee.Add("Mocha", 90); //your code goes here double y; foreach (var x in coffee.Keys.ToArray()){ y = coffee[x]*(100-discount); y /= 100; coffee[x] = RoundOFF(y); Console.WriteLine("{0}: {1}",x,coffee[x]); } } static int RoundOFF (double a){ double b = 0; int c; if (a%1==0){ b =a ; } else { c = (int)a/1; if (a%1>=0.5){ c++; } b = Convert.ToDouble(c); } c = Convert.ToInt32(b); return c; } }
25th Jul 2022, 9:11 AM
Neon Neupane
Neon Neupane - avatar
0
HrCoder I kind of understand what is your goal here, but not sure. If it's about the fact I didn't use the "insert code" part from the playground, I didn't know about it. So thank you, I'll try to remember it for next time. A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ Thanks for the basic logic math help. Math wasn't my strong suit in school. But again, if I don't need Keys.ToArray() why does the Hint of the project point it to me ? :( It feels like there is some inconsistencies in this last module
11th Sep 2021, 7:00 AM
Loup-Venant
Loup-Venant - avatar