C
C#17mo 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
Angius17mo 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
rei17mo 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
Angius17mo 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
rei17mo 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
Pobiega17mo ago
How about storing your current value before you clear the input?
rei
rei17mo 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
Angius17mo ago
int.Parse() and int.TryParse() would like to introduce themselves
rei
rei17mo 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
Anton17mo ago
judging by that it's clear that you lack the fundamentals. I suggest studying those before jumping into a gui framework
rei
rei17mo ago
Ehhhh i did some gui work on java so its fine basically the same syntax
Anton
Anton17mo ago
you clearly don't understand how methods and parameters work
Servator
Servator17mo ago
Servator
Servator17mo ago
what this line supposed to do ?
rei
rei17mo ago
convert from string back to int to be able to do operations on it with num2
Anton
Anton17mo 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
Servator17mo ago
Servator
Servator17mo ago
num1 is already an int when you type "num1.ToString()" it doesn't change its type
rei
rei17mo ago
o wait help im dumb
Servator
Servator17mo ago
Did you solve @rei ?
rei
rei17mo ago
yea all good ty
Servator
Servator17mo ago
Type "/close" then 👍