Creating a method that I can call in the main method











up vote
0
down vote

favorite












Instruction: DoublePrompt() - Prints some prompt text to the console, reads in user response and returns that value as a parsed double. This method should protect against invalid numeric entries. When the user enters an invalid input, it should prompt them to enter a valid numeric input over and over again.



This is the code in my main method:



        while (choice.ToUpper() == "Y" )
{
double length = DoublePrompt`("nPlease enter package length: ");
double width = DoublePrompt("Please enter package width: ");
double height = DoublePrompt("Please enter package height: ");
double weight = DoublePrompt("Please enter package weight: ");

double cost = CalculatePackageCost(length, width, height, weight); totalCost += cost;
PrintCost("package", cost); choice = PackagePrompt();
}
while (choice.ToUpper() == "Y" )
{
double length = DoublePrompt`("nPlease enter package length:");
double width = DoublePrompt("Please enter package width: ");
double height = DoublePrompt("Please enter package height: ");
double weight = DoublePrompt("Please enter package weight: ");

double cost = CalculatePackageCost(length, width, height, weight); totalCost += cost;
PrintCost("package", cost); choice = PackagePrompt();
}


How do I create a method that does what the instruction states?
This is all that I have so far:



static double DoublePrompt(string Measurements )
{
string choice = double.TryParse(Console.ReadLine());


}


Any assistance will be appreciated










share|improve this question
























  • Your static Main() method is in some class. The easiest thing to do is to add your DoublePrompt() static method to the same class :) Make sure to have "DoublePrompt()" actually return a double ;)
    – paulsm4
    Nov 11 at 8:00












  • (A) Look up the documentation and read carefully about what double.TryParse does and how to use it. (B) Why do you try assigning the result (rerturn value) of double.TryParse to a string variable?
    – elgonzo
    Nov 11 at 8:02












  • break down your problem into parts: (A) Prints some prompt text to the console. (B) Read in use input (C) return that value(from (b)) as a parsed double (D) protect against invalid numeric entries- this is a bit ambiguous, protect how, decline the entry, throw an exception, what? all the other ((A), (B), and (C) ) can be found easily with google
    – styx
    Nov 11 at 8:11










  • Specify what should happen when an invalid numeric value is entered.
    – Francesc Castells
    Nov 11 at 8:12










  • Well, following your last edit, I think my answer should cover everything but writing the code for you. I suggest you give it a go.
    – Zohar Peled
    Nov 11 at 13:43















up vote
0
down vote

favorite












Instruction: DoublePrompt() - Prints some prompt text to the console, reads in user response and returns that value as a parsed double. This method should protect against invalid numeric entries. When the user enters an invalid input, it should prompt them to enter a valid numeric input over and over again.



This is the code in my main method:



        while (choice.ToUpper() == "Y" )
{
double length = DoublePrompt`("nPlease enter package length: ");
double width = DoublePrompt("Please enter package width: ");
double height = DoublePrompt("Please enter package height: ");
double weight = DoublePrompt("Please enter package weight: ");

double cost = CalculatePackageCost(length, width, height, weight); totalCost += cost;
PrintCost("package", cost); choice = PackagePrompt();
}
while (choice.ToUpper() == "Y" )
{
double length = DoublePrompt`("nPlease enter package length:");
double width = DoublePrompt("Please enter package width: ");
double height = DoublePrompt("Please enter package height: ");
double weight = DoublePrompt("Please enter package weight: ");

double cost = CalculatePackageCost(length, width, height, weight); totalCost += cost;
PrintCost("package", cost); choice = PackagePrompt();
}


How do I create a method that does what the instruction states?
This is all that I have so far:



static double DoublePrompt(string Measurements )
{
string choice = double.TryParse(Console.ReadLine());


}


Any assistance will be appreciated










share|improve this question
























  • Your static Main() method is in some class. The easiest thing to do is to add your DoublePrompt() static method to the same class :) Make sure to have "DoublePrompt()" actually return a double ;)
    – paulsm4
    Nov 11 at 8:00












  • (A) Look up the documentation and read carefully about what double.TryParse does and how to use it. (B) Why do you try assigning the result (rerturn value) of double.TryParse to a string variable?
    – elgonzo
    Nov 11 at 8:02












  • break down your problem into parts: (A) Prints some prompt text to the console. (B) Read in use input (C) return that value(from (b)) as a parsed double (D) protect against invalid numeric entries- this is a bit ambiguous, protect how, decline the entry, throw an exception, what? all the other ((A), (B), and (C) ) can be found easily with google
    – styx
    Nov 11 at 8:11










  • Specify what should happen when an invalid numeric value is entered.
    – Francesc Castells
    Nov 11 at 8:12










  • Well, following your last edit, I think my answer should cover everything but writing the code for you. I suggest you give it a go.
    – Zohar Peled
    Nov 11 at 13:43













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Instruction: DoublePrompt() - Prints some prompt text to the console, reads in user response and returns that value as a parsed double. This method should protect against invalid numeric entries. When the user enters an invalid input, it should prompt them to enter a valid numeric input over and over again.



