找到你要的答案

Q:Make variables forget the previous values?

Q:使变量忘记以前的值?

I'm having a really irritating problem.

Firstly, I wrote a program that runs only for once and everything was perfect until I decided to make it re-executable with an user input.

Now I'm in a big trouble. When I re-execute the program, it falls into the wrong switch or if statements since the variables doesn't re-initialize themselves. To portray it:

// Libraries and other stuff

int numA, numB, numC;
char charA = 'A', charB = 'B', charC = 'C';

int main() {

do {

// Randomly assigning some numbers between 1-3 range into numA, numB and numC

...

// Converting the integers above into chars depending on the random numbers

switch (numA)
{
  case 1:
  numA = charA;
  break;

  case 2:
  numA = charB;
  break;

  case 3
  numA = charC;
  break;
}

switch (nu...

...

// A lot of IFs and SWITCHs that consistently changes the values above within themselves.

...

// Taking 1 input from user to re-execute the program

} while (input == 1)

return 0;

}

I know I messed everything up with not initializing the variables properly at first but I wasn't planing to make it re-executable when I started creating it, and now I'm looking for the optimal way to escape. Can I somehow make variables forget the previous values that they carry? Or do I really need to re-initialize everything from scratch?

我有一个很烦人的问题。

首先,我写了一个程序,只运行一次,一切都是完美的,直到我决定让它重新执行与用户输入。

现在我有一个大麻烦。当我重新执行程序,它落入错误的开关或语句,因为变量不重新初始化自己。描绘它:

// Libraries and other stuff

int numA, numB, numC;
char charA = 'A', charB = 'B', charC = 'C';

int main() {

do {

// Randomly assigning some numbers between 1-3 range into numA, numB and numC

...

// Converting the integers above into chars depending on the random numbers

switch (numA)
{
  case 1:
  numA = charA;
  break;

  case 2:
  numA = charB;
  break;

  case 3
  numA = charC;
  break;
}

switch (nu...

...

// A lot of IFs and SWITCHs that consistently changes the values above within themselves.

...

// Taking 1 input from user to re-execute the program

} while (input == 1)

return 0;

}

我知道我把事情搞砸了没有初始化的变量在第一但我不打算让它重新执行的时候我开始创造它,现在我正在寻找逃跑的最佳途径。我能不知何故使变量忘记以前的价值观,他们进行?还是我真的需要从头开始重新初始化?

answer1: 回答1:

Move declaration of your variables into a scope inside the loop, so they will be reinitialized on each pass.

int main() {

do {
//new scope
{
int numA, numB, numC;
char charA = 'A', charB = 'B', charC = 'C';

// Randomly assigning some numbers between 1-3 range into numA, numB and numC

...

// Converting the integers above into chars depending on the random numbers

switch (numA)
{
  case 1:
  numA = charA;
  break;

  case 2:
  numA = charB;
  break;

  case 3
  numA = charC;
  break;
}

switch (nu...

...

// A lot of IFs and SWITCHs that consistently changes the values above within themselves.

...

// Taking 1 input from user to re-execute the program
}//end of scope
} while (input == 1)

However you should understand, that usage of uninitialized variables is undefined behavior and should always be avoided.

Also your char variables initialization (char charA = 'charA') is completely invalid. You could not store 6 character string (5 characters and null terminator) in one character variable. You either should use char* or this is simple typographical error and it should be char charA = 'A'.

移动你的变量声明为内部循环的范围,所以他们将初始化每个通。

int main() {

do {
//new scope
{
int numA, numB, numC;
char charA = 'A', charB = 'B', charC = 'C';

// Randomly assigning some numbers between 1-3 range into numA, numB and numC

...

// Converting the integers above into chars depending on the random numbers

switch (numA)
{
  case 1:
  numA = charA;
  break;

  case 2:
  numA = charB;
  break;

  case 3
  numA = charC;
  break;
}

switch (nu...

...

// A lot of IFs and SWITCHs that consistently changes the values above within themselves.

...

// Taking 1 input from user to re-execute the program
}//end of scope
} while (input == 1)

但是你要明白,那未初始化变量的使用未定义的行为和应该避免的。

同时你的char变量初始化(char轮藻=“甜心”)是完全无效。不能在一个字符变量中存储6个字符串(5个字符和null终止符)。你应该使用char *或者是简单的印刷错误,应该是字符特征=“A”。

answer2: 回答2:

You initialized all chars to 'c'. As you are initialising all chars to 'charA', 'charB' & 'charC' but while initialising chars it need only one character like char ch1= '6', ch2='\n' ; As all the initializers have first letter common that is 'c'. That's why all initialized to 'c'.
Also you forgot the semicolon after while() of do.
Also you are using numA as switch control variable. & inside switch statement you are changing value inside numA. This is not wrong, but bad practice. Sorry for many edit, as I'm on stackexchange app I need to see question many times.

You initialized all chars to 'c'. As you are initialising all chars to 'charA', 'charB' & 'charC' but while initialising chars it need only one character like char ch1= '6', ch2='\n' ; As all the initializers have first letter common that is 'c'. That's why all initialized to 'c'.
Also you forgot the semicolon after while() of do.
Also you are using numA as switch control variable. & inside switch statement you are changing value inside numA. This is not wrong, but bad practice. Sorry for many edit, as I'm on stackexchange app I need to see question many times.

c++