473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Calculator not functioning correctly

6
I have no clue why this is happening.

The intended behaviour is:
1. Passing strings of numbers using the numbered buttons (1..0).
2. Concatenate the strings above into one and converting it into an integer.
3. Select an operation ( +, -, * and /)
4. Passing strings of numbers using the numbered buttons (1..0).
5. Concatenate the strings above into one and converting it into an integer.
6. Perform the calculation and output the result to the same textbox.

The actual behaviour:
1. The program works well with single digits.
2. The problem starts when I use 2 digit numbers. The first click on the buttons displays the correct number in the text box.
3. When I click a second time to add a second digit the first value is over written with the new value.
4. The third click (number selection) adds the intended number correctly.


Samples of my code that perform the main operations:

A typical numbered button calls the method constructString:

Expand|Select|Wrap|Line Numbers
  1.  private void button8_Click(object sender, EventArgs e)
  2.         {
  3.             int j = 2;
  4.             string s = j.ToString(); 
  5.             constructString(s);
  6.         }
  7.  
  8. constructString builds the string, saves it into the variable nubString and passes it to textbox
  9.  
  10. private void constructString(String str)
  11.         {
  12.             if (numString == null)
  13.             {
  14.                 numString = str;
  15.  
  16.             }
  17.             else
  18.             {
  19.                 numString = numString + str;
  20.  
  21.             }
  22.  
  23.             textBox1.Text = numString;
  24.  
  25.         }
  26. This is followed by the following:
  27.  
  28. private void textBox1_TextChanged(object sender, EventArgs e)
  29.         {
  30.                  if (textBox1.Text != null && numString != null)
  31.                    {
  32.                         if (counter == 0)
  33.                         {
  34.                             v1 = PNNumber(numString);
  35.  
  36.                             //for test purposes
  37.                             textBox3.Text = v1.ToString();
  38.                             numString = null;
  39.                             counter++;
  40.                         }
  41.                         else
  42.                         {
  43.                             v2 = PNNumber(numString);
  44.  
  45.                             // for test purposes
  46.                             textBox3.Text = v2.ToString();
  47.                             numString = null;
  48.  
  49.                             counter = 0;
  50.  
  51.                         }
  52.  
  53.                   }
  54.  
  55.  
  56.         }
Oct 17 '08 #1
3 1088
r035198x
13,262 8TB
1.) Please use code tags when posting code.
2.) Why are you hard coding 2 in the button8_Click method?
3.) Surely you did not create 10 buttons and ten button_XX methods for each of the buttons?You can create those buttons in a loop and use one method to handle a click on them .
4.) What does PNNumber do?
Oct 17 '08 #2
sgmbuk
6
Hello and thanks for your response. What the PNNumber does the following:

1, Checks whether the value of the string is NOT null or not.
2. Converts the string value to integer. Using the Parse method.
3. Returnds the value

If the value of the string passed is null you will get the message 'NumString is null' printed into another another textbox.
Oct 17 '08 #3
sgmbuk
6
If I was to use a loop to create all the buttons where would I have to call that metho?
Oct 17 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Rafael | last post by:
Hi Everyone, I need some help with my calculator program. I need my program to do 2 arguments and a 3rd, but the 3rd with different operators. Any help would be great. Here is my code.... ...
3
by: Paul | last post by:
I want to make a simple calculator program but dont know where to get started. This is not GUI but a simple terminal program. It would get input like this Enter number: 5 + 10
10
by: collegestudent | last post by:
hello there, I was wondering if anyone could give me some advice on where to start on a project for college where I have to design and create a hand-held calculator simulation in Visual...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
19
by: TexasNewbie | last post by:
This was originally just a calculator without a decimal point. After I added the decimal, it now tells me invalid second number. //GUI Calculator Program import javax.swing.*; import...
0
by: battles | last post by:
There is a new freeware Win stand-alone RPN hex, binary, octal, decimal calculator at: www.simtel.net/search.phpsearchGulf+ Coastal+Softwaresimtel.net or ...
3
by: itsmichelle | last post by:
This is a very primative code of a java swing calculator. I have assigned all the number buttons and the operator buttons and I can add, subtract, multiply, and divide two numbers together. However,...
2
by: jtanz0 | last post by:
Im fairly new to java programming although i have some experience in python. I have to create a calculator program (command line) which takes as input a mathematical expression consisting of...
3
by: mandy335 | last post by:
public class Calculator { private long input = 0; // current input private long result = 0; // last input/result private String lastOperator = ""; // keeps track of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.