C
C#ā€¢14mo ago
Dinny

A quick question, how do we edit the value of an attribute in an input file?

My prof states that items named, appetizer, entree, and dessert must have values of 0, 1, and 2, when reading the input file, but I am at a loss. I emailed him, but his respond time is crazy long.
No description
51 Replies
Pobiega
Pobiegaā€¢14mo ago
What do you mean when you say "an attribute"? Because that word means something very specific in C#
Dinny
DinnyOPā€¢14mo ago
pause, that's my fault for wrong terminology. i think i am mixing things up i am simply referring to the items in an input file
Pobiega
Pobiegaā€¢14mo ago
"the items in an input file"? You mean the lines? "an input file" could mean pretty much anything
Dinny
DinnyOPā€¢14mo ago
lemme just send a screnshot bc i don't mean the full line
Pobiega
Pobiegaā€¢14mo ago
they can be read as a byte array, or an array of strings, or one giant string
Dinny
DinnyOPā€¢14mo ago
like this for exmaple
No description
Dinny
DinnyOPā€¢14mo ago
withing the first line, where it says entree he says it must return an integer value in this case a value of 1
Pobiega
Pobiegaā€¢14mo ago
can you paste the entire file? in a code block
Dinny
DinnyOPā€¢14mo ago
yes, please allow me a moment
Entree 28 Pan-Seared Halibut with Sauteed Spinach
Appetizer 9 Thai Green Curry Chicken Wings
Appetizer 10 Char-Grilled Shrimp Skewers
Entree 10 Angus Beef Burger with Aged Cheddar
Dessert 5 Gluten-Free Carrot Cake
Entree 19 Linguine with White Clam Sauce
Entree 23 12-oz Ribeye Steak with Cumin Pepper
Appetizer 4 Chilled Cucumber Soup
Dessert 5 Jam and Peanut Butter Cookie
Appetizer 10 Grilled Artichokes
Entree 28 Pan-Seared Halibut with Sauteed Spinach
Appetizer 9 Thai Green Curry Chicken Wings
Appetizer 10 Char-Grilled Shrimp Skewers
Entree 10 Angus Beef Burger with Aged Cheddar
Dessert 5 Gluten-Free Carrot Cake
Entree 19 Linguine with White Clam Sauce
Entree 23 12-oz Ribeye Steak with Cumin Pepper
Appetizer 4 Chilled Cucumber Soup
Dessert 5 Jam and Peanut Butter Cookie
Appetizer 10 Grilled Artichokes
this is the entire input file appetizer returns value 0, entree returns 1, ans 2 for dessert
Pobiega
Pobiegaā€¢14mo ago
ok so its a tab separated CSV Im guessing Entree, Appetizer and Dessert should be an enum
Dinny
DinnyOPā€¢14mo ago
i honestly didn't even think of that i felt so stuck too okay let me try that first, i'll brb i have a question again so when the stream reader reads the first part of the line, entree in this case, does it automatically read the enum value with it? he only taught us what enums are but never really implimented them, so i am unsure google is not helping much
Pobiega
Pobiegaā€¢14mo ago
no of course it doesnt
Dinny
DinnyOPā€¢14mo ago
i didn't think so
Pobiega
Pobiegaā€¢14mo ago
the file contains the string "Entree\t28\tPan-Seared Halibut" the reader is not aware of any enums
Dinny
DinnyOPā€¢14mo ago
enum aed
{
Appetizer = 0,
Entree = 1,
Dessert = 2,
}
enum aed
{
Appetizer = 0,
Entree = 1,
Dessert = 2,
}
so this is the enum i had created
Pobiega
Pobiegaā€¢14mo ago
yup, seems fine, other than the name use PascalCase, and call it something appropriate like DishType
Dinny
DinnyOPā€¢14mo ago
i can alter it of course, but i just wanted to make sure the structure was proper
Pobiega
Pobiegaā€¢14mo ago
yeah the definition is fine
Dinny
DinnyOPā€¢14mo ago
i have an integer variable named that already so i didn't bc i didn't wanna be confused or to confused my prof well its pascal case so it would be fine one moment
Pobiega
Pobiegaā€¢14mo ago
that integer is supposed to map up with your enum, no? like, literally replace it :p
Dinny
DinnyOPā€¢14mo ago
yes, but that's the thing. I am unsure how to do that because he never truly went over it. like when the reader reads that line and sees "entree" i am unsure how to make have its enum value i watched a youtube videi for better understanding though i don't know how to make that apply to input files
Pobiega
Pobiegaā€¢14mo ago
you read it as a string then you parse the string to the correct enum value Enum.TryParse<T> is what you need, assuming you are using modern C#
Dinny
DinnyOPā€¢14mo ago
DishType dish = Enum.Parse<DishType>(Entree);
DishType dish = Enum.Parse<DishType>(Entree);
something like this ?
Pobiega
Pobiegaā€¢14mo ago
so close, yet so far away šŸ˜„ Entree would be your variable containing the string value you read from the file
Dinny
DinnyOPā€¢14mo ago
as long as there's hope i'm still willing to learn lol then would i put the literal value there ? 1 in this case
Pobiega
Pobiegaā€¢14mo ago
no you dont put that anywhere your file contains the string "Entree", no?
Dinny
DinnyOPā€¢14mo ago
yes it does so i leave it blank?
Pobiega
Pobiegaā€¢14mo ago
wdym you leave it blank "Entree" will get mapped to DishType.Entree by the parse function
Dinny
DinnyOPā€¢14mo ago
like after the <DishType> is it just <DishType>()
Pobiega
Pobiegaā€¢14mo ago
that has the true value 1
Dinny
DinnyOPā€¢14mo ago
:000 that makes way more sense then wtf i was saying
Pobiega
Pobiegaā€¢14mo ago
yes I agree šŸ˜„
Dinny
DinnyOPā€¢14mo ago
i assumed that there a lot of steps i need to take šŸ˜­ that's the way i'm being taught, so ig i just make things unnecessarily difficult u break things down easier
Pobiega
Pobiegaā€¢14mo ago
well to be fair, its one of me and one of you
Dinny
DinnyOPā€¢14mo ago
true
Pobiega
Pobiegaā€¢14mo ago
its always harder to teach multiple students
Dinny
DinnyOPā€¢14mo ago
there's only like 6 ppl total in this class though idk i just wish he would slow down some he moves way too fast and apparently with an outdated teaching style anyways so look at this code then rq
while (!reader.EndOfStream)
{

string[] nextLine = reader.ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);

