The int wont change to anything except 0









up vote
-4
down vote

favorite












I am trying to make a function that changes the cout and the cin without copy pasting the code but when I try to use it, it always input 0



void enterdata(string data, int data2) // Inputing datas

cout << "Please Enter your ";
cout << data.c_str() << endl;
cin >> data2;

void input1() // Saving/Creating New

system("cls");
enterdata("Mobile Number", number);
cout << "Your number is " << number << endl;










share|improve this question























  • Pass number as reference like this void enterdata(string data, int& data2). This is because data2 is a function parameter in which you are taking input, but it is call by value, so actual variable number wont get changed and you will get some default or garbage value.
    – kiner_shah
    Nov 11 at 12:07







  • 1




    omg thank you so much it was so simple
    – Riebyfe Edelweiss
    Nov 11 at 12:09











  • Since the suggested answers are correct, but enterdata can be improved more with void enterdata(string const &data, int &data2) // Inputing datas cout << "Please Enter your " << data << endl; cin >> data2; No need for .c_str() or to copy the string data.
    – Bo R
    Nov 11 at 13:04















up vote
-4
down vote

favorite












I am trying to make a function that changes the cout and the cin without copy pasting the code but when I try to use it, it always input 0



void enterdata(string data, int data2) // Inputing datas

cout << "Please Enter your ";
cout << data.c_str() << endl;
cin >> data2;

void input1() // Saving/Creating New

system("cls");
enterdata("Mobile Number", number);
cout << "Your number is " << number << endl;










share|improve this question























  • Pass number as reference like this void enterdata(string data, int& data2). This is because data2 is a function parameter in which you are taking input, but it is call by value, so actual variable number wont get changed and you will get some default or garbage value.
    – kiner_shah
    Nov 11 at 12:07







  • 1




    omg thank you so much it was so simple
    – Riebyfe Edelweiss
    Nov 11 at 12:09











  • Since the suggested answers are correct, but enterdata can be improved more with void enterdata(string const &data, int &data2) // Inputing datas cout << "Please Enter your " << data << endl; cin >> data2; No need for .c_str() or to copy the string data.
    – Bo R
    Nov 11 at 13:04













up vote
-4
down vote

favorite









up vote
-4
down vote

favorite











I am trying to make a function that changes the cout and the cin without copy pasting the code but when I try to use it, it always input 0



void enterdata(string data, int data2) // Inputing datas

cout << "Please Enter your ";
cout << data.c_str() << endl;
cin >> data2;

void input1() // Saving/Creating New

system("cls");
enterdata("Mobile Number", number);
cout << "Your number is " << number << endl;










share|improve this question















I am trying to make a function that changes the cout and the cin without copy pasting the code but when I try to use it, it always input 0



void enterdata(string data, int data2) // Inputing datas

cout << "Please Enter your ";
cout << data.c_str() << endl;
cin >> data2;

void input1() // Saving/Creating New

system("cls");
enterdata("Mobile Number", number);
cout << "Your number is " << number << endl;







c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 12:52









kiner_shah

1,13121323




1,13121323










asked Nov 11 at 12:04









Riebyfe Edelweiss

11




11











  • Pass number as reference like this void enterdata(string data, int& data2). This is because data2 is a function parameter in which you are taking input, but it is call by value, so actual variable number wont get changed and you will get some default or garbage value.
    – kiner_shah
    Nov 11 at 12:07







  • 1




    omg thank you so much it was so simple
    – Riebyfe Edelweiss
    Nov 11 at 12:09











  • Since the suggested answers are correct, but enterdata can be improved more with void enterdata(string const &data, int &data2) // Inputing datas cout << "Please Enter your " << data << endl; cin >> data2; No need for .c_str() or to copy the string data.
    – Bo R
    Nov 11 at 13:04

















  • Pass number as reference like this void enterdata(string data, int& data2). This is because data2 is a function parameter in which you are taking input, but it is call by value, so actual variable number wont get changed and you will get some default or garbage value.
    – kiner_shah
    Nov 11 at 12:07







  • 1




    omg thank you so much it was so simple
    – Riebyfe Edelweiss
    Nov 11 at 12:09











  • Since the suggested answers are correct, but enterdata can be improved more with void enterdata(string const &data, int &data2) // Inputing datas cout << "Please Enter your " << data << endl; cin >> data2; No need for .c_str() or to copy the string data.
    – Bo R
    Nov 11 at 13:04
















