C++ infinite loop in code that should move forward

For some reason, this block of code is indefinitely looping the cin question, rather than moving forward in the program.

  const int SIZE = 300;    // Number of household sizes
   int householdSizes[SIZE];    // Array used to store 300 household sizes
   int x; 
   int limit = SIZE;
   int householdSize = 0;
   int pairsToCompare;
   bool switchOccurred; 
   int temp;
   double sum = 0;
   double mean = 0;
   double median = 0;

   // Input household size      
   cout << "Enter household size or 999 to quit: ";
   cin >> householdSize;
   // This is the work done in the fillArray() function
   x = 0;
   while(x < limit && householdSize != 999)   
      // Place value in array.
      householdSizes[x] = householdSize;
      // Calculate total of household sizes
      x++;    // Get ready for next input item.
      cout << "Enter household size or 999 to quit: ";
      cin >> householdSize;
   }  // End of input loop.

Read more here: https://stackoverflow.com/questions/67930567/c-infinite-loop-in-code-that-should-move-forward

Content Attribution

This content was originally published by Yellowslotcar at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: