Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

Input string was not in a correct format

$
0
0

hi   i am trying to develop personal algorithem for calculator and i a made it to do functions like + and - and now i am trying to add another operator *  and i got some issues with it please can someone tell me what is the problem so i can fix it thanks for helpers.

EXAMPLE

10+5*2=

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Math_algorithem_1
{
    class Program
    {
        static void Main(string[] args)
        {


            string targil = Console.ReadLine();



            double result = 0;
            double tval = 0; //temp value for multiply or devision
            string value = "";
            char sign = ' ';
            char tsign = ' ';
            double acc_val = 0;

            for (int i = 0; i < targil.Length && sign != '='; i++)
            {

                if (targil[i] != '+' && targil[i] != '-' && targil[i] != '=' && targil[i] != '*') //this is a number
                {
                    value = value + targil[i];

                }
                else  //this is a sign +/-
                {

                    result = Convert.ToDouble(value);

                    //Console.WriteLine("Converted '{0}' to {1}.", value, result);
                    if (sign == ' ') //first time sign detected
                    {
                        acc_val = result;
                    }

                    else if (targil[i] == '*')//at least second time sign detected
                    {
                        tval = result;
                        tsign = sign;
                    }
                    else // sign is + or -
                    {
                        switch (sign)
                        {
                            case '+':

                                acc_val = acc_val + result;
                                break;
                            case '-':
                                acc_val = acc_val - result;
                                break;
                            case '*':
                                tval = tval * result;
                                switch (tsign)
                                {
                                    case '+':
                                        acc_val = acc_val + tval;
                                        break;
                                    case '-':
                                        acc_val = acc_val - tval;
                                        break;
                                    case '*':
                                        tval = tval * result;
                                        break;
                                }
                                tval = 0;
                                tsign = ' ';
                                break;
                        } //end switch
                    } //end else sign is + or -
                    //Console.WriteLine("Result " + acc_val);
                } //end else this is a sign +/-

                    value = "";
                    sign = targil[i];




            }//end of loop

            Console.Clear();
            Console.WriteLine(targil + " " + acc_val);

            //Console.WriteLine();

        } //end of void main
    } //end of class program

}//end of Math_algorithem_1



Viewing all articles
Browse latest Browse all 8156

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>