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;
c++
add a comment |
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;
c++
Pass number as reference like thisvoid 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 variablenumber
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 withvoid 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
add a comment |
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;
c++
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++
c++
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 thisvoid 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 variablenumber
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 withvoid 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
add a comment |
Pass number as reference like thisvoid 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 variablenumber
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 withvoid 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
add a comment |
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)
add a comment |
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;
add a comment |
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;
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Nov 11 at 12:11
Jesper Juhl
15.7k32244
15.7k32244
add a comment |
add a comment |
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;
add a comment |
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;
add a comment |
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;
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;
answered Nov 11 at 12:13
kiner_shah
1,13121323
1,13121323
add a comment |
add a comment |
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;
add a comment |
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;
add a comment |
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;
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;
answered Nov 11 at 13:57
HAL9000
2386
2386
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%2f53248539%2fthe-int-wont-change-to-anything-except-0%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
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 variablenumber
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