This is the code in my main method:



        while (choice.ToUpper() == "Y" )
{
double length = DoublePrompt`("nPlease enter package length: ");
double width = DoublePrompt("Please enter package width: ");
double height = DoublePrompt("Please enter package height: ");
double weight = DoublePrompt("Please enter package weight: ");

double cost = CalculatePackageCost(length, width, height, weight); totalCost += cost;
PrintCost("package", cost); choice = PackagePrompt();
}
while (choice.ToUpper() == "Y" )
{
double length = DoublePrompt`("nPlease enter package length:");
double width = DoublePrompt("Please enter package width: ");
double height = DoublePrompt("Please enter package height: ");
double weight = DoublePrompt("Please enter package weight: ");

double cost = CalculatePackageCost(length, width, height, weight); totalCost += cost;
PrintCost("package", cost); choice = PackagePrompt();
}


How do I create a method that does what the instruction states?
This is all that I have so far:



static double DoublePrompt(string Measurements )
{
string choice = double.TryParse(Console.ReadLine());


}


Any assistance will be appreciated










share|improve this question















Instruction: DoublePrompt() - Prints some prompt text to the console, reads in user response and returns that value as a parsed double. This method should protect against invalid numeric entries. When the user enters an invalid input, it should prompt them to enter a valid numeric input over and over again.



This is the code in my main method:



        while (choice.ToUpper() == "Y" )
{
double length = DoublePrompt`("nPlease enter package length: ");
double width = DoublePrompt("Please enter package width: ");
double height = DoublePrompt("Please enter package height: ");
double weight = DoublePrompt("Please enter package weight: ");

double cost = CalculatePackageCost(length, width, height, weight); totalCost += cost;
PrintCost("package", cost); choice = PackagePrompt();
}
while (choice.ToUpper() == "Y" )
{
double length = DoublePrompt`("nPlease enter package length:");
double width = DoublePrompt("Please enter package width: ");
double height = DoublePrompt("Please enter package height: ");
double weight = DoublePrompt("Please enter package weight: ");

double cost = CalculatePackageCost(length, width, height, weight); totalCost += cost;
PrintCost("package", cost); choice = PackagePrompt();
}


How do I create a method that does what the instruction states?
This is all that I have so far:



static double DoublePrompt(string Measurements )
{
string choice = double.TryParse(Console.ReadLine());


}


Any assistance will be appreciated







c# methods






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 13:26

























asked Nov 11 at 7:57









Israek

13




13












  • Your static Main() method is in some class. The easiest thing to do is to add your DoublePrompt() static method to the same class :) Make sure to have "DoublePrompt()" actually return a double ;)
    – paulsm4
    Nov 11 at 8:00












  • (A) Look up the documentation and read carefully about what double.TryParse does and how to use it. (B) Why do you try assigning the result (rerturn value) of double.TryParse to a string variable?
    – elgonzo
    Nov 11 at 8:02












  • break down your problem into parts: (A) Prints some prompt text to the console. (B) Read in use input (C) return that value(from (b)) as a parsed double (D) protect against invalid numeric entries- this is a bit ambiguous, protect how, decline the entry, throw an exception, what? all the other ((A), (B), and (C) ) can be found easily with google
    – styx
    Nov 11 at 8:11










  • Specify what should happen when an invalid numeric value is entered.
    – Francesc Castells
    Nov 11 at 8:12










  • Well, following your last edit, I think my answer should cover everything but writing the code for you. I suggest you give it a go.
    – Zohar Peled
    Nov 11 at 13:43


















  • Your static Main() method is in some class. The easiest thing to do is to add your DoublePrompt() static method to the same class :) Make sure to have "DoublePrompt()" actually return a double ;)
    – paulsm4
    Nov 11 at 8:00












  • (A) Look up the documentation and read carefully about what double.TryParse does and how to use it. (B) Why do you try assigning the result (rerturn value) of double.TryParse to a string variable?
    – elgonzo
    Nov 11 at 8:02












  • break down your problem into parts: (A) Prints some prompt text to the console. (B) Read in use input (C) return that value(from (b)) as a parsed double (D) protect against invalid numeric entries- this is a bit ambiguous, protect how, decline the entry, throw an exception, what? all the other ((A), (B), and (C) ) can be found easily with google
    – styx
    Nov 11 at 8:11










  • Specify what should happen when an invalid numeric value is entered.
    – Francesc Castells
    Nov 11 at 8:12










  • Well, following your last edit, I think my answer should cover everything but writing the code for you. I suggest you give it a go.
    – Zohar Peled
    Nov 11 at 13:43
















