Getting an error for an undefined variable
Hello, I'm a CS major working on a project so I'm still pretty new. I'm getting a CS0165 error on line 85 of this program I'm working on. Wouldn't the variable already defined after I've done all of the previous equations above it? The weird part is that it's not showing as an error for the rest of my code, just this one specific line and I'm not sure why. I've attached a picture as well as a BlazeBin link.
https://paste.mod.gg/trcpyfqktemo/0
BlazeBin - trcpyfqktemo
A tool for sharing your source code with the world!
31 Replies
Well,
points
can be uninitialized
What will be the value of points
if exerciseComboBox.SelectedText
is "Shrek"
?posted this before i added the else statements, refresh the link
but it looks like it works fine if I include it in one of the above if statements with no errors
i just don't think that it makes sense to just copy/paste those lines across 5 different statements if i don't need to
I don't see any other revisions in the link
oh it made a new one
ok click the new link
You can just add an
else
to your chain of else-if
s
Or turn one of the else-ifs into just an else
Or you can extract the repeated code into a methodso if I add another
else
in there with the chunk of code that I mentioned, it'll apply to the rest of the previous else/else if
statements?The else-if chain will be exhaustive
In case the input is
"Shrek"
, the code inside of the else
will execute
And, for example, set points
to 0
Or... you can give points a default value
0
seems like a safe defaultso I initally had it set to 0, but then I was getting a bunch of weird errors after I tried to use it in a equation
is it because i put the
const
there?Well, if
points
is a constant, then... it cannot change
So, sure, you can give it an initial valueoh true, duh
But none of your
points =
will do anythingi understand that, but then without the
points =
, how would i do the equation and have it output correctly?this is an example of what the program is supposed to look like for reference
Just don't make
points
a constantyes, but for example if I were to do
weightPoints * exerciseDuration;
in one of my if
statements without the points =
in front, how would the program know that I want the product to apply to the points
variable?
i'm just trying to understand why it wouldn't be necessary anymore, i know the code works either wayWhy would you want to do it without
points =
in front?
Of course it would be necessary
All I said, is that it won't work if points
is a constantwell you said it wouldn't do anything earlier
Yes, if
points
is constantohhhh ok
then why would i still be getting this if I have
int points = 0;
?Is your code still this? https://paste.mod.gg/trcpyfqktemo/0
this appears in all of the
points =
line but doesn't stop the program from running
yesThen that would be because you no longer use an unassigned value
points
are set in line 60, then used on 64 and 70
Everything is fineok cool cool
So... go back to how it was, but now with a default points value
yep, it works fine
also another thing, which i dont think you'd really be able to help me with without me putting this on github, but for some reason, when i run the program, the button isnt clickable? Do you know why this would be? I've checked the Event Handler to make sure it's linked and it is
No idea
all good, thanks for your help!