Pass number as reference like this void enterdata(string data, int& data2). This is because data2 is a function parameter in which you are taking input, but it is call by value, so actual variable number wont get changed and you will get some default or garbage value.
– kiner_shah
Nov 11 at 12:07





Pass number as reference like this void enterdata(string data, int& data2). This is because data2 is a function parameter in which you are taking input, but it is call by value, so actual variable number wont get changed and you will get some default or garbage value.
– kiner_shah
Nov 11 at 12:07





1




1




omg thank you so much it was so simple
– Riebyfe Edelweiss
Nov 11 at 12:09





omg thank you so much it was so simple
– Riebyfe Edelweiss
Nov 11 at 12:09













Since the suggested answers are correct, but enterdata can be improved more with void enterdata(string const &data, int &data2) // Inputing datas cout << "Please Enter your " << data << endl; cin >> data2; No need for .c_str() or to copy the string data.
– Bo R
Nov 11 at 13:04





Since the suggested answers are correct, but enterdata can be improved more with void enterdata(string const &data, int &data2) // Inputing datas cout << "Please Enter your " << data << endl; cin >> data2; No need for .c_str() or to copy the string data.
– Bo R
Nov 11 at 13:04













3 Answers
3






active

oldest

votes

















up vote
2
down vote













Your int data2 variable is a variable that's local to enterdata. It will be destroyed once the function returns.



If you want to change the passed-in variable you want to take in a reference to the passed argument:



void enterdata(string data, int& data2)





