Making A Calculator!
Posted: Tue Jan 15, 2008 3:34 am
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.
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.
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)
{
}
}
}
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.