Your static Main() method is in some class. The easiest thing to do is to add your DoublePrompt() static method to the same class :) Make sure to have "DoublePrompt()" actually return a double ;)
– paulsm4
Nov 11 at 8:00






Your static Main() method is in some class. The easiest thing to do is to add your DoublePrompt() static method to the same class :) Make sure to have "DoublePrompt()" actually return a double ;)
– paulsm4
Nov 11 at 8:00














(A) Look up the documentation and read carefully about what double.TryParse does and how to use it. (B) Why do you try assigning the result (rerturn value) of double.TryParse to a string variable?
– elgonzo
Nov 11 at 8:02






(A) Look up the documentation and read carefully about what double.TryParse does and how to use it. (B) Why do you try assigning the result (rerturn value) of double.TryParse to a string variable?
– elgonzo
Nov 11 at 8:02














break down your problem into parts: (A) Prints some prompt text to the console. (B) Read in use input (C) return that value(from (b)) as a parsed double (D) protect against invalid numeric entries- this is a bit ambiguous, protect how, decline the entry, throw an exception, what? all the other ((A), (B), and (C) ) can be found easily with google
– styx
Nov 11 at 8:11




break down your problem into parts: (A) Prints some prompt text to the console. (B) Read in use input (C) return that value(from (b)) as a parsed double (D) protect against invalid numeric entries- this is a bit ambiguous, protect how, decline the entry, throw an exception, what? all the other ((A), (B), and (C) ) can be found easily with google
– styx
Nov 11 at 8:11












Specify what should happen when an invalid numeric value is entered.
– Francesc Castells
Nov 11 at 8:12




Specify what should happen when an invalid numeric value is entered.
– Francesc Castells
Nov 11 at 8:12












Well, following your last edit, I think my answer should cover everything but writing the code for you. I suggest you give it a go.
– Zohar Peled
Nov 11 at 13:43




Well, following your last edit, I think my answer should cover everything but writing the code for you. I suggest you give it a go.
– Zohar Peled
Nov 11 at 13:43












1 Answer
1






active

oldest

votes

















up vote
1
down vote













Ok, let's review the instructions:




DoublePrompt() - Prints some prompt text to the console, reads in user response and returns that value as a parsed double. This method should protect against invalid numeric entries.




So The first thing this method should do is Console.WriteLine - to print the prompt text to the console. Then, it should read the user's response (using Console.ReadLine()), try to parse it as double (using the double.TryParse() method), and return the double back to the caller.



What should happen if the user enters some random string that can't be parsed to double?

The instruction doesn't say, but let's think about it for a second: from the application point of view, there's no point of moving on since we are missing a value the user should have entered. from the user's point of view, they might have tried typing something like 6.35 and accidentally pressed t with the 5 and got 6.35t - which will fail the double.TryParse - so as a user, you would like to be notified your input is invalid and get a chance to reenter it.



So your method should do something like this:




  1. Take in a string parameter.

  2. Inside a loop, write the parameter to the console, and get the user response.

  3. try to parse the user response to a double, if success, return the double. if fail, prompt a message to the customer saying the input can't be parsed to double, and start the loop again.


