Making my FIRST very basic calculator (don't judge), need help and clarification
Hiya, I've followed a couple of Microsoft's tutorials on integers and strings, I've decided to try my best and make a very basic calculator program to perform operations on two numbers, a and b. I'm having problems with my code as I keep getting errors such as "unassigned local variable". I don't really understand the whole local business and what not, could someone perhaps help me by explaining it to me like I'm an idiot?
Here's a bit of code I wrote so far, now I'm working on selecting the operation.
44 Replies
And I guess I should have commented to clarify what I'm trying to do, but I figured it's so basic that it didn't need this. Essentially:
1. The program welcomes the user and displays a message asking for the first number (a)
2. The program checks whether their input is an actual number, if not it repeats the question, if yes it assigns the number to "a"
3. The program does the same thing but for a "b"
4. When it has the two numbers and they're indeed numbers, it asks for one of the four math operations I've included
5. The program checks whether the user inputed a number between 1-4, which correspond to the math operation
what errors do you currently have?
CS0165: use of unassigned local variables "a" and "b" just by the 4th line from the end
I guess it has to do something with variables not being accessible even though they exist in the code?
It's telling you that a and b don't have defined values by the time that line executes.
The compiler doesn't like it if there is a chance of the variable being uninitialized when it's used. A simple fix would be to just give them a default value, or to make sure than whatever branch of your program executes, a and b are always set to some definite value.
so at the beginning of the code I could do:
and it would assign them SOME value?
Specifically 0, yes 🙂
That should do it.
Okay, let me try then!
I don't know if you want to keep it separate for redability, but you could simplify this kind of thing
to
If you want to keep the string and int variables, I would at least use the result of TryParse directly as the condition. Just avoids an (imo) unnecessary extra step.
Also, the expression
isNumber != true
returns a bool, but isNumber
is already a bool, so you don't need the extra comparison. You can use its value directly like isNumber
or !isNumber
oh yeah, why assign into a separate variable when I can just use the outputs of the directly in the , is this what you mean?
Yes.
Didn't know you could do that!
You could argue that maybe stuffing it all in one line might make it less readable for some.
You don't have to do it that way, and if you find your approcah clearer, thats perfectly fine too 🙂
Definitely, I'm just doing it the very crude way just to get it working 😄
That's the nice thing about naming bools stuff like
isX
or canY
etc. You can use them directly a bit like regular speech, rather than having to do these abstract math-y operations on them.So it's a good practice to name booleans like this? I thought it'd get confusing with the booleans which are already in-built
Can you give an example?
Someone else might correct me, but I would say generally it is. Bools express something about your program that either is or isn't the case. Naming them this way is just quite natural and easy to read and reason about.
Is this valid? Is this thing active? Can I do this operation?
Anything built-in using this naming scheme is probably named like that for a reason.
Well the example is "isNumber". But I get what you mean!
Well I think everything is working now, my mini very crude calculator is working!
Yeah, that's the example here. Although try to image what you would call this bool if you didn't want to use that naming scheme. What name could you use that wouldnt be confusing?
Well, except the division result showing only the whole number and not fractional part...
Ah yes
That's another thing
I dunno, I think it's named pretty nicely
It is 🙂
That#s what I'm saying
I made the "c" a double, is that correct?
Kind of
Also holy cow you're of such help you've no idea
The problem here is a little thing called integer division
integers dont behave the way you would expect from "normal" math as you might be thinking about it.
Integers have no fractional part
They can only be whole numbers
So while
c
is a double, which would be correct, a
and b
are integers.
The result of an operation of only ints can only ever be another int.
So by the time you assign the result (which is an int) to you double, any fractional part that should be there is already goneSo I need to make all the variables into doubles?
Well, no not necessarily
You could
but to keep it simple, you could just cast one of your inputs to a double for example.
You only need one of them to be a double, for the operation to no longer be integer division, but dividing with fractional parts like you would expect.
e.g.
double c = (double)a / b;
should do the trick.IT WORKS
:D!!!
I am so happy even though I made only this
5 / 2
returns 2 because ints
5.0 / 2
returns 2.5 because one of them is a double 🙂I also noticed that even though I changed "a" and "b" into doubles, "c" produced errors because it was an int in some other line
Everyone needs to start somewhere, no need to put yourself down 😉
Ah yes, you can't re-declare a different variable with a name you've already used.
I am so happy
Now I want to export this so I can show to my friends who dont have Visual Studio
I suppose it's under "build" tab?
What editor/ide are you using?
Although honestly I won't know much whichever one it is 😄
Visual Studio 2022
There should be a drop down just under Build
That is probably set to Debug, which is how you've been testing your program I assume.
Ooo I see
And I need to "release" it?
If you set it to release and
Build
your project again, it should do it automatically
The default should be a bin
folder in your project directory.I SEE IT
Man i am beyond happy
Thank yo uso much you're a godsend
Can I message you privately on discord with some more help if I need it later?
Sure. No guarantees that I will have any idea but I can try.
Man I'm typing like I'm drunk today, I keep missing whole words.
it's best to make new help posts here so anyone can chime in (or keep using the same one if it's the same problem)
^this actually, I don't mind helping, but on balance I probably know less than average. You'd be missing out on many people here way more knowledgable than me and I would not be corrected if tell you something that's just plain wrong. @ineffable_
Yeah, makes sense.