C
C#13mo ago
Array

Programming a menu on WinForms

Coding a Pizza menu selector that outputs the price, thinking i could mainly use if and else if statements along with “/n”, a bit stuck on how to make the values add together tho. Should I assign them as double datatypes? I got my code to work to just show them after pressing button, a bit stuck on the math I need to sum these and output a subtotal, make a tax variable and have a total. Anyone think they can help? I actually was able to get the strings to show, just dont know how to do calculations
No description
90 Replies
Omnissiah
Omnissiah13mo ago
double could be fine for learning, but decimal would be better for handling this kind of values what's the problem with calculations, percentages? getting the values?
Array
ArrayOP13mo ago
Hi, I am having issues getting the values, will send pic of code
Omnissiah
Omnissiah13mo ago
pasting code as text would be better...
Array
ArrayOP13mo ago
Will do too! Sorry I appreciate you for helping me I have been stuck for so long I am new to C# so I appreciate the help One sec while I load my computer csharp private void btnCheckout_Click(object sender, EventArgs e) { string OrderSummary = string.Empty; //check for selected size if (optSlice.Checked == true) { OrderSummary = OrderSummary + "Slice ($3.00)"; } else if (optSmall.Checked) { OrderSummary = OrderSummary + "Small ($7.00)"; } else if (optMedium.Checked) { OrderSummary = OrderSummary + "Medium ($9.50)"; } else if (optLarge.Checked) { OrderSummary = OrderSummary + "Large ($12.99)"; } else if (optExtraLarge.Checked) { OrderSummary = OrderSummary + "Party ($18.99)"; } //new line for more outputs OrderSummary = OrderSummary + "\n"; //check for drinks if (optSoftDrink.Checked) { OrderSummary = OrderSummary + "Soft Drink ($1.99)" + "\n"; } else if (optOrangeJuice.Checked) { OrderSummary = OrderSummary + "Orange Juice ($1.60)" + "\n"; } else if (optChocolateMilk.Checked) { OrderSummary = OrderSummary + "Chocolate Milk ($2.25)" + "\n"; } else if (optIceTea.Checked) { OrderSummary = OrderSummary + "Ice Tea ($2.50)" + "\n"; } OrderSummary = OrderSummary + "\n"; @dont I am not sure how to assign these items number values and add them. Was thinking I could assign them a double value and add them after but I'm not sure how to go about it
Omnissiah
Omnissiah13mo ago
ah ok so you have the strings, but you don't really have a place where you are keeping the relation between the price and the item
Array
ArrayOP13mo ago
Right! I was thinking of making up a variable called double and seeing if I could assign it to everything with their price
Omnissiah
Omnissiah13mo ago
which is ironic for a guy called Array
Array
ArrayOP13mo ago
LOOOOL I can tell you a funny story About my name I actually just got into Comp Sci I called myself Array since I was like 10 I had no idea I would want to study it though I’ve always loved math though Been my best subject But my career path was wanted to do med stuff But I think I just decided to do something I think I’m actually good at and have interest in Had Discord since 2016 so maybe it was meant to be 😂😂😂 I actually haven’t learned about Array’s yet In computer science
Omnissiah
Omnissiah13mo ago
in this way you should get the price from the text which would be way more complicated than learn about arrays i mean, it's not that much difficult, it's like one line of code, but still you would need arrays
Array
ArrayOP13mo ago
Hmm, maybe I could make the variable double at the top instead of string? Or is there a way to parse it into a double
Omnissiah
Omnissiah13mo ago
not really, that's a string
Array
ArrayOP13mo ago
Gotcha
Omnissiah
Omnissiah13mo ago
to me the smartest move right now would be having the price in a switch series of ifs and checking for checked for every group of items
Array
ArrayOP13mo ago
Makes sense, so like two diff button? Or how would you go about coding that It’s just odd because I know how to do console integer calculations … like I could name it Side A and give it a number value as a double datatype. Just confused why it doesn’t work here Like hypothetically why can’t I add to each if statement a double variable and name it price and assign it a number value Or something for each category
Omnissiah
Omnissiah13mo ago
not two buttons but two variables you already have the various if for building the final text you can just re-use those to add the price too without parsing it, but hey, baby steps
Array
ArrayOP13mo ago
Thanks for patient, I appreciate you being kind bc I am really new to this Ok, let me try to understand this rq
Array
ArrayOP13mo ago
Also here is what I am supposed to be doing, (I could have messed up with the way I communicated my problem)
No description
Array
ArrayOP13mo ago
So you’re saying I should make another variable for price with a double data type?
Omnissiah
Omnissiah13mo ago
seems the best choice right now (although a better one would be using arrays)
Array
ArrayOP13mo ago
I think that’s what my classmates did too So what I’ll do is that I’ll make a header comment before I start my if structure and say //make variable for price I learn Arrays in a 2 days haha Thank you so much, I’ll try it out
Omnissiah
Omnissiah13mo ago
i would say that if you name the variable with a good name and stow some of that stuff in methods you don't really need comments (yet)
Array
ArrayOP13mo ago
Sorry if I sound really stupid (I’m really new I’m sorry 😭) but what do you mean by stow some of that stuff in the methods? Was thinking I could do a header comment to show that I would be introducing that variable at the start Or maybe I could just assign it under the first if statement So like in the if statement for slice double Price = 3.00
Omnissiah
Omnissiah13mo ago
if you give an organization to your code so that everything is in logical steps, then it's clear enough to understand that it doesn't need comments for example if you have all the if (checked) related to an item group, say the drinks or the desserts, if you put all the if of the drinks in a method then you have a place for that group of items also this
//make variable for price
if you use
// TODO comment
// TODO comment
you can see your task in the Tasks window of visual studio (if you are using visual studio), it's easier to remember and find again stuff this way for me
Array
ArrayOP13mo ago
I am using Visual Studio! Okay I get what you’re saying, my teacher wants us to use comments to make it clear even though what you’re saying makes sense, just so that if we come back we understand what’s happening
Omnissiah
Omnissiah13mo ago
it's like if your variable is named slicePrice then it's clear enough that it's the price for a slice of pizza, that's it (and so on)
Array
ArrayOP13mo ago
Just a question, should I actually assign each item it’s own individual double variable? I thought I would just name it sizeprice and give it a datatype Then afterwards I’d add the 4 categories together depending on what was selected I could be wront
Omnissiah
Omnissiah13mo ago
i'll tell you i would made a variable for each category and them sum them in the end (plus other operations that are eventually needed)
Array
ArrayOP13mo ago
Yeah I think that makes sense I’ll make 4 doubles, named SizePrice DrinkPrice ToppingPrice DessertsPrice For each if statement, underneath the if / if else statement for it and under the string, I’ll do like double SizePrice = 3; Would something like that make sense
Omnissiah
Omnissiah13mo ago
from what i read pizza toppings should be plural too
Array
ArrayOP13mo ago
Right Thanks, good catch
Omnissiah
Omnissiah13mo ago
also, variables should not be start with capitals only public fields although it's not so strict the naming on those
Array
ArrayOP13mo ago
Wait really? Oh okay Okay I’ll try coding in the values for size Thank you Let’s see what we come up with I really appreciate your help Cause I was looking at this for 4 hrs and lost lol
Omnissiah
Omnissiah13mo ago
i'm pretty sure if you try the visual studio will give you a notice
Array
ArrayOP13mo ago
Yea my teacher ignores them though
Omnissiah
Omnissiah13mo ago
eh in general if you can solve all the notices and the warnings your code will be higher quality i know it's hard
Array
ArrayOP13mo ago
It might just be because we are beginners But yeah
Omnissiah
Omnissiah13mo ago
it's discipline, you need to push yourself a little to do it but it pays off in the end you read the code more than you write it, so it useful to have it look all the same
Array
ArrayOP13mo ago
Yeah you’re right I’ll def start doing that private void btnCheckout_Click(object sender, EventArgs e) { string OrderSummary = string.Empty; //check for selected size if (optSlice.Checked == true) { OrderSummary = OrderSummary + "Slice ($3.00)"; double SizePrice = 3.00; } else if (optSmall.Checked) { OrderSummary = OrderSummary + "Small ($7.00)"; double SizePrice = 7.00; } else if (optMedium.Checked) { OrderSummary = OrderSummary + "Medium ($9.50)"; double SizePrice = 9.50; } else if (optLarge.Checked) { OrderSummary = OrderSummary + "Large ($12.99)"; double SizePrice = 12.99; ` @dont here's a sample of what ive tried, does it look okay? I did for all of them, just a question, how would I go about outputting this? like after I try this
Array
ArrayOP13mo ago
No description
Array
ArrayOP13mo ago
I also get this warning
Omnissiah
Omnissiah13mo ago
you need the variable to be visible outside the if since you need to use it in the end
Array
ArrayOP13mo ago
Oh okay
Array
ArrayOP13mo ago
No description
Array
ArrayOP13mo ago
I get this error I’m a bit confused on what it means? @dont
Omnissiah
Omnissiah13mo ago
you can (and need to) define the variable once
Array
ArrayOP13mo ago
No description
Array
ArrayOP13mo ago
Ohhh so I don't need to call it a double you're saying Dang I’m an idiot lol Thank you
Array
ArrayOP13mo ago
Hm, another error
No description
Array
ArrayOP13mo ago
@dont i did assign it though
Omnissiah
Omnissiah13mo ago
well first you're missing a space in the text after Subtotal second, probably what's he's saying is true
Array
ArrayOP13mo ago
Will fix Wdym?
Omnissiah
Omnissiah13mo ago
because the last case of the if sequence would be and else paste here the if sequence of one of the categories
Array
ArrayOP13mo ago
Gotcha //check for toppings if (chkPepperoni.Checked) { OrderSummary = OrderSummary + "Pepperoni ($1.50)" + "\n"; ToppingsPrice = 1.50; } if (chkExtraMozzarella.Checked) { OrderSummary = OrderSummary + "Extra Mozzarella ($0.99)" + "\n"; ToppingsPrice = 0.99; } if (chkSausage.Checked) { OrderSummary = OrderSummary + "Sausage ($1.99)" + "\n"; ToppingsPrice = 1.99; } if (chkHam.Checked) { OrderSummary = OrderSummary + "Ham ($2.20)" + "\n"; ToppingsPrice = 2.20; } if (chkHotPeppers.Checked) { OrderSummary = OrderSummary + "Hot Peppers ($2.50)" + "\n"; ToppingsPrice = 2.50; } OrderSummary = OrderSummary + "\n"; //check for desserts if (chkOneGelato.Checked) { OrderSummary = OrderSummary + "Gelato 1 Scoop ($4.50)" + "\n"; DessertsPrice = 4.50; } if (chkTwoGelato.Checked) { OrderSummary = OrderSummary + "Gelato 2 Scoop ($6.50)" + "\n"; DessertsPrice = 6.50; } if (chkCannoli.Checked) { OrderSummary = OrderSummary + "Cannoli ($2.50)" + "\n"; DessertsPrice = 2.50; } if (chkNutellaDonut.Checked) { OrderSummary = OrderSummary + "Nutella Donut ($3.25)" + "\n"; DessertsPrice = 3.25; } lblOrderSummary.Text = OrderSummary; lblOrderSummary.Text = "Subtotal" + SizePrice + DrinkPrice + ToppingsPrice + DessertsPrice; Pasted
Omnissiah
Omnissiah13mo ago
can you figure out in this why variables could be left not assigned? say DessertsPrice
Array
ArrayOP13mo ago
I think the computer is getting confused because multiple things could be true maybe?
Omnissiah
Omnissiah13mo ago
the other way around, they could be false
Array
ArrayOP13mo ago
True Maybe I should make the last one an else statement?
Omnissiah
Omnissiah13mo ago
well dessert was a multiple options right?
Array
ArrayOP13mo ago
Right
Omnissiah
Omnissiah13mo ago
so you could choose everyone of them
Array
ArrayOP13mo ago
So then I could make them else if ‘s then? Trying to think
Omnissiah
Omnissiah13mo ago
what would happen
Array
ArrayOP13mo ago
My first condition is that if you press on one dessert, but if that’s false it just skips to the end right?
Omnissiah
Omnissiah13mo ago
yes
Array
ArrayOP13mo ago
If I made it an else if it just another condition apart of the same if statement Like if it is false, it checks for next condition So I’ll try else if’s
Omnissiah
Omnissiah13mo ago
what if more than 1 is true tho
Array
ArrayOP13mo ago
Hmm Well An if is for true An else if is for false but a new condition an else is for false and goes to the end Let’s say our first one is true and our second one is true It might be hard to program Maybe I should make the initial one an else if? Or I’m a bit confused I could chain everything within that first if statement
Omnissiah
Omnissiah13mo ago
so let's complicate it even more what if they are all false
Array
ArrayOP13mo ago
Well I think the else if’s would eventually skim to the end of the code This requires alot of thinking lol Hmm Well
Omnissiah
Omnissiah13mo ago
not really, the conditions are few or just 2 really
Array
ArrayOP13mo ago
I need to make it so that it can check for None All Or a combination of 2/3 So 3 cases right?
Omnissiah
Omnissiah13mo ago
is it really that different having all or a combination? why would you care
Array
ArrayOP13mo ago
I think so, because all means all conditions are true, whereas a combination is some are true some aren’t
Omnissiah
Omnissiah13mo ago
how that would differ, you have to sum them anyway
Array
ArrayOP13mo ago
I kinda see what you’re saying since it’ll just sum the ones that apply I was thinking that it would check through the else if that if it was true it would run that code and get that double value to be added after I think it might getting confused because I assigned different numbers to the same variable So if multiple are true it doesn’t know what to do
Omnissiah
Omnissiah13mo ago
the error is saying that variable could be uninitialized so with no value
Array
ArrayOP13mo ago
Oh So it doesn’t recognize the 0?
Omnissiah
Omnissiah13mo ago
i don't think you wrote 0 anywhere or at least not that i can see
Array
ArrayOP13mo ago
I gotcha So maybe I need a final else statement for all that is 0 Ahhh That makes sense
Omnissiah
Omnissiah13mo ago
there could be a shorter way
Array
ArrayOP13mo ago
What would that be?
Omnissiah
Omnissiah13mo ago
think about it
Array
ArrayOP13mo ago
A final else statement for all variablesb ?
Omnissiah
Omnissiah13mo ago
some variables are for items that are mutually exclusive and some that are not so it wouldn't work 100% but you have another place where you use variables time for dinner for me
Array
ArrayOP13mo ago
Have a good dinner 🙂 @dont any chance you could help me figure it out now? Still a bit stuck Or is anyone else able to help me add values? Been stuck on this for so long and still stuck 😢 Tried to re arrange variables and stuff but just not working Yea I'm lost, tried to make else for everything but when I got to the groupboxes it gave me a whopping 81 errors, not sure what to do. Asking for help because I've been stuck for the past 12 hrs and can't seem to figure it out
SinFluxx
SinFluxx13mo ago
Well the last problem you showed was that there were scenarios where your prices were unassigned, so you should just need to make sure they're assigned in every situation If you want to be sure that there's no chance of a variable being unintialized then there's one very obvious point in time at which to give it a value 🙂
Array
ArrayOP13mo ago
Hey are you online??? I gave it to it the start It wasn’t working still
SinFluxx
SinFluxx13mo ago
I assume those must be different errors then?
Omnissiah
Omnissiah13mo ago
focus on the first error btw sometimes the number of errors doesn't really matter correcting one error could make half of them go away
Array
ArrayOP13mo ago
Thanks
Want results from more Discord servers?
Add your server