if ( nextLine.Length == 3)
{
int dishType = Convert.ToInt32(nextLine[0]);


int dishPrice = Convert.ToInt32(nextLine[1]);


string dishName = nextLine[2];


}
while (!reader.EndOfStream)
{

string[] nextLine = reader.ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);

if ( nextLine.Length == 3)
{
int dishType = Convert.ToInt32(nextLine[0]);


int dishPrice = Convert.ToInt32(nextLine[1]);


string dishName = nextLine[2];


}
i did a quick loop thingy ik it's messed up but i have a question so im told we need a while look to read the contents of the input file, yes
Pobiega
Pobiegaā€¢14mo ago
I would suggest adding Trim to the string split options btw
Dinny
DinnyOPā€¢14mo ago
Trim ?
Pobiega
Pobiegaā€¢14mo ago
what .NET version are you using?
Dinny
DinnyOPā€¢14mo ago
one moment, let me see it says .NET 6.0
Pobiega
Pobiegaā€¢14mo ago
good
var x = "ost kaka hello".Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
var x = "ost kaka hello".Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
Dinny
DinnyOPā€¢14mo ago
okay i can do that, though why would u say trim is better out of curiosity
Pobiega
Pobiegaā€¢14mo ago
its not better I use both see?
Dinny
DinnyOPā€¢14mo ago
oh ok i though u meant get rid of that entirely one moment while i fix it pl
Pobiega
Pobiegaā€¢14mo ago
I even posted the code verbatim wth and I said " I suggest adding trim"
Dinny
DinnyOPā€¢14mo ago
ok so back to my question, in this sense, would i put the enum parse within the while loop ? like while loop only, or inside the if, or none?
Pobiega
Pobiegaā€¢14mo ago
seriously?
Dinny
DinnyOPā€¢14mo ago
sigh nevermind thank for your help thus far, but i will figre it out on my own
Pobiega
Pobiegaā€¢14mo ago
Just kinda feels like you have no idea whats going on and just throwing code at the problem when you ask a question like that its like discussing temperatures and basking techniques for cooking, to then ask "yeah so uh do we cook the chiken before or after serving it by the way?" Think of what is available and known * outside the while * inside the while, outside the if * inside the if and the answer should be obvious
Dinny
DinnyOPā€¢14mo ago
dawg i started this coding class literally last month. i've expressed how much i am struffling, how intense and unreliable my professor is, and how confusing this all is to me. i understand it can be frustrating and one may lose their patience, but alls i ask for is help. I don't need the attitude or remarks. I simply ask for help, not to feel stupid with every question i ask. i feel like i am frustrating you, so i will try and get ahold of my prof again and get out of your hair i will close the post now, i do appreciate the help you've given me thus far though
Want results from more Discord servers?
Add your server