C # doubling value alone

-1

I'm getting C #, but the code below doubles the value and quits unexpectedly. Can someone take a look?

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

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            float a, b = 0;
            float resul = 0;
            string dado;


            Console.WriteLine("digite o primeiro valor");
            dado = Console.ReadLine();
            a = float.Parse(dado);
            Console.WriteLine("Digite o segundo valor");
            b = float.Parse(dado);
            resul = a + b;

            if (resul > 10)
            {
                Console.WriteLine(resul);

            }
            else 
            {
                Console.WriteLine("O valor" + resul,"é muito abaixo do esperado");

            }

            Console.WriteLine("encerre esse programa digitando qualquer tecla");
                Console.ReadLine();



        }
    }
}
    
asked by anonymous 29.10.2017 / 02:41

2 answers

0

As mentioned in the comment, you have to add the following line before assigning the value to the variable b

dado = Console.ReadLine();
b = float.Parse(dado);
...
    
29.10.2017 / 02:14
0

First of all, variables must be initialized. However, the code can be simplified, both for better understanding, and for reading the code itself. Look below the code with the change and compare yours.

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

 namespace ConsoleApp5
 {
   class Program
   {
    static void Main(string[] args)
    { 
        decimal a = 0; 
        decimal b = 0;
        decimal resul = 0;  

        Console.WriteLine("digite o primeiro valor");
        a = decimal.Parse(Console.ReadLine());
        Console.WriteLine("Digite o segundo valor");
        b = decimal.Parse(Console.ReadLine());

        resul = a + b;

        if (resul > 10)
        {
            Console.WriteLine(resul);
        }
        else 
        {
            Console.WriteLine("O valor " + resul + " é muito abaixo do esperado");
        }

        Console.WriteLine("encerre esse programa digitando qualquer tecla...");
        Console.ReadLine();
    }
   }
 }

Now, it will work perfectly.

    
29.10.2017 / 02:21