❔ Need help with basic string code

Im a learner trying to get into this. and im not sure what im doing wrong here i'll post pictures
46 Replies
ero
ero2y ago
the error is pretty self explanatory, no? you can't store a string in a variable of type int
sunny innit
sunny innitOP2y ago
How would you fix it? Which line is the int in? im within my first weeks of learning this
ero
ero2y ago
you're not showing the line where you declare värde but you have to convert the input from Console.ReadLine() into an integer somehow you would use int.Parse or, preferrably, int.TryParse for that TryParse returns a boolean which indicates whether the input was able to be converted to an int or not an out parameter gives you the converted number
sunny innit
sunny innitOP2y ago
sunny innit
sunny innitOP2y ago
i assume the 16th line is the int line? The thing is im trying to make a menu with 4 lines 4 options* 1) lets you choose a value 2) lets you save the value 3) reset the value 4) close the program the resetting doesnt work and the #4 crashes the program i think i need a way to end the while loop at #4
Pobiega
Pobiega2y ago
resetting doesnt work because you dont reset anything in it, you just readline thats easy, just slap in a "break;"
ero
ero2y ago
but wait, except it isn't
Pobiega
Pobiega2y ago
oh, its in a switch
ero
ero2y ago
:l
sunny innit
sunny innitOP2y ago
Yeah haha 😛
Pobiega
Pobiega2y ago
you'll need to break the while then, by setting the while condition to false however, lets turn our attention to the core problem first C# is a typed language, so all variables we create have a declared type and can only ever be that type int and string are just fundamentally different types
Buddy
Buddy2y ago
Isn't this homework?
Pobiega
Pobiega2y ago
it could be. the file name is "exercise 2.cs"
sunny innit
sunny innitOP2y ago
where would you break it? Yeah it is that i've assigned for myself i dont go to school, but i got some assignments from a friend who did at one point trying to learn for fun
Pobiega
Pobiega2y ago
where do you think? explain your reasoning
sunny innit
sunny innitOP2y ago
so this might be stupid, but bear with me lol #53 is where the while thing ends so i could slap it in there orrrrr under #4 thats my reasoning as it needs to stop when we tell it to?
Pobiega
Pobiega2y ago
Are we trying to fix case 4 atm? ie, "avsluta programmet"?
sunny innit
sunny innitOP2y ago
Well theres two problems first one is case 3
Pobiega
Pobiega2y ago
there are a lot more problems 😛
sunny innit
sunny innitOP2y ago
definitely there is hahaha case #3 is resetting it
Pobiega
Pobiega2y ago
I suggest we fix them one by one, starting with the most important one: storing the value
sunny innit
sunny innitOP2y ago
Aye capn, how would we go about that?
Pobiega
Pobiega2y ago
first things first, I strongly suggest always naming your variables and methods in english värde should be storedValue or something
sunny innit
sunny innitOP2y ago
I can rewrite it
Pobiega
Pobiega2y ago
Second, can you tell me what that variable is declared as? it seems as its declared as int värde; atm
sunny innit
sunny innitOP2y ago
i can copy paste the entire thing into here if that helps; But yeah i believe int värde is it?
Pobiega
Pobiega2y ago
$paste
MODiX
MODiX2y ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
sunny innit
sunny innitOP2y ago
oooh
Pobiega
Pobiega2y ago
okay, so its declared as an int and can thus only ever hold integer values what does Console.ReadLine() return?
sunny innit
sunny innitOP2y ago
BlazeBin - hqjzonskjoio
A tool for sharing your source code with the world!
Pobiega
Pobiega2y ago
this doesnt compile at the moment, right?
sunny innit
sunny innitOP2y ago
What do you mean? 😮
Pobiega
Pobiega2y ago
visual studio doesnt want to build it because of errors
sunny innit
sunny innitOP2y ago
Yeah exactly i have 1 error that it can see i believe theres errors that it cannot pick up
Pobiega
Pobiega2y ago
correct
sunny innit
sunny innitOP2y ago
so called "Logical errors"
Pobiega
Pobiega2y ago
but lets fix them one by one
sunny innit
sunny innitOP2y ago
Yes please!
MODiX
MODiX2y ago
Pobiega#2671
what does Console.ReadLine() return?
Quoted by
React with ❌ to remove this embed.
Pobiega
Pobiega2y ago
start here.
sunny innit
sunny innitOP2y ago
Return how? like what value should be put into it?
Pobiega
Pobiega2y ago
no, what does it return what comes "out" of the method someVariable = Console.ReadLine(); what is now in someVariable?
sunny innit
sunny innitOP2y ago
Im sorry, but i dont understand 😮 can i call you or something?
Pobiega
Pobiega2y ago
#dev-vc-1
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server