Q:

C++ Program Debugging

I seem to be having some problems with my program. It compiles fine, but when I get to my first loop it asks me for a positive integer than it asks me again like its suppose too. Than it hops down a blank space and wont run any further until you enter another number which its not suppose to do. But than when you enter a number it goes back and asks me to enter an integer also like its suppose to the problem is, IT will do this infinite amount of times until I quit the program. Any suggestions as to why this is happening?
/* Search the entries of the n X n matrix mat in rowwise order for an entry to item */
#include <iostream>

using namespace std;

int main(void)

{ 

int n=10, item, row=3, col=3, mat[row][col];
bool found;

for (int row = 0; row < 3; row++)
        for (int col = 0; col < 3; col++)
        {
                cout << "Enter Positive Integer :  ";
                        cin >> row;
                cout << " Enter Positive Integer : ";
                cin >> mat[row][col];
        }

cout << "Enter a positive integer you want to be searched: ";
        cin >> item;

for(int i=0; i<row; ++i)
{
     for(int j=0; j<col; ++j)
     {
          if(mat[row][col] == item)
              found = true;
          else
               found = false;
     }  
}

if(found==true)
  cout << "item found" ;
else
    cout << "item found ";

return 0;
}

A:

The line for (int cols = 0; cols < 5; col++) increments the variable col, not cols. Since cols is always 0, the loop will never terminate.
You've done the same in for (int rows = 0; rows < 5; row++). Infinite loops often occur due to typos in loop conditions ;)
I would also like to point out some logic errors during your search:
for(int i=0; i<row; ++i)
{
     for(int j=0; j<col; ++j)
     {
          if(mat[row][col] == item)
              found = true;
          else
               found = false;
    }  
}

When that loop ends, found will only be true if mat[row-1][col-1] == item (the very last element). Heck, row and col never change in the loop so you are repeatedly checking the exact same element every time! You should also expect even more funky program behavior if you don't get a grasp on your variable names. I strongly recommend adding debug statements to see how your variables are being modified throughout the program (aka: cout << "rows = " << rows << endl;, and cout << "i = " << i << endl;, etc). You're preparing a recipe for disaster when you reuse your variables.
Disclaimer: Reusing variables is not always a bad thing. However, it's best to avoid it until you have a stronger understanding of variables.

