2021年4月22日星期四

C++ Variables not updating in loop

For some reason the values in the loop do not update. Instead of taking the new values they are assigned in the loop they stay the same as when first assigned. Why is this?

  using namespace std;    int main() {        int town_A_Pop;      int town_A_Growth;      int town_B_Pop;      int town_B_Growth;      int years = 0;        cout << "Enter the polulation of town A and of town B:" << endl;      cin >> town_A_Pop >> town_B_Pop;      cout << "Enter the growth rate of town A and of town B:" << endl;      cin >> town_A_Growth >> town_B_Growth;        do {        town_A_Pop = town_A_Pop * (1 + (town_A_Growth / 100));        cout << town_A_Pop << endl;        town_B_Pop = town_B_Pop * (1 + (town_B_Growth / 100));        cout << town_B_Pop << endl;        years++;      }      while (town_A_Pop < town_B_pop);        cout << "It took " << years << " years for Town A to overtake Town B." << endl;      cout << "Town A Population: " << town_A_Pop << endl;      cout << "Town B Population: " << town_B_Pop << endl;        return 0;  }      
https://stackoverflow.com/questions/67223792/c-variables-not-updating-in-loop April 23, 2021 at 11:54AM

没有评论:

发表评论