Since this is clearly a homework assignment I'll let you do the coding yourself, because otherwise you will learn nothing from my answer.
I will, however, leave you with one last hint - you can use a do {}while(condition) loop or use while(true) and return inside.






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',
    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%2f53246836%2fcreating-a-method-that-i-can-call-in-the-main-method%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








    up vote
    1
    down vote













    Ok, let's review the instructions:




    DoublePrompt() - Prints some prompt text to the console, reads in user response and returns that value as a parsed double. This method should protect against invalid numeric entries.




    So The first thing this method should do is Console.WriteLine - to print the prompt text to the console. Then, it should read the user's response (using Console.ReadLine()), try to parse it as double (using the double.TryParse() method), and return the double back to the caller.



    What should happen if the user enters some random string that can't be parsed to double?

    The instruction doesn't say, but let's think about it for a second: from the application point of view, there's no point of moving on since we are missing a value the user should have entered. from the user's point of view, they might have tried typing something like 6.35 and accidentally pressed t with the 5 and got 6.35t - which will fail the double.TryParse - so as a user, you would like to be notified your input is invalid and get a chance to reenter it.



    So your method should do something like this:




    1. Take in a string parameter.

    2. Inside a loop, write the parameter to the console, and get the user response.

    3. try to parse the user response to a double, if success, return the double. if fail, prompt a message to the customer saying the input can't be parsed to double, and start the loop again.


    Since this is clearly a homework assignment I'll let you do the coding yourself, because otherwise you will learn nothing from my answer.
    I will, however, leave you with one last hint - you can use a do {}while(condition) loop or use while(true) and return inside.






    share|improve this answer

























      up vote
      1
      down vote













      Ok, let's review the instructions:




      DoublePrompt() - Prints some prompt text to the console, reads in user response and returns that value as a parsed double. This method should protect against invalid numeric entries.




      So The first thing this method should do is Console.WriteLine - to print the prompt text to the console. Then, it should read the user's response (using Console.ReadLine()), try to parse it as double (using the double.TryParse() method), and return the double back to the caller.



      What should happen if the user enters some random string that can't be parsed to double?

      The instruction doesn't say, but let's think about it for a second: from the application point of view, there's no point of moving on since we are missing a value the user should have entered. from the user's point of view, they might have tried typing something like 6.35 and accidentally pressed t with the 5 and got 6.35t - which will fail the double.TryParse - so as a user, you would like to be notified your input is invalid and get a chance to reenter it.



      So your method should do something like this:




      1. Take in a string parameter.

      2. Inside a loop, write the parameter to the console, and get the user response.

      3. try to parse the user response to a double, if success, return the double. if fail, prompt a message to the customer saying the input can't be parsed to double, and start the loop again.


      Since this is clearly a homework assignment I'll let you do the coding yourself, because otherwise you will learn nothing from my answer.
      I will, however, leave you with one last hint - you can use a do {}while(condition) loop or use while(true) and return inside.






      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        Ok, let's review the instructions:




        DoublePrompt() - Prints some prompt text to the console, reads in user response and returns that value as a parsed double. This method should protect against invalid numeric entries.




        So The first thing this method should do is Console.WriteLine - to print the prompt text to the console. Then, it should read the user's response (using Console.ReadLine()), try to parse it as double (using the double.TryParse() method), and return the double back to the caller.



        What should happen if the user enters some random string that can't be parsed to double?

        The instruction doesn't say, but let's think about it for a second: from the application point of view, there's no point of moving on since we are missing a value the user should have entered. from the user's point of view, they might have tried typing something like 6.35 and accidentally pressed t with the 5 and got 6.35t - which will fail the double.TryParse - so as a user, you would like to be notified your input is invalid and get a chance to reenter it.



        So your method should do something like this:




        1. Take in a string parameter.

        2. Inside a loop, write the parameter to the console, and get the user response.

        3. try to parse the user response to a double, if success, return the double. if fail, prompt a message to the customer saying the input can't be parsed to double, and start the loop again.


        Since this is clearly a homework assignment I'll let you do the coding yourself, because otherwise you will learn nothing from my answer.
        I will, however, leave you with one last hint - you can use a do {}while(condition) loop or use while(true) and return inside.






        share|improve this answer












        Ok, let's review the instructions:




        DoublePrompt() - Prints some prompt text to the console, reads in user response and returns that value as a parsed double. This method should protect against invalid numeric entries.




        So The first thing this method should do is Console.WriteLine - to print the prompt text to the console. Then, it should read the user's response (using Console.ReadLine()), try to parse it as double (using the double.TryParse() method), and return the double back to the caller.



        What should happen if the user enters some random string that can't be parsed to double?

        The instruction doesn't say, but let's think about it for a second: from the application point of view, there's no point of moving on since we are missing a value the user should have entered. from the user's point of view, they might have tried typing something like 6.35 and accidentally pressed t with the 5 and got 6.35t - which will fail the double.TryParse - so as a user, you would like to be notified your input is invalid and get a chance to reenter it.



        So your method should do something like this:




        1. Take in a string parameter.

        2. Inside a loop, write the parameter to the console, and get the user response.

        3. try to parse the user response to a double, if success, return the double. if fail, prompt a message to the customer saying the input can't be parsed to double, and start the loop again.


        Since this is clearly a homework assignment I'll let you do the coding yourself, because otherwise you will learn nothing from my answer.
        I will, however, leave you with one last hint - you can use a do {}while(condition) loop or use while(true) and return inside.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 8:12









        Zohar Peled

        51.8k73172




        51.8k73172






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53246836%2fcreating-a-method-that-i-can-call-in-the-main-method%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

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma