Home > Back-end >  C# false input returned after second try
C# false input returned after second try

Time:12-26

Hello I'm new to C# and started learning it yesterday with some Java and Python knowledge. I tried to make a calculator but it isn't working as expected. The code is:

using System;

namespace LolCS{
    class Program{
        
        static void Main(){
            //variables and objects
            Program proj = new Program();
            double solution = 0;
            string operation;
            double input1 = 0;
            double input2 = 0;

            //take inputs for input 1,2 and which operation to use
            Console.WriteLine("Welcome to this calculator, let's get started\n" 
                              "Please input your operations\n");               

            double.TryParse(proj.get_input("in1"), out input1);
            operation = proj.get_input("op");
            double.TryParse(proj.get_input("in2"), out input2);

            //call function which does the math and assign result to variable solution
            solution = proj.domath(operation, input1, input2);

            //print solution
            Console.WriteLine(input1 operation input2 " = " solution);
            if (Console.ReadLine() == "r") { Main(); } 
        }

        //function to do the math according to the given operation
        double domath(string oper, double in1, double in2){
            double solu = 0;
            switch(oper){                                    
                case " ":
                    solu = in1   in2;
                    break;
                case "-":
                    solu = in1-in2;
                    break;
                case "*":
                    solu = in1 * in2;
                    break;
                case "/":
                    solu = in1 / in2;
                    break;
            }
            return solu;
        }

        //gets values for the variables
        string get_input(string in_type){
            string gon_ret = "";
            switch(in_type){
                case "in1":
                    Console.WriteLine("Input 1: ");
                    gon_ret = Console.ReadLine();
                    break;
                case "in2":
                    Console.WriteLine("Input 2: ");
                    gon_ret = Console.ReadLine();
                    break;
                case "op":
                    Console.WriteLine("Operation: ");
                    gon_ret = Console.ReadLine();       
                    if (!(gon_ret == " ") && !(gon_ret == "-") && !(gon_ret == "*") && !(gon_ret == "/")){
                        Console.WriteLine(gon_ret " Not Valid, try again");
                        get_input("op");
                    }
                    break;  
            }
            return gon_ret;

        }
    }
}

The problem is with this line of code:

case "op":
                    Console.WriteLine("Operation: ");
                    gon_ret = Console.ReadLine();       
                    if (!(gon_ret == " ") && !(gon_ret == "-") && !(gon_ret == "*") && !(gon_ret == "/")){
                        Console.WriteLine(gon_ret " Not Valid, try again");
                        get_input("op");
                    }
                    break;  

It is expected to check if the input for operation is different than ,-,* or / and if it is it should print "Not Valid" and repeat the input for the operation. That works as expected if the input is correct the first time but if the correct input is given on the second try or later, the false input is returned. Example output:

Welcome to this calculator, let's get started
Please input your operations

Input 1: 
2
Operation: 
p
p Not Valid, try again
Operation: 
 
Input 2: 
3
2p3 = 0

Sorry if it is a stupid mistake but I'm still learning. Thanks for the help!

CodePudding user response:

You are doing a recursive operation with get_input("op") The function works fine but you are not returning the results of the new get_input("op") call. You are returning the invalid string produce from the original call.

            case "op":
                Console.WriteLine("Operation: ");
                gon_ret = Console.ReadLine();       
                if (!(gon_ret == " ") && !(gon_ret == "-") && !(gon_ret == "*") && !(gon_ret == "/")){
                    Console.WriteLine(gon_ret " Not Valid, try again");
                    //Return here as the current call is invalid
                    return get_input("op");
                }
                break;
  • Related