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
c# methods
add a comment |
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
c# methods
Your staticMain()
method is in some class. The easiest thing to do is to add yourDoublePrompt()
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 whatdouble.TryParse
does and how to use it. (B) Why do you try assigning the result (rerturn value) ofdouble.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
add a comment |
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
c# methods
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
c# methods
edited Nov 11 at 13:26
asked Nov 11 at 7:57
Israek
13
13
Your staticMain()
method is in some class. The easiest thing to do is to add yourDoublePrompt()
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 whatdouble.TryParse
does and how to use it. (B) Why do you try assigning the result (rerturn value) ofdouble.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
add a comment |
Your staticMain()
method is in some class. The easiest thing to do is to add yourDoublePrompt()
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 whatdouble.TryParse
does and how to use it. (B) Why do you try assigning the result (rerturn value) ofdouble.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
add a comment |
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:
- Take in a string parameter.
- Inside a loop, write the parameter to the console, and get the user response.
- 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.
add a comment |
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:
- Take in a string parameter.
- Inside a loop, write the parameter to the console, and get the user response.
- 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.
add a comment |
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:
- Take in a string parameter.
- Inside a loop, write the parameter to the console, and get the user response.
- 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.
add a comment |
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:
- Take in a string parameter.
- Inside a loop, write the parameter to the console, and get the user response.
- 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.
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:
- Take in a string parameter.
- Inside a loop, write the parameter to the console, and get the user response.
- 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.
answered Nov 11 at 8:12
Zohar Peled
51.8k73172
51.8k73172
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Your static
Main()
method is in some class. The easiest thing to do is to add yourDoublePrompt()
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) ofdouble.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