tkennedy741
tkennedy741

Reputation: 9

I can't figure out why my Console.ReadLine() is not working

I'm relatively new to programming and I'm working on various projects to get better at it. One is a calculator that I want to be able to solve more than just basic addition and subtraction. Currently, all I have is the basics with the quadratic formula. But my design weighs on user input to decide what to do. It prompts the user with "What would you like to do? Add: Sub: Div: Mul: Quad Equation:" After typic in "Add" the code operates normally but if I type in anything else like "Sub" or "Div". it does nothing. doesn't even spit back an error. As far as I can tell, my code is fine (well it's terrible code but it should work nonetheless) But I just do not know how to proceed.

using System;

namespace Better_Calculator
{
    class Program
    {
        static void Main(string[] args)
        {
            double num01;
            double num02;

            double a;
            double b;
            double c;

            System.Console.WriteLine("Welcome to how you are going to cheat through math lol");
            System.Console.WriteLine("What would you like to do? \nAdd: \nSub: \nDiv: \nMul: \nQuad Equation: ");

            if (Console.ReadLine() == "Add")
            {   
                System.Console.WriteLine("what is the first number?");
                num01 = Convert.ToInt32(Console.ReadLine());
                System.Console.WriteLine("what is the second number?");
                num02 = Convert.ToInt32(Console.ReadLine());

                Add(num01, num02);
            }
            else if (System.Console.ReadLine() == "Sub")
            {
                System.Console.WriteLine("what is the first number?");
                num01 = Convert.ToInt32(Console.ReadLine());
                System.Console.WriteLine("what is the second number?");
                num02 = Convert.ToInt32(Console.ReadLine());

                Sub(num01, num02);                
            }
            else if (System.Console.ReadLine() == "Mul")
            {
                System.Console.WriteLine("what is the first number?");
                num01 = Convert.ToInt32(Console.ReadLine());
                System.Console.WriteLine("what is the second number?");
                num02 = Convert.ToInt32(Console.ReadLine());


                Mul(num01, num02);
            }
             else if (System.Console.ReadLine() == "Div")
            {
                System.Console.WriteLine("what is the first number?");
                num01 = Convert.ToInt32(Console.ReadLine());
                System.Console.WriteLine("what is the second number?");
                num02 = Convert.ToInt32(Console.ReadLine());


                Div(num01, num02);
            }
            else if (System.Console.ReadLine() == "Quad Equation")
            {
                System.Console.WriteLine("what is a?");
                a = Convert.ToInt32(Console.ReadLine());               
                System.Console.WriteLine("what is b");
                b = Convert.ToInt32(Console.ReadLine());
                System.Console.WriteLine("what is c");
                c = Convert.ToInt32(Console.ReadLine());

                Quad(a, b, c);
            }

            static void Add(double num01, double num02)
            {
                string answer = Convert.ToString(num01 + num02);
                System.Console.WriteLine(answer);
            }
            static void Sub(double num01, double num02)
            {
                string answer = Convert.ToString(num01 - num02);
                System.Console.WriteLine(answer);
            }
            static void Mul(double num01, double num02)
            {
                
                string answer = Convert.ToString(num01 * num02);
                System.Console.WriteLine(answer);
            }
            static void Div(double num01, double num02)
            {
                string answer = Convert.ToString(num01 / num02);
                System.Console.WriteLine(answer);
            }
            static void Quad(double a, double b, double c)
            {
                
                double bNeg = b * -1;
                double bSqr = b * b;
                double SqR = Math.Sqrt(bSqr - (4 * a * c));

                double solvedAdd = (bNeg + SqR) / (2 * a);
                double solvedSub = (bNeg - SqR) / (2 * a);

                string answer = Convert.ToString(solvedAdd) + " or " + Convert.ToString(solvedSub);

                System.Console.WriteLine(answer);

            }       
        }
    }
}

Upvotes: 1

Views: 956

Answers (3)

RAJKUMAR VELPANDI
RAJKUMAR VELPANDI

Reputation: 1

When we get input from console application from user may be they given as

CAPS, Small or combination of both

. In that situation you can use the following approach,

string value=Console.ReadLine();

if(string.Equals(value, "Add", StringComparison.CurrentCultureIgnoreCase))
{
    // Do add stuff
}
else if(string.Equals(value, "Sub", StringComparison.CurrentCultureIgnoreCase))
{
    // Do add stuff
}

Upvotes: 0

Heinz Kessler
Heinz Kessler

Reputation: 1670

With every ´if´, you call Console.ReadLine() again, so if you enter "Quad Equation", your code has passed 5 Readlines until you reach the code that does Quad Equation.

Solution: Do only one Console.ReadLine() and put its result into a variable:

var userInput = Console.ReadLine();

and then, test against userInput in your if statements (ex.)e:

else if (userInput == "Sub")

Upvotes: 6

DavidG
DavidG

Reputation: 119136

You only need to do a ReadLine once and store the value, then use that to compare. For example:

var operation = Console.ReadLine();

if(operation == "Add")
{
    // Do add stuff
}

But you should consider using a switch statement instead:

switch(operation)
{
    case "Add":
        // do Add Stuff
        break;
    case "Sub":
        // do Add Stuff
        break;
    case "Mul":
        // do Add Stuff
        break;

    // etc...
}

Upvotes: 3

Related Questions