C
C#2y ago
rei

✅ Calculator Help

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
21 Replies
Angius
Angius2y ago
Simple. When you click numeric buttons, append that number to some variable. Perform calculations only when you press the math buttons The exact way actual calculators work
rei
reiOP2y ago
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 Oh wait uhhh Mmm
Angius
Angius2y ago
You can use a list to store those numbers, and then calculate the actual number from that Or use a string and later parse it
rei
reiOP2y ago
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(); but other than that iii think i understand ill come back if it anything doesnt work out correctly Alrighttt that works but how do i get the second number input after an operator button is clicked?
Pobiega
Pobiega2y ago
How about storing your current value before you clear the input?
rei
reiOP2y ago
i did label1.Text = num1.ToString(); Just to display the number as a string but id need to convert it back to int if i do calculations with it
Angius
Angius2y ago
int.Parse() and int.TryParse() would like to introduce themselves
rei
reiOP2y ago
Did that right after got an error for some reason public void button1_Click(object sender EventArgs) { num1 = Int.Parse(num1.ToString() + "1"); label1.Text = num1.ToString(); num1 = Int.Parse(); } no clue what im doing wrong
Anton
Anton2y ago
judging by that it's clear that you lack the fundamentals. I suggest studying those before jumping into a gui framework
rei
reiOP2y ago
Ehhhh i did some gui work on java so its fine basically the same syntax
Anton
Anton2y ago
you clearly don't understand how methods and parameters work
Servator
Servator2y ago
Servator
Servator2y ago
what this line supposed to do ?
rei
reiOP2y ago
convert from string back to int to be able to do operations on it with num2
Anton
Anton2y ago
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
Servator
Servator2y ago
Servator
Servator2y ago
num1 is already an int when you type "num1.ToString()" it doesn't change its type
rei
reiOP2y ago
o wait help im dumb
Servator
Servator2y ago
Did you solve @rei ?
rei
reiOP2y ago
yea all good ty
Servator
Servator2y ago
Type "/close" then 👍
Want results from more Discord servers?
Add your server