简体   繁体   中英

C++ -- need help figuring out a while loop issue

So, need some direction on why this loop is loopin' forever. What I want it do is read a new m, which then sets the terms for the for loops. So, it reads m, which then is calculated into an array, and then reads a new m, etc. Currently it reads one m and does the for loops forever.

while(inputFile >> m) {
  while (m != 0) { 
    cout << "m=" << m << endl;
    for (i=0; i < (m*m); i++) { 
      cout << "m times m = " << (m*m) << endl;
      for (ROW=0; ROW < m; ROW++) {
        inputFile >> image[ROW][COL];
        cout << "array= " << image[ROW][COL] << endl;
          for (COL=0; COL < m; COL++) {
            inputFile >> image[ROW][COL];
            cout << "array= " << imagee[ROW][COL] << endl;
          }
        }
      }
    }
  }
}

Sample of input file:

3
1 2 3
4 5 6
7 8 9

You'll find that you won't have an infinite loop if you clean up your code a little bit:

while(inputFile >> m) {
  while (m != 0) {
    ...

can be combined into this:

while(inputFile >> m && m != 0) {

Now once the for-loop s finish, another m will be read from the inputFile .

Let's examine why it wasn't working before. Let's say inputFile extracted 4 and bound it to m .

while(4 != 0) is always true, so this is the infinite loop. m couldn't be read from inputFile again after this.

Alternatively you could write

while(inputFile >> m) {
      if (m != 0) {
        ...

This would guarantee the entire file is processed, skipping values of 0.

EDIT

You needed to consume the newline character with inputFile.get() after reading m .

I have written a small driver to show you how to do this, but I used std::vector s instead of arrays - they're a little easier to use.

Here's the ideone with two arrays being read.

Code:

#include <iostream>
#include <sstream>
#include <string>
#include <istream>
#include <vector>

void output_matrix(const std::vector<std::vector<int > > arr, size_t m) {
    std::cout << "Matrix we read in was: " << std::endl;
    for(size_t i = 0; i < m; ++i) {
        for(size_t j = 0; j < m; ++j) {
            std::cout << arr[i][j] << " ";
        }
        std::cout << std::endl;
    }
}

void read_matrix(std::istream &in) {
    size_t m = 0;
    while(in >> m && m > 0) {
        std::vector<std::vector<int > > arr(m);
        // Swallow the newline after reading in `m`.
        in.get();
        std::string line;
        for(size_t i = 0; i < m; ++i) {
            arr.resize(m);
            /* 
                `std::getline` will swallow newlines in other lines until `m`
                needs to be read again.
            */
            std::getline(std::cin, line);
            std::stringstream ss(line);
            for(size_t j = 0; j < m; ++j) {
                arr[i].resize(m);
                int temp = 0;
                ss >> temp;
                arr[i][j] = temp;
            }
        }
        output_matrix(arr, m);
    }
}

int main() {
    read_matrix(std::cin);
    return 0;
}

The problem is here:

while(inputFile >> m) {
  while (m != 0) {

You have an outer loop where you read into m and you have an inner loop which ends when m is 0. The problem is that the outer loop's condition is not checked again until the inner loop is completed. The inner loop will never be completed, since reading into m , which happens to be the outer check as well is never reached.

Generally, if you have loop1 and loop2, loop2 being the inner loop of loop1, then loop2 is reached through loop1, but as soon as loop2 is reached, it must be finished before loop1's condition is checked. If loop1 is not completed, then loop2 will be restarted and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM