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