C ++不会更改变量值

时间:2018-12-04 00:56:20

标签: c++

当我输入66以外的数字时,我希望代码退出while循环;我让它打印'plz'的值,以查看该值是否正在更改。它没有变化,即使我键入plz = o,它仍保持为1;

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

int main()
{
  cout << "welcome to the random number generator, please enter a number\n";
  int plz=1, d;
  d=1;
  while (plz>0)
  {
    d=d+1;
    int g;
    cin >> g;
    g = 666;
    if (g==666)
      int j;
    else
      plz=0;
    cout << plz << "\n";
  }
  srand (d);
  d = rand();
  cout << d << "\n";
}

1 个答案:

答案 0 :(得分:2)

仅当plz不是 666时更改g。因为您刚刚将其指定为666,所以永远不会发生。

相关问题