Jump to content

I'm currently writing a code to run a calculator, but im having some trouble.

1)

I have 2 input text boxes and one output text box:

//Input

            int one = Convert.ToInt16(onetext.Text);
            int two = Convert.ToInt16(twotext.Text);
            

//Output
           int three = Convert.ToInt16(threetext.Text);

and when i try to Mod the two numbers like this:
int sum = one % two;

i get an error with the third text box, but not on any other equation type, divide, minus, add, multiply etc.

 

-----------------------------

2)

      when i divide the number, say i divide 10/3, it gives me 3, rather than the actual answer including decimal, does anyone know how to fix this?


this is all done in visual studio as a windows visual application if that helps :)

thanks in advance.

Link to comment
https://linustechtips.com/topic/413502-c-visual-studio-visual-calculator/
Share on other sites

Link to post
Share on other sites

I would suggest using

 

int one = int.Parse(oneText.Text);

etc etc etc...

 

But go on what is the question @Flanelman?

 

Processor : i7-6700k (stock speed)

Cooler : Hyper 212X

Motherboard: MSI Krait Gaming Z170

RAM : 4x4gb LPX Memory

Boot Device : Samsung 860 EVO

GPU : ZOTAC 1070 Extreme

PSU :Antec HCG-620M

Link to post
Share on other sites

Snip

Okay, think I replied before you were finished.

 

Not sure about your modulo '%' problem but for the decimal point things. Int can only hold whole numbers, use something like double or float and you'll get your decimal point.

 

Processor : i7-6700k (stock speed)

Cooler : Hyper 212X

Motherboard: MSI Krait Gaming Z170

RAM : 4x4gb LPX Memory

Boot Device : Samsung 860 EVO

GPU : ZOTAC 1070 Extreme

PSU :Antec HCG-620M

Link to post
Share on other sites

Okay, think I replied before you were finished.

 

Not sure about your modulo '%' problem but for the decimal point things. Int can only hold whole numbers, use something like double or float and you'll get your decimal point.

 

haha yeah sorry ia ccidentaly submitted when i was mid typing :P the mod one im not sure why it isn't working, but the divide one gives me an error if i change it to double. i changed the code to this:

 

            int one = Convert.ToInt16(onetext.Text);
            int two = Convert.ToInt16(twotext.Text);
            double three = Convert.ToDouble(threetext.Text);      **gives error here just like before
 
            double sum = one / two;
            threetext.Text = Convert.ToString(sum);
 
any ideas what ive done wrong?

thanks for your help btw :)

Link to post
Share on other sites

snip

 try casting to double.

 

double sum = double(one)/double(two)

or

double sum = double(one/two)

 

and if memory serves, you don't need to convert int to string you could just do it directly like

threetext.Text = sum;

 

hope this helps :)

 

Edit: Still don't get why your mod code won't work... should be working fine though...

 

Processor : i7-6700k (stock speed)

Cooler : Hyper 212X

Motherboard: MSI Krait Gaming Z170

RAM : 4x4gb LPX Memory

Boot Device : Samsung 860 EVO

GPU : ZOTAC 1070 Extreme

PSU :Antec HCG-620M

Link to post
Share on other sites

For your second issue, instead of converting the textbox into an integer, convert it to decimal.

 int one = Convert.ToDecimal(onetext.Text);

And do that for the other integers as well and then pass that value back into the calculators textbox.

Link to post
Share on other sites

got it all working, i deleted the line 

 

Additionally, could you provide the error message?

i was able to fix both errors by removing this from the code in both parts.

as well as changing one of the two remaining text boxes to double.. strange but got it all workign now :) i thought i'd post to et you know why the mod part wasnt workign haha :)

 

thanks for your help :)

int three = Convert.ToInt16(threetext.Text);

Link to post
Share on other sites

got it all working, i deleted the line 

 

i was able to fix both errors by removing this from the code in both parts.

as well as changing one of the two remaining text boxes to double.. strange but got it all workign now :) i thought i'd post to et you know why the mod part wasnt workign haha :)

 

thanks for your help :)

int three = Convert.ToInt16(threetext.Text);

Hmmmm... okay it works :) glad I could help :)

Processor : i7-6700k (stock speed)

Cooler : Hyper 212X

Motherboard: MSI Krait Gaming Z170

RAM : 4x4gb LPX Memory

Boot Device : Samsung 860 EVO

GPU : ZOTAC 1070 Extreme

PSU :Antec HCG-620M

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×