C
C#•2y ago
theralsei

"FOR"

I have this code:
{
internal class Program
{
static void Main(string[] args)
{
int[] foodPrices = { 70, 60, 55, 30, 80, 130 };
int[] foodAmount = { 5, 10, 10, 7, 12, 15 };

Console.WriteLine("Pizzeria!");
Console.SetCursorPosition(0, 5);
Console.WriteLine("Prices:");
for (int i = 0; i < foodPrices.Length; i++)
{
Console.WriteLine($"\nPizza {i+1} cost {foodPrices[i]}." );
for(int j = 0; j < foodAmount.Length; j++)
{
Console.WriteLine($"Pizza {i+1} left {???}");
}
}
}
}
}
{
internal class Program
{
static void Main(string[] args)
{
int[] foodPrices = { 70, 60, 55, 30, 80, 130 };
int[] foodAmount = { 5, 10, 10, 7, 12, 15 };

Console.WriteLine("Pizzeria!");
Console.SetCursorPosition(0, 5);
Console.WriteLine("Prices:");
for (int i = 0; i < foodPrices.Length; i++)
{
Console.WriteLine($"\nPizza {i+1} cost {foodPrices[i]}." );
for(int j = 0; j < foodAmount.Length; j++)
{
Console.WriteLine($"Pizza {i+1} left {???}");
}
}
}
}
}
I somehow need to make it so that I was shown both the price of pizza and its amount. I ran into a problem: the whole array is shown to me, and not the value determined from it. How can I extract this particular value for each pizza?
34 Replies
Pobiega
Pobiega•2y ago
well you have a nested loop so the outer loop i will run 6 times, and the inner loop will run 6*6 times
theralsei
theralseiOP•2y ago
It turns out I need to make them not nested?
Pobiega
Pobiega•2y ago
in fact, you just need one loop
theralsei
theralseiOP•2y ago
how exactly can i do this in one loop?
Pobiega
Pobiega•2y ago
Well, both foodPrices and foodAmount have the exact same number of items and i is just a number, it has no direct association to either array (I'm not going to hand you the answer, but you should be able to figure it out from here on)
theralsei
theralseiOP•2y ago
I'll think about it
Pobiega
Pobiega•2y ago
you can literally just remove the inner loop, but keep the two writelines.
theralsei
theralseiOP•2y ago
I came to a working way. Is it must be like
int[] foodPrices = { 70, 60, 55, 30, 80, 130 };
int[] foodAmount = { 5, 10, 10, 7, 12, 15 };

Console.WriteLine("Pizzeria!");
Console.SetCursorPosition(0, 5);
Console.WriteLine("Prices:");
for (int i = 0; i < foodPrices.Length; i++)
{
Console.WriteLine($"\nPizza {i+1} cost {foodPrices[i]}." );
Console.WriteLine($"Pizza {i+1} left {foodAmount[i]}");
}
int[] foodPrices = { 70, 60, 55, 30, 80, 130 };
int[] foodAmount = { 5, 10, 10, 7, 12, 15 };

Console.WriteLine("Pizzeria!");
Console.SetCursorPosition(0, 5);
Console.WriteLine("Prices:");
for (int i = 0; i < foodPrices.Length; i++)
{
Console.WriteLine($"\nPizza {i+1} cost {foodPrices[i]}." );
Console.WriteLine($"Pizza {i+1} left {foodAmount[i]}");
}
Pobiega
Pobiega•2y ago
yep!
theralsei
theralseiOP•2y ago
Thank you for your help!
Pobiega
Pobiega•2y ago
yw. btw, a more idiomatic C# solution to this would be to not have two arrays of prices and amounts but rather have a single array of "Pizza" class/struct that represents both values
theralsei
theralseiOP•2y ago
Something like that?
string[] pizza = { "Pepperoni - 70" ...};
string[] pizza = { "Pepperoni - 70" ...};
Pobiega
Pobiega•2y ago
Pizza[] food = new[]
{
new Pizza(70, 5),
new Pizza(60, 10),
new Pizza(55, 10),
new Pizza(30, 7),
new Pizza(80, 12),
new Pizza(130, 15),
};

Console.WriteLine("Pizzeria!");
Console.SetCursorPosition(0, 5);
Console.WriteLine("Prices:");
for (int i = 0; i < food.Length; i++)
{
Console.WriteLine($"\nPizza {i + 1} cost {food[i].Price}.");
Console.WriteLine($"Pizza {i + 1} left {food[i].Amount}");
}

internal record struct Pizza(int Price, int Amount);
Pizza[] food = new[]
{
new Pizza(70, 5),
new Pizza(60, 10),
new Pizza(55, 10),
new Pizza(30, 7),
new Pizza(80, 12),
new Pizza(130, 15),
};

Console.WriteLine("Pizzeria!");
Console.SetCursorPosition(0, 5);
Console.WriteLine("Prices:");
for (int i = 0; i < food.Length; i++)
{
Console.WriteLine($"\nPizza {i + 1} cost {food[i].Price}.");
Console.WriteLine($"Pizza {i + 1} left {food[i].Amount}");
}

internal record struct Pizza(int Price, int Amount);
something like this we can make our own types and you should be taking advantage of that 🙂
theralsei
theralseiOP•2y ago
I'm just a beginner in C#, and I didn't even think about a solution like that. catshrug Anyways, thank you!
Pobiega
Pobiega•2y ago
Thats fine! Im just telling you the "idiomatic" solution
theralsei
theralseiOP•2y ago
case 1:
Console.WriteLine("Pizza name: ");
userPizzaInput = Console.ReadLine();
if(userPizzaInput != food.Name)
break;
case 1:
Console.WriteLine("Pizza name: ");
userPizzaInput = Console.ReadLine();
if(userPizzaInput != food.Name)
break;
Here another problem come. I have the same code, I just added a new string parameter called name. Since I have a pizzeria, you have to buy pizza in a pizzeria, so I wanted to make such a mechanic. But just by calling food in the condition, I will not be able to get "Name" from it. How can i do this?
Pobiega
Pobiega•2y ago
can you show your full code? or did you base this on my code above?
theralsei
theralseiOP•2y ago
Yeah
Pobiega
Pobiega•2y ago
well, food is not just one pizza, its all of them so you need to see if one of them match the provided name you could do this with a loop, like before, but there are some very helpful methods for this kind of operation already
theralsei
theralseiOP•2y ago
I need to make sure that I can't buy pizza that doesn't exist.
Pobiega
Pobiega•2y ago
sure
theralsei
theralseiOP•2y ago
I hate the translator, lol. I mean I want to make that I can't but pizza that doesn't exist But I'll think you understood me
Pobiega
Pobiega•2y ago
I understand what you are trying to say, np
Pizza[] food =
{
new Pizza("One", 70, 5),
new Pizza("Two", 60, 10),
new Pizza("Three", 55, 10),
new Pizza("Four", 30, 7),
new Pizza("Five", 80, 12),
new Pizza("Six", 130, 15),
};
Pizza[] food =
{
new Pizza("One", 70, 5),
new Pizza("Two", 60, 10),
new Pizza("Three", 55, 10),
new Pizza("Four", 30, 7),
new Pizza("Five", 80, 12),
new Pizza("Six", 130, 15),
};
internal class Pizza
{
public Pizza(string name, int price, int amount)
{
Name = name;
Price = price;
Amount = amount;
}

public string Name { get; set; }
public int Price { get; set; }
public int Amount { get; set; }
}
internal class Pizza
{
public Pizza(string name, int price, int amount)
{
Name = name;
Price = price;
Amount = amount;
}

public string Name { get; set; }
public int Price { get; set; }
public int Amount { get; set; }
}
Console.WriteLine("Pizza name: ");
var userPizzaInput = Console.ReadLine();
var selectedPizza = food.FirstOrDefault(x => x.Name == userPizzaInput);
if (selectedPizza is null)
{
Console.WriteLine("No pizza with that name.");
}
Console.WriteLine("Pizza name: ");
var userPizzaInput = Console.ReadLine();
var selectedPizza = food.FirstOrDefault(x => x.Name == userPizzaInput);
if (selectedPizza is null)
{
Console.WriteLine("No pizza with that name.");
}
I changed Pizza a bit, since we'll likely want to change the value of Amount in the future and record struct would make that... awkward. Better to use a normal class here the real magic is these lines:
var selectedPizza = food.FirstOrDefault(x => x.Name == userPizzaInput);
if (selectedPizza is null)
var selectedPizza = food.FirstOrDefault(x => x.Name == userPizzaInput);
if (selectedPizza is null)
theralsei
theralseiOP•2y ago
I have not met var. I understand that it can store data of any type?
Pobiega
Pobiega•2y ago
no, it just means "figure out the type for me" in this case, that type is Pizza?
theralsei
theralseiOP•2y ago
Oh well, I take it that's pretty effective.
Pobiega
Pobiega•2y ago
its entirely optional, I personally use it a lot but I can see why a beginner would prefer to type out their types 🙂
theralsei
theralseiOP•2y ago
Could you explain how these lines work?
Pobiega
Pobiega•2y ago
the important part is understanding that there is no difference in type safety here - they are both the exact same thing being compiled sure FirstOrDefault will return the first item in the collection that matches the predicate function the predicate being x => x.Name == userPizzaInput here thats a "lambda function", here its a function that takes in a Pizza x and returns true or false the first item to return true will be returned by FirstOrDefault if the end of the collection is reached and no item has been returned, it will return default, in this case null (because Pizza is a class)
theralsei
theralseiOP•2y ago
That's really interesting. Thank you!
Pobiega
Pobiega•2y ago
There are TONS of similar functions! First, Any, All, Single, Where, OrderBy etc
theralsei
theralseiOP•2y ago
By the way, I didn’t understand how Pizza turned into x, and why it’s x at all.
Angius
Angius•2y ago
x => x.Name == userPizzaInput
x => x.Name == userPizzaInput
is, essentially, a shorthand for
static bool Whatever(Pizza x)
{
return x.Name == userPizzaInput;
}
static bool Whatever(Pizza x)
{
return x.Name == userPizzaInput;
}
theralsei
theralseiOP•2y ago
got it! thanks!
Want results from more Discord servers?
Add your server