C++不改变变量值

C++不改变变量值,c++,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 en

当我输入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";
}

仅当g不是666时才更改plz;由于您刚刚将其指定为666,这将永远不会发生。

谢谢,我刚刚注意到我将cin>>g;在声明gIf之前,它不会编译;但是你把g=666;就在if之前,这导致了我描述的行为。我的意思是定义。我把cin>>g;在我定义它之前。