C
C#•16mo ago
ineffable

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
ineffable
ineffableOP•16mo ago
using System;

Console.WriteLine("VERY Simple Calculator v1.0 \nMade by Lucas Tumors (TM)\n");
Console.WriteLine("\nList of available operations:\n(a + b) - adds two numbers together\n(a - b) - subtracts one number from another\n(a * b) - multiplies two numbers together\n(a / b) - divides one number by another\n");

int a;
int b;

Console.WriteLine("\nChoose the first number:");

bool valid1 = false;
while (valid1 == false)
{
var stringNumber1 = Console.ReadLine();
int numericValue1;
bool isNumber1 = int.TryParse(stringNumber1, out numericValue1);

if (isNumber1 != true)
{
Console.WriteLine("You need to choose a valid number");
}
else
{

a = numericValue1;
valid1 = true;
}
}

Console.WriteLine("\nChoose the second number:");

bool valid2 = false;
while (valid2 == false)
{
var stringNumber2 = Console.ReadLine();
int numericValue2;
bool isNumber2 = int.TryParse(stringNumber2, out numericValue2);

if (isNumber2 != true)
{
Console.WriteLine("You need to choose a valid number");
}
else
{
b = numericValue2;
valid2 = true;
}

}

Console.WriteLine("\nChoose a mathematical operation:\n1. Addition (+)\n2. Subtraction (-)\n3. Multiplication (*)\n4. Division (/)");

bool valid3 = false;
while (valid3 == false)
{
var stringNumber3 = Console.ReadLine();
int numericValue3;
bool isNumber3 = int.TryParse(stringNumber3, out numericValue3);


if (isNumber3 != true || numericValue3 < 1 || numericValue3 > 4)
{
Console.WriteLine("You need to choose a valid option");
}
else if (isNumber3 == true && numericValue3 == 1)
{
int c = a + b;
Console.WriteLine("$The sum of the two equals {c}");
}
}
using System;

Console.WriteLine("VERY Simple Calculator v1.0 \nMade by Lucas Tumors (TM)\n");
Console.WriteLine("\nList of available operations:\n(a + b) - adds two numbers together\n(a - b) - subtracts one number from another\n(a * b) - multiplies two numbers together\n(a / b) - divides one number by another\n");

int a;
int b;

Console.WriteLine("\nChoose the first number:");

bool valid1 = false;
while (valid1 == false)
{
var stringNumber1 = Console.ReadLine();
int numericValue1;
bool isNumber1 = int.TryParse(stringNumber1, out numericValue1);

if (isNumber1 != true)
{
Console.WriteLine("You need to choose a valid number");
}
else
{

a = numericValue1;
valid1 = true;
}
}

Console.WriteLine("\nChoose the second number:");

bool valid2 = false;
while (valid2 == false)
{
var stringNumber2 = Console.ReadLine();
int numericValue2;
bool isNumber2 = int.TryParse(stringNumber2, out numericValue2);

if (isNumber2 != true)
{
Console.WriteLine("You need to choose a valid number");
}
else
{
b = numericValue2;
valid2 = true;
}

}

Console.WriteLine("\nChoose a mathematical operation:\n1. Addition (+)\n2. Subtraction (-)\n3. Multiplication (*)\n4. Division (/)");

bool valid3 = false;
while (valid3 == false)
{
var stringNumber3 = Console.ReadLine();
int numericValue3;
bool isNumber3 = int.TryParse(stringNumber3, out numericValue3);


if (isNumber3 != true || numericValue3 < 1 || numericValue3 > 4)
{
Console.WriteLine("You need to choose a valid option");
}
else if (isNumber3 == true && numericValue3 == 1)
{
int c = a + b;
Console.WriteLine("$The sum of the two equals {c}");
}
}
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
Jimmacle
Jimmacle•16mo ago
what errors do you currently have?
ineffable
ineffableOP•16mo ago
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?
hiyosilver
hiyosilver•16mo ago
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.
ineffable
ineffableOP•16mo ago
so at the beginning of the code I could do:
int a = 0
int b = 0
int a = 0
int b = 0
and it would assign them SOME value?
hiyosilver
hiyosilver•16mo ago
Specifically 0, yes 🙂 That should do it.
ineffable
ineffableOP•16mo ago
Okay, let me try then!
hiyosilver
hiyosilver•16mo ago
I don't know if you want to keep it separate for redability, but you could simplify this kind of thing
var stringNumber3 = Console.ReadLine();
int numericValue3;
bool isNumber3 = int.TryParse(stringNumber3, out numericValue3);

