C# i'm trying to make addition work,but constantly getting error System.FormatException HResult=0x80131537

-2

So the problem is on line broj1 = double.Parse(broj1string); I can run the code, it works perfectly fine until I try to add number 1 (line broj1 = double.Parse(broj1string);)

I want to convert string input to double.

I also tried using TryParse instead of Parse, and code still breaks at broj1 = double.TryParse(broj1string,get...)

Fixed it by changing below code with:


while (true)
{
    string selectedMenuItem = meni(meniodabira);
    if (selectedMenuItem == "Sabiranje")
    {
        Console.Clear();
        Console.WriteLine("Molim vas Unesite prvi broj?");
        broj1string = Console.ReadLine();

        bool v = double.TryParse(broj1string, out double broj1);
        Console.WriteLine("Molim vas unesite drugi broj?");
        broj2string = Console.ReadLine();
        bool g = double.TryParse(broj2string, out double broj2);
        rezultat = broj1 + broj2;
        Console.WriteLine($"Zbir prvog i drugog broja iznosi {rezultat}");
    }
    else if (selectedMenuItem == "Izlazak") {
        Environment.Exit(0);
    }
}

This is whole code I've written, hope you guys can help me :)

class Program
    {
        private static int index = 0, broj;
        private static double broj1, broj2, rezultat;
        private static string broj1string, broj2string;
        private static void Main(string[] args)
        {
            //Sadrzaj menija
            start:
            List<string> meniodabira = new List<string>() {
                "Sabiranje",
                "Oduzimanje",
                "Mnozenje",
                "Deljenje",
                "Izlazak"
            };

            Console.CursorVisible = false;

            while (true)
            {
                string selectedMenuItem = meni(meniodabira);
                if (selectedMenuItem == "Sabiranje")
                {
                    Console.Clear();
                    Console.WriteLine("Molim vas Unesite prvi broj?");
                    broj1string = Console.ReadLine();
                    broj1 = double.Parse(broj1string);
                    Console.WriteLine("Molim vas unesite drugi broj?");
                    broj2string = Console.ReadLine();
                    broj2 = double.Parse(broj1string);
                    rezultat = broj1 + broj2;
                    Console.WriteLine($"Zbir prvog i drugog broja iznosi {rezultat}");
                    goto start;
                }
                else if (selectedMenuItem == "Izlazak")
                {
                    Environment.Exit(0);
                }
            }
        }


        // Konzolni meni
        private static string meni(List<string> sadrzaj)
        {
            for (int i = 0; i < sadrzaj.Count; i++)
            {
                if (i == index)
                {
                    Console.BackgroundColor = ConsoleColor.Black;
                    Console.ForegroundColor = ConsoleColor.Green;

                    Console.WriteLine(sadrzaj[i]);
                }
                else
                {
                    Console.WriteLine(sadrzaj[i]);
                }
                Console.ResetColor();
            }

            ConsoleKeyInfo ckey = Console.ReadKey();

            if (ckey.Key == ConsoleKey.DownArrow)
            {
                if (index == sadrzaj.Count - 1)
                {
                    index = 0;
                }
                else { index++; }
            }
            else if (ckey.Key == ConsoleKey.UpArrow)
            {
                if (index <= 0)
                {
                    index = sadrzaj.Count - 1;
                }
                else { index--; }
            }
            else if (ckey.Key == ConsoleKey.Enter)
            {
                return sadrzaj[index];
            }
            else
            {
                return "";
            }

            Console.Clear();
            return "";
        }
    }

Also yea I'm new to programming overall and appriciate all of your answers and helps 


c#
asked on Stack Overflow Jan 18, 2021 by Szwitch • edited Jan 18, 2021 by YakovL

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0