Jump to content

  • Log In with Google      Sign In   
  • Create Account

#Actualpulpfist

Posted 22 January 2013 - 07:53 AM

I would make a loop so that the program continues. Also, if you make functions that will continue to ask for input until the input is valid, you don't really need a default

 

class Program
{
    static int GetOperand(string title)
    {
        int operand;
        while (true)
        {                
            Console.Write(title + "\n> ");
            if (int.TryParse(Console.ReadLine(), out operand))                
                break;                
            else Console.WriteLine("Invalid input. Try again");
        }
        return operand;
    }

    static string GetOperator(string title)
    {
        string op;
        while (true)
        {
            Console.Write(title + "\n> ");
            op = Console.ReadLine();
            if (op == "+" || op == "-" || op == "*" || op == "/")
                break;
            else Console.WriteLine("Invalid input. Try again");
        }
        return op;
    }

    static void Main(string[] args)
    {
        int num1, num2, answer=0;
        string op;
        while (true)
        {
            num1 = GetOperand("Write Your First Number");                                
            op = GetOperator("Write Your Operand");
            num2 = GetOperand("Write Your Second Number");                                
            switch (op)
            {
                case "+":
                    answer = num1 + num2;
                    break;
                case "-":
                    answer = num1 - num2;
                    break;
                case "*":
                    answer = num1 * num2;
                    break;
                case "/":
                    answer = num1 / num2;
                    break;                    
            }
            Console.WriteLine("Answer is " + answer.ToString());
        }
    }
}

 

 

edit: Don't forget to check for division by zero. If the denominator is zero, your program will crash

edit2: For a more flexible design, search for "reverse polish notation", as Mercile55 mentioned


#12pulpfist

Posted 22 January 2013 - 07:51 AM

I would make a loop so that the program continues. Also, if you make functions that will continue to ask for input until the input is valid, you don't really need a default

[source]
class Program
{
static int GetOperand(string title)
{
int operand;
while (true)
{
Console.Write(title + "\n> ");
if (int.TryParse(Console.ReadLine(), out operand))
break;
else Console.WriteLine("Invalid input. Try again");
}
return operand;
}

static string GetOperator(string title)
{
string op;
while (true)
{
Console.Write(title + "\n> ");
op = Console.ReadLine();
if (op == "+" || op == "-" || op == "*" || op == "/")
break;
else Console.WriteLine("Invalid input. Try again");
}
return op;
}

static void Main(string[] args)
{
int num1, num2, answer=0;
string op;
while (true)
{
num1 = GetOperand("Write Your First Number");
op = GetOperator("Write Your Operand");
num2 = GetOperand("Write Your Second Number");
switch (op)
{
case "+":
answer = num1 + num2;
break;
case "-":
answer = num1 - num2;
break;
case "*":
answer = num1 * num2;
break;
case "/":
answer = num1 / num2;
break;
}
Console.WriteLine("Answer is " + answer.ToString());
}
}
}
[source]

edit:
Don't forget to check for division by zero. If the denominator is zero, your program will crash

edit2:
For a more flexible design, search for "reverse polish notation", as Mercile55 mentioned

#11pulpfist

Posted 22 January 2013 - 05:16 AM

I would make a loop so that the program continues. Also, if you make functions that will continue to ask for input until the input is valid, you don't really need a default

example

[source]
class Program
{
static int GetOperand(string title)
{
int operand;
while (true)
{
Console.Write(title + "\n> ");
if (int.TryParse(Console.ReadLine(), out operand))
break;
else Console.WriteLine("Invalid input. Try again");
}
return operand;
}

static string GetOperator(string title)
{
string op;
while (true)
{
Console.Write(title + "\n> ");
op = Console.ReadLine();
if (op == "+" || op == "-" || op == "*" || op == "/")
break;
else Console.WriteLine("Invalid input. Try again");
}
return op;
}

static void Main(string[] args)
{
int num1, num2, answer=0;
string op;
while (true)
{
num1 = GetOperand("Write Your First Number");
op = GetOperator("Write Your Operand");
num2 = GetOperand("Write Your Second Number");
switch (op)
{
case "+":
answer = num1 + num2;
break;
case "-":
answer = num1 - num2;
break;
case "*":
answer = num1 * num2;
break;
case "/":
answer = num1 / num2;
break;
}
Console.WriteLine("Answer is " + answer.ToString());
}
}
}
[source]

edit:
Don't forget to check for division by zero. If the denominator is zero, your program will crash

edit2:
For a more flexible design, search for "reverse polish notation", as Mercile55 mentioned

#10pulpfist

Posted 22 January 2013 - 05:14 AM

I would make a loop so that the program continues. Also, if you make functions that will continue to ask for input until the input is valid, you don't really need a default

example

[source]
class Program
{
static int GetOperand(string title)
{
int operand;
while (true)
{
Console.Write(title + "\n> ");
if (int.TryParse(Console.ReadLine(), out operand))
break;
else Console.WriteLine("Invalid input. Try again");
}
return operand;
}

static string GetOperator(string title)
{
string op;
while (true)
{
Console.Write(title + "\n> ");
op = Console.ReadLine();
if (op == "+" || op == "-" || op == "*" || op == "/")
break;
else Console.WriteLine("Invalid input. Try again");
}
return op;
}

static void Main(string[] args)
{
int num1, num2, answer=0;
string op;
while (true)
{
num1 = GetOperand("Write Your First Number");
op = GetOperator("Write Your Operand");
num2 = GetOperand("Write Your Second Number");
switch (op)
{
case "+":
answer = num1 + num2;
break;
case "-":
answer = num1 - num2;
break;
case "*":
answer = num1 * num2;
break;
case "/":
answer = num1 / num2;
break;
}
Console.WriteLine("Answer is " + answer.ToString());
}
}
}
[source]

edit:
Don't forget to check for division by zero. If the denominator is zero, your program will crash

#9pulpfist

Posted 22 January 2013 - 05:13 AM

I would make a loop so that the program continues. Also, if you make functions that will continue to ask for input until the input is valid, you don't really need a default

example
[source]
class Program
{
static int GetOperand(string title)
{
int operand;
while (true)
{
Console.Write(title + "\n> ");
if (int.TryParse(Console.ReadLine(), out operand))
break;
else Console.WriteLine("Invalid input. Try again");
}
return operand;
}

static string GetOperator(string title)
{
string op;
while (true)
{
Console.Write(title + "\n> ");
op = Console.ReadLine();
if (op == "+" || op == "-" || op == "*" || op == "/")
break;
else Console.WriteLine("Invalid input. Try again");
}
return op;
}

static void Main(string[] args)
{
int num1, num2, answer=0;
string op;
while (true)
{
num1 = GetOperand("Write Your First Number");
op = GetOperator("Write Your Operand");
num2 = GetOperand("Write Your Second Number");
switch (op)
{
case "+":
answer = num1 + num2;
break;
case "-":
answer = num1 - num2;
break;
case "*":
answer = num1 * num2;
break;
case "/":
answer = num1 / num2;
break;
}
Console.WriteLine("Answer is " + answer.ToString());
}
}
}
[source]
edit:
Don't forget to check for division by zero. If the denominator is zero, your program will crash

#8pulpfist

Posted 22 January 2013 - 05:12 AM

I would make a loop so that the program continues. Also, if you make functions that will continue to ask for input until the input is valid, you don't really need a default

example

edit:
Don't forget to check for division by zero. If the denominator is zero, your program will crash

PARTNERS