Making A Calculator!

Discuss anything programming related.
Locked
User avatar
Aumaan Anubis
Staff
Posts: 1812
Joined: Thu Dec 13, 2007 12:18 am
Contact:

Making A Calculator!

Post by Aumaan Anubis »

Well, as many know, I'm trying to learn C# by scanning through lines of code. It's helped me, but only to a certain extent. I'm currently trying to make a calculator in C#, and so far, it's gone pretty well. Also, this isn't my code. It's WaywornMmmmm's over at HM. I followed a tutorial of his. I PMed him about the issue, but he said that he made it awhile back, and he doesn't really know enough.

The problem is, that the calculator doesn't accept decimals. Nor is its output ever a decimal. For instance, 1/2 will equal 0. If you input 1.2 or any other decimal into the textbox to do an operation, a runtime error occurs, stating that the input was not in the correct format. I'm at a loss as for what to do.

Code: Select all

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Basic_Calculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnsolve_Click(object sender, EventArgs e)
        {
            /*The first line defines a string with the value of 
              tbNumberA's text and the second with tbNumberB's text. 
              After that we create two variable with values of 0*/
            string number1 = tbNumberA.Text;
            string number2 = tbNumberB.Text;
            if (number1 == "" || number2 == "")
            {
                MessageBox.Show("Please make sure that you entered your values correctly!", "Value Error");
            }
            else
            {
                int v_Number1 = 0;
                int v_Number2 = 0;
                /*These next statements convert number1 and number2 
                  to variables.*/
                v_Number1 += Convert.ToInt32(number1);
                v_Number2 += Convert.ToInt32(number2);
                /*This is an if statement.  I do realize that a switch 
                  statement would be more practical, but I don't want 
                  to overload you with information.*/
                if (rbAdd.Checked)
                {
                    //Creates a Variable for the solution 
                    int v_Solution = v_Number1 + v_Number2;
                    MessageBox.Show(Convert.ToString(v_Solution));
                }
                else
                {
                }
                   
                if (rbSubtract.Checked)
                {
                    int v_Solution = v_Number1 - v_Number2;
                    MessageBox.Show(Convert.ToString(v_Solution));
                }
                else
                {
                }
                if (rbMultiply.Checked)
                {
                    int v_Solution = v_Number1 * v_Number2;
                    MessageBox.Show(Convert.ToString(v_Solution));
                }
                else
                {
                }
                if (rbDivide.Checked)
                {
                    int v_Solution = v_Number1 / v_Number2;
                    MessageBox.Show(Convert.ToString(v_Solution));
                }
                else
                {
                }
                if (!rbAdd.Checked && !rbSubtract.Checked && !rbMultiply.Checked && !rbDivide.Checked)
                {
                    //The MessageBox.Show method shows a messagebox of the text inside the quotations 
                    MessageBox.Show("Please make sure you have specified an operation!", "Value Error");
                }
                else
                {
                }
            }
        }
        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Notice that we included a ,"About" after the first text.  This will appear in the title bar. 
            MessageBox.Show("This application was created by me, Aumaan Anubis. It is my first application created in C#, following a tutorial by WaywornMmmmm. ", "About");
        }

        private void usageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Put your first number in the top textbox and the second number in the bottom textbox.  Then select an operation and click the Solve button.", "Help"); 
        }
        private void tbNumberA_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsLetter(e.KeyChar))
            {
                e.Handled = true;
            }
            if (char.IsSeparator(e.KeyChar))
            {
                e.Handled = true;
            } 
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
There's the code for the application. If you want to manually edit my code for me, and give it back to me and it'll work correctly, please go ahead. If you'd like to give me a hint, therefore making me do some actual work/research, that's cool too.

I personally think that the issue is that the variables are strings, and strings may not accept decimals. I'm going to experiment for a bit, and then come back to see if anyone has replied.
Supermodder911
Posts: 409
Joined: Sat Jan 12, 2008 11:42 pm
Location: Michigan
Contact:

Making A Calculator!

Post by Supermodder911 »

It cannot do decimals cause you have the 2 numbers defined as ints there for it can only store int numbers And can only be in range of -2,000,000 to 2,000,000(Son't remember the exact value but its within 2 million).
Image
Chad Warden is Ballin'. No jk.
User avatar
Aumaan Anubis
Staff
Posts: 1812
Joined: Thu Dec 13, 2007 12:18 am
Contact:

Making A Calculator!

Post by Aumaan Anubis »

So, I was correct?

I just basically have to change everything in the code from int to....?
Supermodder911
Posts: 409
Joined: Sat Jan 12, 2008 11:42 pm
Location: Michigan
Contact:

Making A Calculator!

Post by Supermodder911 »

You could do that but... then numbers like 324134 will error........
Image
Chad Warden is Ballin'. No jk.
User avatar
Aumaan Anubis
Staff
Posts: 1812
Joined: Thu Dec 13, 2007 12:18 am
Contact:

Making A Calculator!

Post by Aumaan Anubis »

Ok, then that doesn't fix the problem.
User avatar
Prey
Posts: 129
Joined: Sat Dec 29, 2007 5:06 pm
Location: UK

Making A Calculator!

Post by Prey »

Stringc can store decimals.. integers only store whole numbers, that's why you're not seeing decimals..

Just replace 'int' with 'decimal'.. and 'Convert.ToInt32' with 'decimal.Parse'..
User avatar
Aumaan Anubis
Staff
Posts: 1812
Joined: Thu Dec 13, 2007 12:18 am
Contact:

Making A Calculator!

Post by Aumaan Anubis »

decimal does not contain a definition for parse.


^^ error message.
User avatar
Aumaan Anubis
Staff
Posts: 1812
Joined: Thu Dec 13, 2007 12:18 am
Contact:

Making A Calculator!

Post by Aumaan Anubis »

Nevermind, I fixed it.

Apparently, C# is more sensitive to capitilization than I remember VB being. I'm probably wrong though.

Thanks Prey.
User avatar
Prey
Posts: 129
Joined: Sat Dec 29, 2007 5:06 pm
Location: UK

Making A Calculator!

Post by Prey »

VB is not case-sensitive, C# is.
User avatar
Aumaan Anubis
Staff
Posts: 1812
Joined: Thu Dec 13, 2007 12:18 am
Contact:

Making A Calculator!

Post by Aumaan Anubis »

ah, well, I guess I was correct.

Problem solved, so Locked.
Locked