C# code executes even when variable not assigned





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















As you can probably tell from my question, I am very new to coding. I am trying to make a calculator that computes some formulas that are used in physics. However, the code runs the formula before the user has time to enter a value for A, in this example at least. Here is the example:



case "f = ma":
Console.WriteLine("Type the value for M in KG:");
var FM = Console.Read();
Console.WriteLine("Type the value for A in M/S:");
var FA = Console.Read();
var FMARes = FM * FA;
Console.WriteLine("Your answer (in Newtowns) is " + FMARes);
break;


How am I able to check whether a value has been assigned to the variable A, and only run the formula after the variable has an assigned value? Thanks.










share|improve this question





























    0















    As you can probably tell from my question, I am very new to coding. I am trying to make a calculator that computes some formulas that are used in physics. However, the code runs the formula before the user has time to enter a value for A, in this example at least. Here is the example:



    case "f = ma":
    Console.WriteLine("Type the value for M in KG:");
    var FM = Console.Read();
    Console.WriteLine("Type the value for A in M/S:");
    var FA = Console.Read();
    var FMARes = FM * FA;
    Console.WriteLine("Your answer (in Newtowns) is " + FMARes);
    break;


    How am I able to check whether a value has been assigned to the variable A, and only run the formula after the variable has an assigned value? Thanks.










    share|improve this question

























      0












      0








      0








      As you can probably tell from my question, I am very new to coding. I am trying to make a calculator that computes some formulas that are used in physics. However, the code runs the formula before the user has time to enter a value for A, in this example at least. Here is the example:



      case "f = ma":
      Console.WriteLine("Type the value for M in KG:");
      var FM = Console.Read();
      Console.WriteLine("Type the value for A in M/S:");
      var FA = Console.Read();
      var FMARes = FM * FA;
      Console.WriteLine("Your answer (in Newtowns) is " + FMARes);
      break;


      How am I able to check whether a value has been assigned to the variable A, and only run the formula after the variable has an assigned value? Thanks.










      share|improve this question














      As you can probably tell from my question, I am very new to coding. I am trying to make a calculator that computes some formulas that are used in physics. However, the code runs the formula before the user has time to enter a value for A, in this example at least. Here is the example:



      case "f = ma":
      Console.WriteLine("Type the value for M in KG:");
      var FM = Console.Read();
      Console.WriteLine("Type the value for A in M/S:");
      var FA = Console.Read();
      var FMARes = FM * FA;
      Console.WriteLine("Your answer (in Newtowns) is " + FMARes);
      break;


      How am I able to check whether a value has been assigned to the variable A, and only run the formula after the variable has an assigned value? Thanks.







      c# var






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 17 '18 at 0:54









      Xebra24Xebra24

      31




      31
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You need to use ReadLine instead of Read. You also need to do another ReadLine at the bottom so the user can see the result. And...you should validate that the user entered a valid number. This could be refactored a bit to avoid duplicate code - etc. - but see if this works for you! Good luck!!



              static void Main(string args)
          {
          double fm;
          double fa;

          // Use ReadLine instead of Read
          Console.WriteLine("Type the value for M in KG:");
          var input = Console.ReadLine();

          // Now you need to cast it to a double -
          // -- but only if the user entered a valid number
          if (!double.TryParse(input, out fm))
          {
          Console.WriteLine("Please enter a valid number for M");
          Console.ReadLine();
          return;
          }

          Console.WriteLine("Type the value for A in M/S:");
          input = Console.ReadLine();
          if (!double.TryParse(input, out fa))
          {
          Console.WriteLine("Please enter a valid number for A");
          Console.ReadLine();
          return;
          }

          // Now we have valid values for fa and fm
          // It's a better programming practice to use the string format
          // intead of + here...
          Console.WriteLine($"Your answer (in Newtowns) is {fm * fa}");

          // You need another read here or the program will just exit
          Console.WriteLine("Press Enter to end the program");
          Console.ReadLine();
          }





          share|improve this answer


























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347213%2fc-sharp-code-executes-even-when-variable-not-assigned%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            You need to use ReadLine instead of Read. You also need to do another ReadLine at the bottom so the user can see the result. And...you should validate that the user entered a valid number. This could be refactored a bit to avoid duplicate code - etc. - but see if this works for you! Good luck!!



                static void Main(string args)
            {
            double fm;
            double fa;

            // Use ReadLine instead of Read
            Console.WriteLine("Type the value for M in KG:");
            var input = Console.ReadLine();

            // Now you need to cast it to a double -
            // -- but only if the user entered a valid number
            if (!double.TryParse(input, out fm))
            {
            Console.WriteLine("Please enter a valid number for M");
            Console.ReadLine();
            return;
            }

            Console.WriteLine("Type the value for A in M/S:");
            input = Console.ReadLine();
            if (!double.TryParse(input, out fa))
            {
            Console.WriteLine("Please enter a valid number for A");
            Console.ReadLine();
            return;
            }

            // Now we have valid values for fa and fm
            // It's a better programming practice to use the string format
            // intead of + here...
            Console.WriteLine($"Your answer (in Newtowns) is {fm * fa}");

            // You need another read here or the program will just exit
            Console.WriteLine("Press Enter to end the program");
            Console.ReadLine();
            }





            share|improve this answer






























              1














              You need to use ReadLine instead of Read. You also need to do another ReadLine at the bottom so the user can see the result. And...you should validate that the user entered a valid number. This could be refactored a bit to avoid duplicate code - etc. - but see if this works for you! Good luck!!



                  static void Main(string args)
              {
              double fm;
              double fa;

              // Use ReadLine instead of Read
              Console.WriteLine("Type the value for M in KG:");
              var input = Console.ReadLine();

              // Now you need to cast it to a double -
              // -- but only if the user entered a valid number
              if (!double.TryParse(input, out fm))
              {
              Console.WriteLine("Please enter a valid number for M");
              Console.ReadLine();
              return;
              }

              Console.WriteLine("Type the value for A in M/S:");
              input = Console.ReadLine();
              if (!double.TryParse(input, out fa))
              {
              Console.WriteLine("Please enter a valid number for A");
              Console.ReadLine();
              return;
              }

              // Now we have valid values for fa and fm
              // It's a better programming practice to use the string format
              // intead of + here...
              Console.WriteLine($"Your answer (in Newtowns) is {fm * fa}");

              // You need another read here or the program will just exit
              Console.WriteLine("Press Enter to end the program");
              Console.ReadLine();
              }





              share|improve this answer




























                1












                1








                1







                You need to use ReadLine instead of Read. You also need to do another ReadLine at the bottom so the user can see the result. And...you should validate that the user entered a valid number. This could be refactored a bit to avoid duplicate code - etc. - but see if this works for you! Good luck!!



                    static void Main(string args)
                {
                double fm;
                double fa;

                // Use ReadLine instead of Read
                Console.WriteLine("Type the value for M in KG:");
                var input = Console.ReadLine();

                // Now you need to cast it to a double -
                // -- but only if the user entered a valid number
                if (!double.TryParse(input, out fm))
                {
                Console.WriteLine("Please enter a valid number for M");
                Console.ReadLine();
                return;
                }

                Console.WriteLine("Type the value for A in M/S:");
                input = Console.ReadLine();
                if (!double.TryParse(input, out fa))
                {
                Console.WriteLine("Please enter a valid number for A");
                Console.ReadLine();
                return;
                }

                // Now we have valid values for fa and fm
                // It's a better programming practice to use the string format
                // intead of + here...
                Console.WriteLine($"Your answer (in Newtowns) is {fm * fa}");

                // You need another read here or the program will just exit
                Console.WriteLine("Press Enter to end the program");
                Console.ReadLine();
                }





                share|improve this answer















                You need to use ReadLine instead of Read. You also need to do another ReadLine at the bottom so the user can see the result. And...you should validate that the user entered a valid number. This could be refactored a bit to avoid duplicate code - etc. - but see if this works for you! Good luck!!



                    static void Main(string args)
                {
                double fm;
                double fa;

                // Use ReadLine instead of Read
                Console.WriteLine("Type the value for M in KG:");
                var input = Console.ReadLine();

                // Now you need to cast it to a double -
                // -- but only if the user entered a valid number
                if (!double.TryParse(input, out fm))
                {
                Console.WriteLine("Please enter a valid number for M");
                Console.ReadLine();
                return;
                }

                Console.WriteLine("Type the value for A in M/S:");
                input = Console.ReadLine();
                if (!double.TryParse(input, out fa))
                {
                Console.WriteLine("Please enter a valid number for A");
                Console.ReadLine();
                return;
                }

                // Now we have valid values for fa and fm
                // It's a better programming practice to use the string format
                // intead of + here...
                Console.WriteLine($"Your answer (in Newtowns) is {fm * fa}");

                // You need another read here or the program will just exit
                Console.WriteLine("Press Enter to end the program");
                Console.ReadLine();
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 17 '18 at 1:29

























                answered Nov 17 '18 at 1:23









                Jon VoteJon Vote

                39710




                39710
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347213%2fc-sharp-code-executes-even-when-variable-not-assigned%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Bressuire

                    Vorschmack

                    Quarantine