I have a problem creating a Calculator in console app

Help me!!

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 3510 Views - Last Post: 08 June 2009 - 08:47 AM Rate Topic: -----

#1 source144  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-June 09

I have a problem creating a Calculator in console app

Posted 06 June 2009 - 10:49 PM

look i'm doing a calculator in c# console application but it wont work..
it wont get the parameter (+, -, x, / [ x is equal to * i don't know if i will change..])

heres the code...:

[IT IS NOT DONE YET!]
{AFTER I KNOW HOW TO GET the parameters i will know how to get words and i will ad commands}-i think..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class Addition
{
	public static void Main(string[] args)
	{
		int number1;
		int number2;
		int sum;
		char selection;

		Console.WriteLine("Wellcome to our 1st program! (this is a calculator XD)");

		Console.WriteLine("Please Enter a Number or one of these commands:");
		number1 = Convert.ToInt32(Console.ReadLine());
		Console.WriteLine("You're Numer is: {0}", number1);
		selection = Convert.ToChar(Console.ReadLine());
		Console.WriteLine("Youre Selection var is: {0}", selection);

		if ("selection" == "+")
		{
			int math1 = 1;
			Console.WriteLine("You're Current Number and parameter are: {0} {1}", number1, math1);
		}
		if ("selection" == "-")
		{
			int math1 = 2;
			Console.WriteLine("You're Current Number and parameter are: {0} {1}", number1, math1);
		}
		if ("selection" == "x")
		{
			int math1 = 3;
			Console.WriteLine("You're Current Number and parameter are: {0} {1}", number1, math1);
		}
		if ("selection" == "/")
		{
			int math1 = 4;
			Console.WriteLine("You're Current Number and parameter are: {0} {1}", number1, math1);
		}
		if ("selection" != "+")
		{
			if ("selection" != "-")
			{
				if ("selection" != "x")
				{
					if ("selection" != "/")
					{
						Console.WriteLine("Syntax ERROR!, You're parameter must be +, x, - or /");
						Console.WriteLine("Please try again..");
						Console.WriteLine("You're Number is still {0} to remove it write command CE", number1);
						selection = Convert.ToChar(Console.ReadLine());
						if ("selection" == "+")
						{
							int math1 = 1;
							Console.WriteLine("You're Current Number and parameter are: {0} {1}", number1, math1);
						}
						if ("selection" == "-")
						{
							int math1 = 2;
							Console.WriteLine("You're Current Number and parameter are: {0} {1}", number1, math1);
						}
						if ("selection" == "x")
						{
							int math1 = 3;
							Console.WriteLine("You're Current Number and parameter are: {0} {1}", number1, math1);
						}
						if ("selection" == "/")
						{
							int math1 = 4;
							Console.WriteLine("You're Current Number and parameter are: {0} {1}", number1, math1);
						}
					}
				}
			}
		}
		Console.WriteLine("Enter Your Second Number For Addition Please: ");
		number2 = Convert.ToInt32(Console.ReadLine());
		Console.WriteLine("The second number is: {0}", number2);
		if ("math1" == "1")
		{
			int ans3;
			ans3 = number1 + number2;
			Console.WriteLine("^Calc	  {0} + {1} = {2}", number1, number2, ans3);
		}
		if ("math1" == "2")
		{
			int ans3;
			ans3 = number1 - number2;
			Console.WriteLine("^Calc	  {0} - {1} = {2}", number1, number2, ans3);
		}
		if ("math1" == "3")
		{
			int ans3;
			ans3 = number1 * number2;
			Console.WriteLine("^Calc	  {0} * {1} = {2}", number1, number2, ans3);
		}
		if ("math1" == "4")
		{
			int ans3;
			ans3 = number1 / number2;
			Console.WriteLine("^Calc	  {0} / {1} = {2}", number1, number2, ans3);
		}
		Console.Read();
	}
}


early thanks...
and if you won't understand ask me what i meant

This post has been edited by source144: 06 June 2009 - 11:05 PM


Is This A Good Question/Topic? 0
  • +

Replies To: I have a problem creating a Calculator in console app

#2 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: I have a problem creating a Calculator in console app

Posted 06 June 2009 - 11:10 PM

what is your problem i mean the Error.
Was This Post Helpful? 0
  • +
  • -

#3 zombie_chan51  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 327
  • Joined: 16-March 08

Re: I have a problem creating a Calculator in console app

Posted 06 June 2009 - 11:14 PM

You put quotes on the char variable Selection
get rid of the quotes, might hep

This post has been edited by drjoe51: 06 June 2009 - 11:14 PM

Was This Post Helpful? 0
  • +
  • -

#4 zombie_chan51  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 327
  • Joined: 16-March 08

Re: I have a problem creating a Calculator in console app

Posted 06 June 2009 - 11:20 PM

And Single Quotes on the Characters
Was This Post Helpful? 0
  • +
  • -

#5 zombie_chan51  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 327
  • Joined: 16-March 08

Re: I have a problem creating a Calculator in console app

Posted 06 June 2009 - 11:25 PM

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class Addition
{
	public static void Main(string[] args)
	{
		int number1;
		int number2;
		int sum;
		char selection;
		int math1;

		Console.WriteLine("Wellcome to our 1st program! (this is a calculator XD)");

		Console.WriteLine("Please Enter a Number or one of these commands:");
		number1 = Convert.ToInt32(Console.ReadLine());
		Console.WriteLine("You're Numer is: {0}", number1);
		selection = Convert.ToChar(Console.ReadLine());
		Console.WriteLine("Youre Selection var is: {0}", selection);

		if (selection == '+')
		{
			math1 = 1;
			Console.WriteLine("You're Current Number and parameter are: {0} {1}", number1, math1);
		}
		else if (selection == '-')
		{
			math1 = 2;
			Console.WriteLine("You're Current Number and parameter are: {0} {1}", number1, math1);
		}
		else if (selection == 'x')
		{
			math1 = 3;
			Console.WriteLine("You're Current Number and parameter are: {0} {1}", number1, math1);
		}
		else if (selection == '/')
		{
			math1 = 4;
			Console.WriteLine("You're Current Number and parameter are: {0} {1}", number1, math1);
		}
		else
		{
			Console.WriteLine("Syntax ERROR!, You're parameter must be +, x, - or /");
			Console.WriteLine("Please try again..");
			Console.WriteLine("You're Number is still {0} to remove it write command CE", number1);
			selection = Convert.ToChar(Console.ReadLine());
			math1 = 0;
		}
	
		Console.WriteLine("Enter Your Second Number For Addition Please: ");
		number2 = Convert.ToInt32(Console.ReadLine());
		Console.WriteLine("The second number is: {0}", number2);
		if (math1 == 1)
		{
			int ans3;
			ans3 = number1 + number2;
			Console.WriteLine("^Calc	  {0} + {1} = {2}", number1, number2, ans3);
		}
		else if (math1 == 2)
		{
			int ans3;
			ans3 = number1 - number2;
			Console.WriteLine("^Calc	  {0} - {1} = {2}", number1, number2, ans3);
		}
		else if (math1 == 3)
		{
			int ans3;
			ans3 = number1 * number2;
			Console.WriteLine("^Calc	  {0} * {1} = {2}", number1, number2, ans3);
		}
		else if (math1 == 4)
		{
			int ans3;
			ans3 = number1 / number2;
			Console.WriteLine("^Calc	  {0} / {1} = {2}", number1, number2, ans3);
		}
		Console.Read();
	}
}



Try this
Was This Post Helpful? 1
  • +
  • -

#6 source144  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-June 09

Re: I have a problem creating a Calculator in console app

Posted 07 June 2009 - 12:10 AM

man you are great!! thanks, but now i need to add the commands like: C, CE, S, L, Q
C = Clear
CE = clear all
S = Save answer
L = load answer..
Q = Quit

now it works..

but i have a few questions,
how do i make a loop, if you write 'o' that doesn't do nothing in my calculator then it will say error but the how can i loop??

and how can i check if you said number if not it will be error and ask you to say a number again
Was This Post Helpful? 0
  • +
  • -

#7 zombie_chan51  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 327
  • Joined: 16-March 08

Re: I have a problem creating a Calculator in console app

Posted 07 June 2009 - 12:24 AM

do you mean something like this
if (command == "o")
		{
			while(true)
			{
				Console.WriteLine("ERROR");
			}
		}
		else
		{

		}


Was This Post Helpful? 0
  • +
  • -

#8 source144  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-June 09

Re: I have a problem creating a Calculator in console app

Posted 07 June 2009 - 02:35 AM

View Postdrjoe51, on 6 Jun, 2009 - 11:24 PM, said:

do you mean something like this
if (command == "o")
		{
			while(true)
			{
				Console.WriteLine("ERROR");
			}
		}
		else
		{

		}



i mean if you didn't write a number (you wrote something else)
so it will say "error, you must write a number" and then it will come back to the stage that you need to write the number instead of making a bug in the whole program and then continue...
and instead of making the same thing for alot of time can i do a loop on the program?? (without the start text)..?
and how do i "quit" ( a command the gets out of this console app..0
Was This Post Helpful? 0
  • +
  • -

#9 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: I have a problem creating a Calculator in console app

Posted 07 June 2009 - 06:43 AM

try this...

{
    static void Main(string[] args)
    {
        string operation = string.Empty;
        int number1 = 0;
        int number2 = 0;
        int answer = 0;

        Console.WriteLine("Welcome to our 1st program! (this is a calculator XD)");

        while (true)
        {
            string input = string.Empty;

            if (number1 == 0)
            {
                Console.Write("Please Enter a first Number or Q to Quit: ");
                input = Console.ReadLine();
                if (input.ToUpper() == "Q")
                    break;

                if (!int.TryParse(input, out number1))
                    Console.WriteLine("Invalid Input");
            }
            else if (operation == string.Empty)
            {
                Console.Write("Please Enter an operator or Q to Quit: ");
                input = Console.ReadLine();
                if (input.ToUpper() == "Q")
                    break;

                if (input == "+" || input == "-" || input == "x" || input == "/")
                    operation = input;
                else
                    Console.WriteLine("Invalid Input");
            }
            else if (number2 == 0)
            {
                Console.Write("Please Enter a second Number or Q to Quit: ");
                input = Console.ReadLine();
                if (input.ToUpper() == "Q")
                    break;

                if (!int.TryParse(input, out number2))
                    Console.WriteLine("Invalid Input");
            }
            
            // a check to make sure the input passed
            if (operation != string.Empty && number1 != 0 && number2 != 0)
            {
                if (operation == "+")
                    answer = number1 + number2;
                else if (operation == "-")
                    answer = number1 - number2;
                else if (operation == "x")
                    answer = number1 * number2;
                else if (operation == "/")
                    answer = number1 / number2;

                Console.WriteLine("Calc     {0} {1} {2} = {3}", number1, operation, number2, answer);

                Console.WriteLine("");

                operation = string.Empty;
                number1 = 0;
                number2 = 0;
                answer = 0;
            }
        }
    }
}


Was This Post Helpful? 1

#10 source144  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-June 09

Re: I have a problem creating a Calculator in console app

Posted 07 June 2009 - 12:05 PM

Thanks i'm going to rewrite it and expand it..
Was This Post Helpful? 0
  • +
  • -

#11 C++++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-November 07

Re: I have a problem creating a Calculator in console app

Posted 07 June 2009 - 06:02 PM

I think it would be better if you use switch cases

This post has been edited by C++++: 07 June 2009 - 06:03 PM

Was This Post Helpful? 0
  • +
  • -

#12 source144  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-June 09

Re: I have a problem creating a Calculator in console app

Posted 08 June 2009 - 12:09 AM

How do I convert the .cs (c# project into a exe??)
into a .exe
Was This Post Helpful? 0
  • +
  • -

#13 Bocard  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 223
  • Joined: 24-September 08

Re: I have a problem creating a Calculator in console app

Posted 08 June 2009 - 01:53 AM

View Postsource144, on 8 Jun, 2009 - 08:09 AM, said:

How do I convert the .cs (c# project into a exe??)
into a .exe

you press F5 or go in the menu Debug-> Start debugging after that you go where your project is saved and in the bin folder and then in the debug folder you will have the .exe you want
Was This Post Helpful? 0
  • +
  • -

#14 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: I have a problem creating a Calculator in console app

Posted 08 June 2009 - 07:10 AM

View Postsource144, on 8 Jun, 2009 - 02:09 AM, said:

How do I convert the .cs (c# project into a exe??)
into a .exe


note whether you are in DEBUG or RELEASE mode.

Ctrl + SHIFT + B

after it has completed building, navigate to the solution directory --> project directory --> Debug/Release(depending on the mode you were in) --> bin -->

you will then see your .exe.
Was This Post Helpful? 1

#15 source144  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-June 09

Re: I have a problem creating a Calculator in console app

Posted 08 June 2009 - 08:08 AM

View Posteclipsed4utoo, on 8 Jun, 2009 - 06:10 AM, said:

View Postsource144, on 8 Jun, 2009 - 02:09 AM, said:

How do I convert the .cs (c# project into a exe??)
into a .exe


note whether you are in DEBUG or RELEASE mode.

Ctrl + SHIFT + B

after it has completed building, navigate to the solution directory --> project directory --> Debug/Release(depending on the mode you were in) --> bin -->

you will then see your .exe.



thank you worked, how do i switch the icon?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2