C
Join ServerC#
help
✅ Calculator Help
Rrei2/25/2023
Basically im making a calculator with 9 number buttons. How do i make it so that when 2 number buttons that are pressed in a row, ex. 1 and 1. Display and calculate as 11 as in eleven and not (1 + 1) as in 2
thx lmk if u need all the code so far
thx lmk if u need all the code so far
AAngius2/25/2023
Simple. When you click numeric buttons, append that number to some variable. Perform calculations only when you press the math buttons
AAngius2/25/2023
The exact way actual calculators work
Rrei2/25/2023
Yes but so. If only numeric button 1 is pressed then the variable would have 1 in it. If its pressed again wouldn't the variable now have 2 inside? not 11? Help idk sorry
Rrei2/25/2023
Oh wait uhhh
Rrei2/25/2023
Mmm
AAngius2/25/2023
You can use a list to store those numbers, and then calculate the actual number from that
AAngius2/25/2023
Or use a string and later parse it
Rrei2/25/2023
Basically what i was gonna do is store the 2 different numbers that the user wants to calculate, store tje result as an int, and then display the result in the form of a label using result.ToString();
Rrei2/25/2023
but other than that
Rrei2/25/2023
iii think i understand ill come back if it anything doesnt work out correctly
Rrei2/25/2023
Alrighttt that works but how do i get the second number input after an operator button is clicked?
PPobiega2/25/2023
How about storing your current value before you clear the input?
Rrei2/25/2023
i did label1.Text = num1.ToString();
Rrei2/25/2023
Just to display
Rrei2/25/2023
the number
Rrei2/25/2023
as a string
Rrei2/25/2023
but id need to convert it back to int if i do calculations with it
AAngius2/25/2023
int.Parse()
and int.TryParse()
would like to introduce themselvesRrei2/25/2023
Did that
Rrei2/25/2023
right after
Rrei2/25/2023
got an error for some reason
Rrei2/25/2023
public void button1_Click(object sender EventArgs) {
num1 = Int.Parse(num1.ToString() + "1");
label1.Text = num1.ToString();
num1 = Int.Parse();
}
num1 = Int.Parse(num1.ToString() + "1");
label1.Text = num1.ToString();
num1 = Int.Parse();
}
Rrei2/25/2023
no clue what im doing wrong
AAntonC2/25/2023
judging by that it's clear that you lack the fundamentals. I suggest studying those before jumping into a gui framework
Rrei2/25/2023
Ehhhh i did some gui work on java so its fine
Rrei2/25/2023
basically the same syntax
AAntonC2/25/2023
you clearly don't understand how methods and parameters work
SSamarichitane2/25/2023

SSamarichitane2/25/2023
what this line supposed to do ?
Rrei2/25/2023
convert from string back to int to be able to do operations on it with num2
AAntonC2/25/2023
also gui frameworks are best used with bindings and reactivity, which is an abstraction that's hard to understand for beginners who lack basics, but it's the most efficient way to program guis
SSamarichitane2/25/2023

SSamarichitane2/25/2023
num1 is already an int
SSamarichitane2/25/2023
when you type "num1.ToString()" it doesn't change its type
Rrei2/25/2023
o wait help im dumb
SSamarichitane2/25/2023
Did you solve @rei ?
Rrei2/25/2023
yea all good
Rrei2/25/2023
ty
SSamarichitane2/25/2023
Type "/close" then 👍