Jump to content

Help with C# Calculator

Clanscorpia

I've been learning C# and I've tried to make a calculator and here's my code. I'll explain it after.

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FirstConsoleProgram{    class Program    {        /// <summary>        ///         /// </summary>        /// <param name="args"></param>        static void Main(string[] args)        {                int num01;                int num02;            string Multiply = "multiply";            string Divide = "divide";            string Add = "add";            string Subtract = "subtract";            string userInput;            Console.Write("Write 'multiply' to multiply, 'add' to add, 'subtract' to subtract, or 'divide' to divide");              userInput = Console.ReadLine();            if (userInput = Multiply)            {                Console.Write("Type a number to multiply:  ");                num01 = Convert.ToInt32(Console.ReadLine());                Console.Write("Type another number:   ");                num02 = Convert.ToInt32(Console.ReadLine());                Console.WriteLine("The result is  " + num01 * num02);                    }                          }    }}

I'm trying to make a calculator that when you type in your operation (Add, Subtract, Multiply, Divide), but when I tried to make an IF statement to run a certain set of commands if you type in the operation you desire but I've come up upon a problem. In the IF statement, I get this error (I'm using Visual Studio Community 2015)
Error    CS0029    Cannot implicitly convert type 'string' to 'bool'    I get this on the command if (userInput = Multiply). Anyone that can help?
 

He who asks is stupid for 5 minutes. He who does not ask, remains stupid. -Chinese proverb. 

Those who know much are aware that they know little. - Slick roasting me

Spoiler

AXIOM

CPU- Intel i5-6500 GPU- EVGA 1060 6GB Motherboard- Gigabyte GA-H170-D3H RAM- 8GB HyperX DDR4-2133 PSU- EVGA GQ 650w HDD- OEM 750GB Seagate Case- NZXT S340 Mouse- Logitech Gaming g402 Keyboard-  Azio MGK1 Headset- HyperX Cloud Core

Offical first poster LTT V2.0

 

Link to comment
Share on other sites

Link to post
Share on other sites

userInput.equals(Multiply)

 

Also, I think swtich case is way better in this case.

switch(userInput){      case "Multiply" : //do stuff             break;      case "divide" : //do stuff             break;      default : //print that the user's input is invalid             break;  etc....}
Link to comment
Share on other sites

Link to post
Share on other sites

Ugh, I'm so dumb, should have noticed that from Arduino

He who asks is stupid for 5 minutes. He who does not ask, remains stupid. -Chinese proverb. 

Those who know much are aware that they know little. - Slick roasting me

Spoiler

AXIOM

CPU- Intel i5-6500 GPU- EVGA 1060 6GB Motherboard- Gigabyte GA-H170-D3H RAM- 8GB HyperX DDR4-2133 PSU- EVGA GQ 650w HDD- OEM 750GB Seagate Case- NZXT S340 Mouse- Logitech Gaming g402 Keyboard-  Azio MGK1 Headset- HyperX Cloud Core

Offical first poster LTT V2.0

 

Link to comment
Share on other sites

Link to post
Share on other sites

Got it :P Works now thanks

He who asks is stupid for 5 minutes. He who does not ask, remains stupid. -Chinese proverb. 

Those who know much are aware that they know little. - Slick roasting me

Spoiler

AXIOM

CPU- Intel i5-6500 GPU- EVGA 1060 6GB Motherboard- Gigabyte GA-H170-D3H RAM- 8GB HyperX DDR4-2133 PSU- EVGA GQ 650w HDD- OEM 750GB Seagate Case- NZXT S340 Mouse- Logitech Gaming g402 Keyboard-  Azio MGK1 Headset- HyperX Cloud Core

Offical first poster LTT V2.0

 

Link to comment
Share on other sites

Link to post
Share on other sites

Ack, another error 0.o. This one is with the subtraction part of the calculator. I don't think it wants me to use "-" for subtraction (Without the apostrophes). Here's my line. Any help?

Console.WriteLine("The result is  " + num01 - num02);

Error    CS0019    Operator '-' cannot be applied to operands of type 'string' and 'int'    There's my error. Thanks for the help guys!

He who asks is stupid for 5 minutes. He who does not ask, remains stupid. -Chinese proverb. 

Those who know much are aware that they know little. - Slick roasting me

Spoiler

AXIOM

CPU- Intel i5-6500 GPU- EVGA 1060 6GB Motherboard- Gigabyte GA-H170-D3H RAM- 8GB HyperX DDR4-2133 PSU- EVGA GQ 650w HDD- OEM 750GB Seagate Case- NZXT S340 Mouse- Logitech Gaming g402 Keyboard-  Azio MGK1 Headset- HyperX Cloud Core

Offical first poster LTT V2.0

 

Link to comment
Share on other sites

Link to post
Share on other sites

This line also isn't working. There aren't any errors but when I add something they just go together -.- . 7+3 got 73. Any help for this and the last one?

 if (userInput == Add)
            {
                Console.Write("Type a number to add to:  ");
                num01 = Convert.ToInt32(Console.ReadLine());
                Console.Write("Type another number:  ");
                num02 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("The result is  " + num01 + num02);
                Console.ReadKey();
            }

He who asks is stupid for 5 minutes. He who does not ask, remains stupid. -Chinese proverb. 

Those who know much are aware that they know little. - Slick roasting me

Spoiler

AXIOM

CPU- Intel i5-6500 GPU- EVGA 1060 6GB Motherboard- Gigabyte GA-H170-D3H RAM- 8GB HyperX DDR4-2133 PSU- EVGA GQ 650w HDD- OEM 750GB Seagate Case- NZXT S340 Mouse- Logitech Gaming g402 Keyboard-  Azio MGK1 Headset- HyperX Cloud Core

Offical first poster LTT V2.0

 

Link to comment
Share on other sites

Link to post
Share on other sites

Ack, another error 0.o. This one is with the subtraction part of the calculator. I don't think it wants me to use "-" for subtraction (Without the apostrophes). Here's my line. Any help?

Console.WriteLine("The result is  " + num01 - num02);

Error    CS0019    Operator '-' cannot be applied to operands of type 'string' and 'int'    There's my error. Thanks for the help guys!

"The result is  " + num01

produces a string

"The result is  " + num01 - num02

is trying to subtract from a string, which isn't possible. That's what the error is saying.

What you want is:

"The result is  " + (num01 - num02)

Putting the parenthesis forces num01 - num02 to be evaluated before appending the result to the string.

Link to comment
Share on other sites

Link to post
Share on other sites

This line also isn't working. There aren't any errors but when I add something they just go together -.- . 7+3 got 73. Any help for this and the last one?

 if (userInput == Add)

            {

                Console.Write("Type a number to add to:  ");

                num01 = Convert.ToInt32(Console.ReadLine());

                Console.Write("Type another number:  ");

                num02 = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine("The result is  " + num01 + num02);

                Console.ReadKey();

            }

This issue is very similar to above.

If num01 = 7 and num02 = 3:

"The result is  " + num01

equals the string "The result is 7"

"The result is 7" + 3

equals the string "The result is 73"

Link to comment
Share on other sites

Link to post
Share on other sites

Figured it out :P. Thanks though! But how do I put it as a program?

He who asks is stupid for 5 minutes. He who does not ask, remains stupid. -Chinese proverb. 

Those who know much are aware that they know little. - Slick roasting me

Spoiler

AXIOM

CPU- Intel i5-6500 GPU- EVGA 1060 6GB Motherboard- Gigabyte GA-H170-D3H RAM- 8GB HyperX DDR4-2133 PSU- EVGA GQ 650w HDD- OEM 750GB Seagate Case- NZXT S340 Mouse- Logitech Gaming g402 Keyboard-  Azio MGK1 Headset- HyperX Cloud Core

Offical first poster LTT V2.0

 

Link to comment
Share on other sites

Link to post
Share on other sites

Figured it out :P. Thanks though! But how do I put it as a program?

What do you mean exactly? Do you mean how to build it into a .exe file?

 

You can Build it in the Build menu. Then head over to your project folder and have a look into your binaries folder "/bin/"

Link to comment
Share on other sites

Link to post
Share on other sites

Yeah, but how do I make it so people can download it and use it?

He who asks is stupid for 5 minutes. He who does not ask, remains stupid. -Chinese proverb. 

Those who know much are aware that they know little. - Slick roasting me

Spoiler

AXIOM

CPU- Intel i5-6500 GPU- EVGA 1060 6GB Motherboard- Gigabyte GA-H170-D3H RAM- 8GB HyperX DDR4-2133 PSU- EVGA GQ 650w HDD- OEM 750GB Seagate Case- NZXT S340 Mouse- Logitech Gaming g402 Keyboard-  Azio MGK1 Headset- HyperX Cloud Core

Offical first poster LTT V2.0

 

Link to comment
Share on other sites

Link to post
Share on other sites

Yeah, but how do I make it so people can download it and use it?

 

You can create an installer for it. One option is WiX. Another option is ClickOnce. I'm sure there are others out there if those don't meet your needs.

 

Then host the installer somewhere (website, cloud storage, etc) so people can download it.

Link to comment
Share on other sites

Link to post
Share on other sites

Don't I need to put the source files or something or will the .exe be enough?

He who asks is stupid for 5 minutes. He who does not ask, remains stupid. -Chinese proverb. 

Those who know much are aware that they know little. - Slick roasting me

Spoiler

AXIOM

CPU- Intel i5-6500 GPU- EVGA 1060 6GB Motherboard- Gigabyte GA-H170-D3H RAM- 8GB HyperX DDR4-2133 PSU- EVGA GQ 650w HDD- OEM 750GB Seagate Case- NZXT S340 Mouse- Logitech Gaming g402 Keyboard-  Azio MGK1 Headset- HyperX Cloud Core

Offical first poster LTT V2.0

 

Link to comment
Share on other sites

Link to post
Share on other sites

Don't I need to put the source files or something or will the .exe be enough?

 

Since the best way to learn is by trying it:

Copy everything from your program from the bin folder to another one and remove one file after another. If it doesn't start it was probably needed.

 

If you have a Windows based Server somewhere you might want to have a look at WebDeployment.

Link to comment
Share on other sites

Link to post
Share on other sites

Copy everything from your program from the bin folder to another one and remove one file after another. If it doesn't start it was probably needed.

 

Sorry but that is just a ridiculous suggestion. Just don't and use WIX instead - it is very simple to put together an installer.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

-snip-

 

 

I would have done it like this.

 

 

 

Console.WriteLine("Answer is {0}", num1+num2);

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

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

×