if (isNumber3 != true || numericValue3 < 1 || numericValue3 > 4)
{
Console.WriteLine("You need to choose a valid option");
}
var stringNumber3 = Console.ReadLine();
int numericValue3;
bool isNumber3 = int.TryParse(stringNumber3, out numericValue3);

if (isNumber3 != true || numericValue3 < 1 || numericValue3 > 4)
{
Console.WriteLine("You need to choose a valid option");
}
to
if(int.TryParse(Console.ReadLine(), out int numericValue ||
numericValue < 1 ||
numericValue > 4) {
...
}
if(int.TryParse(Console.ReadLine(), out int numericValue ||
numericValue < 1 ||
numericValue > 4) {
...
}
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
ineffable
ineffableOP•16mo ago
oh yeah, why assign
Console.ReadLine()
Console.ReadLine()
into a separate variable when I can just use the outputs of the
Console.ReadLine()
Console.ReadLine()
directly in the
int.TryParse()
int.TryParse()
, is this what you mean?
hiyosilver
hiyosilver•16mo ago
Yes.
ineffable
ineffableOP•16mo ago
Didn't know you could do that!
hiyosilver
hiyosilver•16mo ago
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 🙂
ineffable
ineffableOP•16mo ago
Definitely, I'm just doing it the very crude way just to get it working 😄
hiyosilver
hiyosilver•16mo ago
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.
ineffable
ineffableOP•16mo ago
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
hiyosilver
hiyosilver•16mo ago
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.
ineffable
ineffableOP•16mo ago
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!
hiyosilver
hiyosilver•16mo ago
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?
ineffable
ineffableOP•16mo ago
Well, except the division result showing only the whole number and not fractional part...
hiyosilver
hiyosilver•16mo ago
Ah yes That's another thing
ineffable
ineffableOP•16mo ago
I dunno, I think it's named pretty nicely
hiyosilver
hiyosilver•16mo ago
It is 🙂 That#s what I'm saying
ineffable
ineffableOP•16mo ago
else if (isNumber3 == true && numericValue3 == 4)
{
double c = a / b;
Console.WriteLine($"The result of division of the two equals {c}.");
valid3 = true;
}
else if (isNumber3 == true && numericValue3 == 4)
{
double c = a / b;
Console.WriteLine($"The result of division of the two equals {c}.");
valid3 = true;
}
I made the "c" a double, is that correct?
hiyosilver
hiyosilver•16mo ago
Kind of
ineffable
ineffableOP•16mo ago
Also holy cow you're of such help you've no idea
hiyosilver
hiyosilver•16mo ago
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 gone
ineffable
ineffableOP•16mo ago
So I need to make all the variables into doubles?
hiyosilver
hiyosilver•16mo ago
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.
ineffable
ineffableOP•16mo ago
IT WORKS :D!!! I am so happy even though I made only this
hiyosilver
hiyosilver•16mo ago
5 / 2 returns 2 because ints 5.0 / 2 returns 2.5 because one of them is a double 🙂
ineffable
ineffableOP•16mo ago
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
hiyosilver
hiyosilver•16mo ago
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.
ineffable
ineffableOP•16mo ago
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?
hiyosilver
hiyosilver•16mo ago
What editor/ide are you using? Although honestly I won't know much whichever one it is 😄
ineffable
ineffableOP•16mo ago
Visual Studio 2022
hiyosilver
hiyosilver•16mo ago
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.
ineffable
ineffableOP•16mo ago
Ooo I see And I need to "release" it?
hiyosilver
hiyosilver•16mo ago
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.
ineffable
ineffableOP•16mo ago
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?
hiyosilver
hiyosilver•16mo ago
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.
Jimmacle
Jimmacle•16mo ago
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)
hiyosilver
hiyosilver•16mo ago
^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_
ineffable
ineffableOP•16mo ago
Yeah, makes sense.
Want results from more Discord servers?
Add your server