Using a function with a string parameter for input validation
up vote
-1
down vote
favorite
I am in an intro to programming class for C++. It's not that hard for me, but this assignment is just confusing me.
The assignment is using two void functions, one will compute the area and perimeter of given length and width for a rectangle. The other is to compute the volume and surface area of the given length, width and height for a rectangular prism.
I have completed all of this so far and it works well, but there is a third int function that we are to use for input validation.
He wants us to make the function named "int promptForPostitiveNumbers(string ____)" Where we fill the blank with whatever name we want for the variable.
How exactly do I go about this if when calling the function its expecting a string and not a number for the lengths widths and heights?
Also this is my first time going to a forum for help with an assignment, so I am not sure what all I should include.
Edit: My professor requires every assignment to be done with the "using namespace std;" and I am only on my 13th week of classes so I'm still pretty basic when it comes to knowledge of C++
c++
add a comment |
up vote
-1
down vote
favorite
I am in an intro to programming class for C++. It's not that hard for me, but this assignment is just confusing me.
The assignment is using two void functions, one will compute the area and perimeter of given length and width for a rectangle. The other is to compute the volume and surface area of the given length, width and height for a rectangular prism.
I have completed all of this so far and it works well, but there is a third int function that we are to use for input validation.
He wants us to make the function named "int promptForPostitiveNumbers(string ____)" Where we fill the blank with whatever name we want for the variable.
How exactly do I go about this if when calling the function its expecting a string and not a number for the lengths widths and heights?
Also this is my first time going to a forum for help with an assignment, so I am not sure what all I should include.
Edit: My professor requires every assignment to be done with the "using namespace std;" and I am only on my 13th week of classes so I'm still pretty basic when it comes to knowledge of C++
c++
The easy way is to usestd::stoi
to turn thestring
into a number (or die trying-remember to handle the exception) and then process the number normally. You can also short-circuit all of the conversion if thestring
starts with a'-'
character.
– user4581301
Nov 11 at 4:21
Side note: Stack Overflow isn't a forum. It's a Q&A. You ask a question, people offer answers. Don't use answers to communicate information with people unless it really is an answer. Amend the question with new information. You pick the most helpful of the answers that resolve your question as the "Official" answer. Don't accept right away. Test the answers first, and who knows? Someone might as a genius answer after a few less brilliant answers are posted.
– user4581301
Nov 11 at 4:26
1
Meant to mention this in the post I will edit it shortly. My professor requires every assignment to be done using namespace std. so I am not sure what std::stoi is.
– Cookie PePe
Nov 11 at 4:32
std::stoi
breaks down into thestoi
function in thestd
namespace. That means you should be good. The only caveat isstoi
isn't in older (pre 2011) versions of the C++ standard. If you're learning old C++, you'll have to usestd::strtol
or (sneaky sneaky)std::stroul
. The u instrtoul
stands for unsigned. It will reject a negative number.
– user4581301
Nov 11 at 4:50
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I am in an intro to programming class for C++. It's not that hard for me, but this assignment is just confusing me.
The assignment is using two void functions, one will compute the area and perimeter of given length and width for a rectangle. The other is to compute the volume and surface area of the given length, width and height for a rectangular prism.
I have completed all of this so far and it works well, but there is a third int function that we are to use for input validation.
He wants us to make the function named "int promptForPostitiveNumbers(string ____)" Where we fill the blank with whatever name we want for the variable.
How exactly do I go about this if when calling the function its expecting a string and not a number for the lengths widths and heights?
Also this is my first time going to a forum for help with an assignment, so I am not sure what all I should include.
Edit: My professor requires every assignment to be done with the "using namespace std;" and I am only on my 13th week of classes so I'm still pretty basic when it comes to knowledge of C++
c++
I am in an intro to programming class for C++. It's not that hard for me, but this assignment is just confusing me.
The assignment is using two void functions, one will compute the area and perimeter of given length and width for a rectangle. The other is to compute the volume and surface area of the given length, width and height for a rectangular prism.
I have completed all of this so far and it works well, but there is a third int function that we are to use for input validation.
He wants us to make the function named "int promptForPostitiveNumbers(string ____)" Where we fill the blank with whatever name we want for the variable.
How exactly do I go about this if when calling the function its expecting a string and not a number for the lengths widths and heights?
Also this is my first time going to a forum for help with an assignment, so I am not sure what all I should include.
Edit: My professor requires every assignment to be done with the "using namespace std;" and I am only on my 13th week of classes so I'm still pretty basic when it comes to knowledge of C++
c++
c++
edited Nov 11 at 4:33
asked Nov 11 at 4:15
Cookie PePe
11
11
The easy way is to usestd::stoi
to turn thestring
into a number (or die trying-remember to handle the exception) and then process the number normally. You can also short-circuit all of the conversion if thestring
starts with a'-'
character.
– user4581301
Nov 11 at 4:21
Side note: Stack Overflow isn't a forum. It's a Q&A. You ask a question, people offer answers. Don't use answers to communicate information with people unless it really is an answer. Amend the question with new information. You pick the most helpful of the answers that resolve your question as the "Official" answer. Don't accept right away. Test the answers first, and who knows? Someone might as a genius answer after a few less brilliant answers are posted.
– user4581301
Nov 11 at 4:26
1
Meant to mention this in the post I will edit it shortly. My professor requires every assignment to be done using namespace std. so I am not sure what std::stoi is.
– Cookie PePe
Nov 11 at 4:32
std::stoi
breaks down into thestoi
function in thestd
namespace. That means you should be good. The only caveat isstoi
isn't in older (pre 2011) versions of the C++ standard. If you're learning old C++, you'll have to usestd::strtol
or (sneaky sneaky)std::stroul
. The u instrtoul
stands for unsigned. It will reject a negative number.
– user4581301
Nov 11 at 4:50
add a comment |
The easy way is to usestd::stoi
to turn thestring
into a number (or die trying-remember to handle the exception) and then process the number normally. You can also short-circuit all of the conversion if thestring
starts with a'-'
character.
– user4581301
Nov 11 at 4:21
Side note: Stack Overflow isn't a forum. It's a Q&A. You ask a question, people offer answers. Don't use answers to communicate information with people unless it really is an answer. Amend the question with new information. You pick the most helpful of the answers that resolve your question as the "Official" answer. Don't accept right away. Test the answers first, and who knows? Someone might as a genius answer after a few less brilliant answers are posted.
– user4581301
Nov 11 at 4:26
1
Meant to mention this in the post I will edit it shortly. My professor requires every assignment to be done using namespace std. so I am not sure what std::stoi is.
– Cookie PePe
Nov 11 at 4:32
std::stoi
breaks down into thestoi
function in thestd
namespace. That means you should be good. The only caveat isstoi
isn't in older (pre 2011) versions of the C++ standard. If you're learning old C++, you'll have to usestd::strtol
or (sneaky sneaky)std::stroul
. The u instrtoul
stands for unsigned. It will reject a negative number.
– user4581301
Nov 11 at 4:50
The easy way is to use
std::stoi
to turn the string
into a number (or die trying-remember to handle the exception) and then process the number normally. You can also short-circuit all of the conversion if the string
starts with a '-'
character.– user4581301
Nov 11 at 4:21
The easy way is to use
std::stoi
to turn the string
into a number (or die trying-remember to handle the exception) and then process the number normally. You can also short-circuit all of the conversion if the string
starts with a '-'
character.– user4581301
Nov 11 at 4:21
Side note: Stack Overflow isn't a forum. It's a Q&A. You ask a question, people offer answers. Don't use answers to communicate information with people unless it really is an answer. Amend the question with new information. You pick the most helpful of the answers that resolve your question as the "Official" answer. Don't accept right away. Test the answers first, and who knows? Someone might as a genius answer after a few less brilliant answers are posted.
– user4581301
Nov 11 at 4:26
Side note: Stack Overflow isn't a forum. It's a Q&A. You ask a question, people offer answers. Don't use answers to communicate information with people unless it really is an answer. Amend the question with new information. You pick the most helpful of the answers that resolve your question as the "Official" answer. Don't accept right away. Test the answers first, and who knows? Someone might as a genius answer after a few less brilliant answers are posted.
– user4581301
Nov 11 at 4:26
1
1
Meant to mention this in the post I will edit it shortly. My professor requires every assignment to be done using namespace std. so I am not sure what std::stoi is.
– Cookie PePe
Nov 11 at 4:32
Meant to mention this in the post I will edit it shortly. My professor requires every assignment to be done using namespace std. so I am not sure what std::stoi is.
– Cookie PePe
Nov 11 at 4:32
std::stoi
breaks down into the stoi
function in the std
namespace. That means you should be good. The only caveat is stoi
isn't in older (pre 2011) versions of the C++ standard. If you're learning old C++, you'll have to use std::strtol
or (sneaky sneaky) std::stroul
. The u in strtoul
stands for unsigned. It will reject a negative number.– user4581301
Nov 11 at 4:50
std::stoi
breaks down into the stoi
function in the std
namespace. That means you should be good. The only caveat is stoi
isn't in older (pre 2011) versions of the C++ standard. If you're learning old C++, you'll have to use std::strtol
or (sneaky sneaky) std::stroul
. The u in strtoul
stands for unsigned. It will reject a negative number.– user4581301
Nov 11 at 4:50
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Using namespace std is just for convenience and sometimes for shorter code as the compiler looks for functions by default in the std namespace. Example I can use
using namespace std
court<<"hello";
If I hadn't mentioned my namespace
std::cout<<"hello";
All of this assumes that I have the iostream header
as for std::stoi is google it , the first link should be to the documentation , which should be self explanatory , if you still have doubts then come ask again
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Using namespace std is just for convenience and sometimes for shorter code as the compiler looks for functions by default in the std namespace. Example I can use
using namespace std
court<<"hello";
If I hadn't mentioned my namespace
std::cout<<"hello";
All of this assumes that I have the iostream header
as for std::stoi is google it , the first link should be to the documentation , which should be self explanatory , if you still have doubts then come ask again
add a comment |
up vote
0
down vote
Using namespace std is just for convenience and sometimes for shorter code as the compiler looks for functions by default in the std namespace. Example I can use
using namespace std
court<<"hello";
If I hadn't mentioned my namespace
std::cout<<"hello";
All of this assumes that I have the iostream header
as for std::stoi is google it , the first link should be to the documentation , which should be self explanatory , if you still have doubts then come ask again
add a comment |
up vote
0
down vote
up vote
0
down vote
Using namespace std is just for convenience and sometimes for shorter code as the compiler looks for functions by default in the std namespace. Example I can use
using namespace std
court<<"hello";
If I hadn't mentioned my namespace
std::cout<<"hello";
All of this assumes that I have the iostream header
as for std::stoi is google it , the first link should be to the documentation , which should be self explanatory , if you still have doubts then come ask again
Using namespace std is just for convenience and sometimes for shorter code as the compiler looks for functions by default in the std namespace. Example I can use
using namespace std
court<<"hello";
If I hadn't mentioned my namespace
std::cout<<"hello";
All of this assumes that I have the iostream header
as for std::stoi is google it , the first link should be to the documentation , which should be self explanatory , if you still have doubts then come ask again
answered Nov 11 at 4:45
Niteya Shah
14418
14418
add a comment |
add a comment |
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%2f53245782%2fusing-a-function-with-a-string-parameter-for-input-validation%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
The easy way is to use
std::stoi
to turn thestring
into a number (or die trying-remember to handle the exception) and then process the number normally. You can also short-circuit all of the conversion if thestring
starts with a'-'
character.– user4581301
Nov 11 at 4:21
Side note: Stack Overflow isn't a forum. It's a Q&A. You ask a question, people offer answers. Don't use answers to communicate information with people unless it really is an answer. Amend the question with new information. You pick the most helpful of the answers that resolve your question as the "Official" answer. Don't accept right away. Test the answers first, and who knows? Someone might as a genius answer after a few less brilliant answers are posted.
– user4581301
Nov 11 at 4:26
1
Meant to mention this in the post I will edit it shortly. My professor requires every assignment to be done using namespace std. so I am not sure what std::stoi is.
– Cookie PePe
Nov 11 at 4:32
std::stoi
breaks down into thestoi
function in thestd
namespace. That means you should be good. The only caveat isstoi
isn't in older (pre 2011) versions of the C++ standard. If you're learning old C++, you'll have to usestd::strtol
or (sneaky sneaky)std::stroul
. The u instrtoul
stands for unsigned. It will reject a negative number.– user4581301
Nov 11 at 4:50