C
C#17mo ago
sunny innit

❔ 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
ero17mo ago
the error is pretty self explanatory, no? you can't store a string in a variable of type int
sunny innit
sunny innit17mo ago
How would you fix it? Which line is the int in? im within my first weeks of learning this
ero
ero17mo 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 innit17mo ago
sunny innit
sunny innit17mo 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
Pobiega17mo ago
resetting doesnt work because you dont reset anything in it, you just readline thats easy, just slap in a "break;"
ero
ero17mo ago
but wait, except it isn't
Pobiega
Pobiega17mo ago
oh, its in a switch
ero
ero17mo ago
:l
sunny innit
sunny innit17mo ago
Yeah haha 😛
Pobiega
Pobiega17mo 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
Buddy17mo ago
Isn't this homework?
Pobiega
Pobiega17mo ago
it could be. the file name is "exercise 2.cs"
sunny innit
sunny innit17mo 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
Pobiega17mo ago
where do you think? explain your reasoning
sunny innit
sunny innit17mo 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
Pobiega17mo ago
Are we trying to fix case 4 atm? ie, "avsluta programmet"?
sunny innit
sunny innit17mo ago
Well theres two problems first one is case 3
Pobiega
Pobiega17mo ago
there are a lot more problems 😛
sunny innit
sunny innit17mo ago
definitely there is hahaha case #3 is resetting it
Pobiega
Pobiega17mo ago
I suggest we fix them one by one, starting with the most important one: storing the value
sunny innit
sunny innit17mo ago
Aye capn, how would we go about that?
Pobiega
Pobiega17mo 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 innit17mo ago
I can rewrite it
Pobiega
Pobiega17mo 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 innit17mo ago
i can copy paste the entire thing into here if that helps; But yeah i believe int värde is it?
Pobiega
Pobiega17mo ago
$paste
MODiX
MODiX17mo 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 innit17mo ago
oooh
Pobiega
Pobiega17mo ago
okay, so its declared as an int and can thus only ever hold integer values what does Console.ReadLine() return?
sunny innit
sunny innit17mo ago
BlazeBin - hqjzonskjoio
A tool for sharing your source code with the world!
Pobiega
Pobiega17mo ago
this doesnt compile at the moment, right?
sunny innit
sunny innit17mo ago
What do you mean? 😮
Pobiega
Pobiega17mo ago
visual studio doesnt want to build it because of errors
sunny innit
sunny innit17mo ago
Yeah exactly i have 1 error that it can see i believe theres errors that it cannot pick up
Pobiega
Pobiega17mo ago
correct
sunny innit
sunny innit17mo ago
so called "Logical errors"
Pobiega
Pobiega17mo ago
but lets fix them one by one
sunny innit
sunny innit17mo ago
Yes please!
MODiX
MODiX17mo ago
Pobiega#2671
what does Console.ReadLine() return?
Quoted by
React with ❌ to remove this embed.
Pobiega
Pobiega17mo ago
start here.
sunny innit
sunny innit17mo ago
Return how? like what value should be put into it?
Pobiega
Pobiega17mo ago
no, what does it return what comes "out" of the method someVariable = Console.ReadLine(); what is now in someVariable?
sunny innit
sunny innit17mo ago
Im sorry, but i dont understand 😮 can i call you or something?
Pobiega
Pobiega17mo ago
#dev-vc-1
Accord
Accord17mo 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.