Mohammed Musekula

Mohammed Musekula

  • NA
  • 36
  • 10.9k

There isa little mistake with this Calculator

Mar 31 2015 4:14 AM

 I wonder if someone can help with this;

there is a a bit of mistake on this calculator which i can't figure.
 
it works ok but i added a feature which i can't figure out to align properly
 
thank you. 
 
 
public static int operation { get; set; }

private static double EndProgram;

static void Main(string[] args)

{

int firstNumber;

int secondNumber;

string operand = string.Empty;

float result;

string text1;

do

{

Console.WriteLine("Please choose your option ");

Console.WriteLine("--------------------------------------------------------------------------------");

Console.WriteLine("1. Addition."

+ Environment.NewLine

+ "2. Substraction"

+ Environment.NewLine

+ "3. Multiplication"

+ Environment.NewLine

+ "4. Division"

+ Environment.NewLine

+ "5. Modulus"

+ Environment.NewLine

+ "6. End Program");


Console.WriteLine("--------------------------------------------------------------------------------");

////enter first number ////

Console.Write("Please enter a number: ");

text1 = Console.ReadLine();


// if number not integer then fail ////

bool res = int.TryParse(text1, out firstNumber);

while (!res)

{

Console.WriteLine(" FAIL");

////enter first number ////

Console.Write("Please enter a number: ");

text1 = Console.ReadLine();

res = int.TryParse(text1, out firstNumber);

}

//// enter operand ////

while (operand == string.Empty || operand.Length > 1 || !(new char[] { '+', '-', '*', '/' }).Contains(char.Parse(operand)))

{

Console.Write("Please enter an operand (+, -, /, *): ");

operand = Console.ReadLine();

}

// enter second number //

Console.Write("Please enter the second number: ");

text1 = Console.ReadLine();

// if number not integer then fail //

bool eff = int.TryParse(text1, out secondNumber);

while (!eff)

{

Console.WriteLine(" FAIL");

// enter second number //

Console.Write("Please enter the second number: ");

text1 = Console.ReadLine();

eff = int.TryParse(text1, out secondNumber);

// Console.Write("\n\tYour Choice : ");

// operand = Console.ReadLine();


// Console.WriteLine("\n\tPlease enter two numbers");

//Console.Write("\n\tfirstNmber : ");

//firstNumber = int.Parse(Console.ReadLine());

// Console.Write("\n\tsecondNumber : ");

// secondNumber = int.Parse(Console.ReadLine());

switch (operation)

{

case 1: result = firstNumber + secondNumber;

break;


case 2: result = firstNumber - secondNumber;

break;

case 3: result = firstNumber * secondNumber;

break;

case 4: result = firstNumber / secondNumber;

break;

if (secondNumber == 0)

{

Console.WriteLine("Divide By Zero Error");

return;

}

case 5: result = firstNumber % secondNumber;

break;

default:

Console.WriteLine("Wrong Choice!!");

result = 0;

break;

Console.WriteLine("{0} {1} {2} = {3}", firstNumber, operand, secondNumber, result);

/// converts numbers to string using operand and writes final line ///

Console.WriteLine(firstNumber.ToString() + " " + operand + " " + secondNumber.ToString() + " = "+ result.ToString());

Console.ReadLine();

}

}

}


Answers (2)