Jump to content

Can someone explain this to me...?

- - - - -

  • Please log in to reply
7 replies to this topic

#1
Universal11

Universal11

    Newbie

  • Members
  • Pip
  • 3 posts
Hello guys! I am new in C++...This is my first project: A calculator! I've watched your Tutorial from here: but I did my calculator a little different...here there are some pictures of it:ImageShack: Host and Share your Photos and Videos - 51108110.jpg , ImageShack: Host and Share your Photos and Videos - 48911803.jpg , ImageShack: Host and Share your Photos and Videos - 27900239.png ; Can you explain me what "Prase" means...and that Double..I mean if you can explain me exactly WHY I had to use those codes? Also how can i make the Clear Button to "clear" the Textboxes? Thank you!...here it is the program too...I have some errors if i type a Sign withouth having numbers in Textbox1 and 2 ...look: Posted Image

Uploaded with ImageShack.us Can you help me ? Thank you alot!!! =) I use the Visual c# 2010 Version!:-P

Edited by Universal11, 30 October 2010 - 08:34 AM.


#2
Doctor

Doctor

    Newbie

  • Members
  • PipPip
  • 23 posts
Hi,

this error you get probably happen because empty text box. I didn't looked at code, so I cant tell u exactly, but you can avoid this by putting "if" sentence
Example:
if(textbox1.text != "" || textbox2.text != "") // then go on ...

If you want to clear textboxes you can do it like this: textbox1.text == ""; // for the first textbox ...

For other answers wait for experts :)


Regards

#3
Universal11

Universal11

    Newbie

  • Members
  • Pip
  • 3 posts

Doctor said:

Hi,

this error you get probably happen because empty text box. I didn't looked at code, so I cant tell u exactly, but you can avoid this by putting "if" sentence
Example:
if(textbox1.text != "" || textbox2.text != "") // then go on ...

Here it is the code:

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

namespace Calculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
      
            double val1, val2;

            val1 = double.Parse(textBox1.Text);

            val2 = double.Parse(textBox2.Text);

            textBox3.Text = (val1 + val2).ToString();
            

        }

        private void button2_Click(object sender, EventArgs e)
        {
            double val1, val2;

            val1 = double.Parse(textBox2.Text);

            val2 = double.Parse(textBox1.Text);

            textBox3.Text = (val1 - val2).ToString();


        }

        private void button3_Click(object sender, EventArgs e)
        {
            double val1, val2;

            val2 = double.Parse(textBox2.Text);

            val1 = double.Parse(textBox1.Text);

            textBox3.Text = (val1 * val2).ToString();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            double val1, val2;

            val2 = double.Parse(textBox2.Text);

            val1 = double.Parse(textBox1.Text);

            textBox3.Text = (val1 / val2).ToString();

        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";

        }

    
    }
}
So the if(textbox1.text != "" || textbox2.text != "") won't help! ...I think it is exactly the opposite (textbox1.text = "" || textbox2.text = "") ...! means NOT but at my file i must write something so that if one of the textboxes is empty it won't allow me to Add/extract/divide etc...Cuz if i press add/extract/divide and so on and one of the first 2 textboxes is empty I get error.. =( Anyway I found out by myself about clearing! Thank you for answer!

Edited by Alexander, 30 October 2010 - 02:48 PM.
Fixed incorrect bbcode


#4
paulinsight

paulinsight

    Newbie

  • Members
  • Pip
  • 1 posts
Hi...thanks for sharing. I like this idea

#5
Universal11

Universal11

    Newbie

  • Members
  • Pip
  • 3 posts
Uhh...Some moderator here? I really need help please!:crying:

#6
Doctor

Doctor

    Newbie

  • Members
  • PipPip
  • 23 posts

Universal11 said:

Here it is the code:
So the if(textbox1.text != "" || textbox2.text != "") won't help! ...I think it is exactly the opposite (textbox1.text = "" || textbox2.text = "") ...! means NOT but at my file i must write something so that if one of the textboxes is empty it won't allow me to Add/extract/divide etc...Cuz if i press add/extract/divide and so on and one of the first 2 textboxes is empty I get error.. =( Anyway I found out by myself about clearing! Thank you for answer!

I think it would help ... you check if textBox is NOT empty then calculate ...


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;


namespace Calculator

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        

        private void button1_Click(object sender, EventArgs e)

        {

            if(textBox1.text != "" && textBox2.text != "")

            {

                  double val1, val2;


                  val1 = double.Parse(textBox1.Text);


                  val2 = double.Parse(textBox2.Text);


                  textBox3.Text = (val1 + val2).ToString();

             }

             else MessageBox.show("Please fill in all required data!");

        }


        private void button2_Click(object sender, EventArgs e)

        {

            if(textBox1.text != "" && textBox2.text != "")

            {

                 double val1, val2;


                 val1 = double.Parse(textBox2.Text);


                 val2 = double.Parse(textBox1.Text);


                 textBox3.Text = (val1 - val2).ToString();


             }

             else MessageBox.show("Please fill in all required data!");



        }


        private void button3_Click(object sender, EventArgs e)

        {

            if(textBox1.text != "" && textBox2.text != "")

            {

                  double val1, val2;


                  val2 = double.Parse(textBox2.Text);


                  val1 = double.Parse(textBox1.Text);


                  textBox3.Text = (val1 * val2).ToString();

             }

             else MessageBox.show("Please fill in all required data!");


        }


        private void button4_Click(object sender, EventArgs e)

        {

            if(textBox1.text != "" && textBox2.text != "")

            {

                 double val1, val2;


                 val2 = double.Parse(textBox2.Text);


                 val1 = double.Parse(textBox1.Text);


                 textBox3.Text = (val1 / val2).ToString();

            }

            else MessageBox.show("Please fill in all required data!");


        }


        private void button5_Click(object sender, EventArgs e)

        {

            textBox1.Text = "";

            textBox2.Text = "";

            textBox3.Text = "";


        }


    

    }

}

Double is the type of variable. It is used for numbers which are not natural (with decimal point).
For example int is used for natural numbers and you can use it with addition, subtraction and multiplication since you are operating only with natural numbers.
For example if you use int for divide 14 with 5 (14/5), the result would be 2.
If you use double, the result would be 2,8.

Parse is there for converting string into double ... you can't operate with strings.

Edited by Doctor, 01 November 2010 - 03:55 AM.
Change || to &&


#7
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript
it should actually be

            if(textBox1.text != "" && textBox2.text != "")
            {...

Since both boxes have to be valid, not just one.

#8
Doctor

Doctor

    Newbie

  • Members
  • PipPip
  • 23 posts
Yes I was wrong :| .... && is correct.
At first I was thinking to make with || and if its true then display error .... :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users