Hello:)
This program is a maths calculator that should return to the menu so you can go round again when you have done one calculation. After great despair I think I have somehow got most of it to work?
However if I input something Invalid, say the number 8 it just asks to input the first number for a calculation again. I want it to do whatever is appropriate for default. I have text in there now but it doesn't output! I have been studying Java basics and am now starting on C# but don't seem to be the sharpest knife in the drawer where coding is concerned despite alot of effort so would be grateful for any help, advice, knowledge, and probably pschiatric care if I don't improve at this coding business soon. Here's the code - thanks for looking.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace mathsApp_Task_1._1
{
class Program
{
static void Main(string[] args)
{
int counter = 0;
int calChoi = 0;
while (calChoi != 5)
{//start while loop
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Hello, please choose from the following calculations");
Console.WriteLine("(1) Add (2) Subtract (3) Multiply (4) Divide or (5) Exit");
Console.WriteLine();
Console.WriteLine();
calChoi = int.Parse(Console.ReadLine());
counter += 1;//increment loop by one
if (calChoi == 5)
{
Console.WriteLine(" Goodbye and thanks for using this program - see you next time");
Console.WriteLine();
Console.WriteLine();
}
else
{//start else - don't know if the loop should be in here? But it seems to work
Console.WriteLine("Please enter the first number ");
float num1 = float.Parse(Console.ReadLine());
Console.WriteLine("Please enter the second number");
float num2 = float.Parse(Console.ReadLine());
MathsCalculator MUtil;
MUtil = new MathsCalculator();
float answer = 0;
switch (calChoi)
{
case 1:
answer = MUtil.Added(num1, num2);
Console.Write(num1 + " added to " + num2 + " is " + answer);
break;
case 2:
answer = MUtil.Subtracted(num1, num2);
Console.Write(num1 + " taken from " + num2 + " is " + answer);
break;
case 3:
answer = MUtil.Multiplied(num1, num2);
Console.Write(num1 + " multiplied by " + num2 + " is " + answer);
break;
case 4:
answer = MUtil.Divided(num1, num2);
Console.Write(num1 + " divided by " + num2 + " is " + answer);
break;
default:
Console.WriteLine(" Invalid input I'm afraid- please choose 1 - 5 ");
break;
}
}//Finish while loop
}//end else after the if statement up there
Console.ReadLine();
}
class MathsCalculator
{
//This class contains four methods/functions
//Add, Subtract, Multiply and Divide
//Add
public float Added(float x, float y)
{
return x + y;
}
public float Subtracted(float x, float y)
{
return x - y;
}
public float Multiplied(float x, float y)
{
return x * y;
}
public float Divided(float x, float y)
{
return x / y;
}
}
}
}