share|improve this answer



























    up vote
    1
    down vote













    You must pass the value as reference when calling enterdata(). This is because you are expecting the value to change and then use that value later from the calling function. Passing as reference will allow you to use the variable with changed value. Not doing so will result in some default or garbage value in the variable.



    So your code should change like this:



    void enterdata(string data, int& data2) // Inputing datas <-- note change here

    cout << "Please Enter your ";
    cout << data.c_str() << endl;
    cin >> data2;

    void input1() // Saving/Creating New

    system("cls");
    enterdata("Mobile Number", number);
    cout << "Your number is " << number << endl;






    share|improve this answer



























      up vote
      0
      down vote













      There are some recomendations to change data2 to a reference or pointer. But there is a second, simpler method. Just return the value



      int enterdata(string data) // Inputing datas

      cout << "Please Enter your ";
      cout << data.c_str() << endl;
      int data2;
      cin >> data2;
      return data2;

      void input1() // Saving/Creating New

      system("cls");
      int number = enterdata("Mobile Number");
      cout << "Your number is " << number << endl;






      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%2f53248539%2fthe-int-wont-change-to-anything-except-0%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        2
        down vote













        Your int data2 variable is a variable that's local to enterdata. It will be destroyed once the function returns.



        If you want to change the passed-in variable you want to take in a reference to the passed argument:



        void enterdata(string data, int& data2)





        share|improve this answer
























          up vote
          2
          down vote













          Your int data2 variable is a variable that's local to enterdata. It will be destroyed once the function returns.



          If you want to change the passed-in variable you want to take in a reference to the passed argument:



          void enterdata(string data, int& data2)





          share|improve this answer






















            up vote
            2
            down vote










            up vote
            2
            down vote









            Your int data2 variable is a variable that's local to enterdata. It will be destroyed once the function returns.



            If you want to change the passed-in variable you want to take in a reference to the passed argument:



            void enterdata(string data, int& data2)





            share|improve this answer












            Your int data2 variable is a variable that's local to enterdata. It will be destroyed once the function returns.



            If you want to change the passed-in variable you want to take in a reference to the passed argument:



            void enterdata(string data, int& data2)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 11 at 12:11









            Jesper Juhl

            15.7k32244




            15.7k32244






















                up vote
                1
                down vote













                You must pass the value as reference when calling enterdata(). This is because you are expecting the value to change and then use that value later from the calling function. Passing as reference will allow you to use the variable with changed value. Not doing so will result in some default or garbage value in the variable.



                So your code should change like this:



                void enterdata(string data, int& data2) // Inputing datas <-- note change here

                cout << "Please Enter your ";
                cout << data.c_str() << endl;
                cin >> data2;

                void input1() // Saving/Creating New

                system("cls");
                enterdata("Mobile Number", number);
                cout << "Your number is " << number << endl;






                share|improve this answer
























                  up vote
                  1
                  down vote













                  You must pass the value as reference when calling enterdata(). This is because you are expecting the value to change and then use that value later from the calling function. Passing as reference will allow you to use the variable with changed value. Not doing so will result in some default or garbage value in the variable.



                  So your code should change like this:



                  void enterdata(string data, int& data2) // Inputing datas <-- note change here

                  cout << "Please Enter your ";
                  cout << data.c_str() << endl;
                  cin >> data2;

                  void input1() // Saving/Creating New

                  system("cls");
                  enterdata("Mobile Number", number);
                  cout << "Your number is " << number << endl;






                  share|improve this answer






















                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    You must pass the value as reference when calling enterdata(). This is because you are expecting the value to change and then use that value later from the calling function. Passing as reference will allow you to use the variable with changed value. Not doing so will result in some default or garbage value in the variable.



                    So your code should change like this:



                    void enterdata(string data, int& data2) // Inputing datas <-- note change here

                    cout << "Please Enter your ";
                    cout << data.c_str() << endl;
                    cin >> data2;

                    void input1() // Saving/Creating New

                    system("cls");
                    enterdata("Mobile Number", number);
                    cout << "Your number is " << number << endl;






                    share|improve this answer












                    You must pass the value as reference when calling enterdata(). This is because you are expecting the value to change and then use that value later from the calling function. Passing as reference will allow you to use the variable with changed value. Not doing so will result in some default or garbage value in the variable.



                    So your code should change like this:



                    void enterdata(string data, int& data2) // Inputing datas <-- note change here

                    cout << "Please Enter your ";
                    cout << data.c_str() << endl;
                    cin >> data2;

                    void input1() // Saving/Creating New

                    system("cls");
                    enterdata("Mobile Number", number);
                    cout << "Your number is " << number << endl;







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 11 at 12:13









                    kiner_shah

                    1,13121323




                    1,13121323




















                        up vote
                        0
                        down vote













                        There are some recomendations to change data2 to a reference or pointer. But there is a second, simpler method. Just return the value



                        int enterdata(string data) // Inputing datas

                        cout << "Please Enter your ";
                        cout << data.c_str() << endl;
                        int data2;
                        cin >> data2;
                        return data2;

                        void input1() // Saving/Creating New

                        system("cls");
                        int number = enterdata("Mobile Number");
                        cout << "Your number is " << number << endl;






                        share|improve this answer
























                          up vote
                          0
                          down vote













                          There are some recomendations to change data2 to a reference or pointer. But there is a second, simpler method. Just return the value



                          int enterdata(string data) // Inputing datas

                          cout << "Please Enter your ";
                          cout << data.c_str() << endl;
                          int data2;
                          cin >> data2;
                          return data2;

                          void input1() // Saving/Creating New

                          system("cls");
                          int number = enterdata("Mobile Number");
                          cout << "Your number is " << number << endl;






                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            There are some recomendations to change data2 to a reference or pointer. But there is a second, simpler method. Just return the value



                            int enterdata(string data) // Inputing datas

                            cout << "Please Enter your ";
                            cout << data.c_str() << endl;
                            int data2;
                            cin >> data2;
                            return data2;

                            void input1() // Saving/Creating New

                            system("cls");
                            int number = enterdata("Mobile Number");
                            cout << "Your number is " << number << endl;






                            share|improve this answer












                            There are some recomendations to change data2 to a reference or pointer. But there is a second, simpler method. Just return the value



                            int enterdata(string data) // Inputing datas

                            cout << "Please Enter your ";
                            cout << data.c_str() << endl;
                            int data2;
                            cin >> data2;
                            return data2;

                            void input1() // Saving/Creating New

                            system("cls");
                            int number = enterdata("Mobile Number");
                            cout << "Your number is " << number << endl;







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 11 at 13:57









                            HAL9000

                            2386




                            2386



























                                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%2f53248539%2fthe-int-wont-change-to-anything-except-0%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







                                這個網誌中的熱門文章

                                Barbados

                                How to read a connectionString WITH PROVIDER in .NET Core?

                                Node.js Script on GitHub Pages or Amazon S3