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++










share|improve this question
























  • 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








  • 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 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

















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++










share|improve this question
























  • 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








  • 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 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















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++










share|improve this question















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++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 4:33

























asked Nov 11 at 4:15









Cookie PePe

11




11












  • 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








  • 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 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




















  • 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








  • 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 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


















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














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






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%2f53245782%2fusing-a-function-with-a-string-parameter-for-input-validation%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
    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






    share|improve this answer

























      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






      share|improve this answer























        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






        share|improve this answer












        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 4:45









        Niteya Shah

        14418




        14418






























            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%2f53245782%2fusing-a-function-with-a-string-parameter-for-input-validation%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

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues