reseting a variable

How can I reset the number assigned to a variable after it reaches the end of the code within a function?

Please give us some example code to show us what you mean. Currently, I think you mean this:

1
2
3
4
5
6
7
void function()
{
  int x = 6;
  // LOTS OF CODE
   
  // HOW DO I SET THE VALUE OF X HERE?
}
example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iomanip>
using namespace std;

int main (){
int y = 1;
int n = 0;

cout<<"would you like to perform a computation? ";
cin>>answer;

if (answer = 1){
function()
   }
else if (answer = 0){
exit(0);
   }
}
function(){
int variable;
cout<<"input variable for random calculations: ";
cin>>variable;
cout<<variable<<"squared is: "<<variable * variable;

//I would like to have some way to reset variable right here so that when it's called
//again in the redo function it doesnt loop the final cout statement of the function()

redo()
}
redo(){
cout<<"would you like to do this again? ";
cin>>yesNo;
if (yesNo = 1){
function();                        //
}
else(){
exit(0);
}
}
Last edited on
In this code, every time function is called, a whole new int variable is created. A whole new one. Any int variable from a previous call to function has no effect whatsoever.
I found my issue then! Thanks Moschops. I had my variable set as a global rather than inside my function so it was never reverting back to the originally undefined variable.
@cb32

Another problem with your code, is you are using assign instead of checking the variable values.

ex.
if (answer = 1)
should be
if (answer == 1)

same with the
else if (answer = 0)
should be
else if (answer == 0)

and also
if (yesNo = 1)
should be
if (yesNo == 1)
Topic archived. No new replies allowed.