Thread: comparing array contents

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    okay now it ran, but for some reason, the entire loop was completely skipped over. that cannot be since while(1) and the only way to get out of the whole thing is if the if statements inside are true which have break; in them to get out of the loop. i never get asked for the row or column at all. it asks for names and goes to press ESC to exit...

    Code:
    //Dan Kemper     ([email protected])
    #include <iostream.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <windows.h>
    
    const int MAXROW = 3;
    const int MAXCOL = 3;
    
    int main()
    {
         //declare variables
         int k=0, j=0, z=1, row=0, col=0, win=0;
         int input=0;
         char p1[40],p2[40];
         //initialize array
         char arr[MAXROW][MAXCOL] =
                        {
                             { '-', '-', '-', },
                             { '-', '-', '-', },
                             { '-', '-', '-', }
                        };
         
         cout << "This is a tic-tac-toe game that I made.\n\n"; //program decription
         cout << "Press a key to continue...";
         getch();
         system("cls");
         //user input for names
         cout << "Player 1, enter your name: ";
         cin.get(p1,40);
         cin.ignore(80,'\n');
         cout << "\n\nPlayer 2, enter your name: ";
         cin.get(p2,40);
         cin.ignore(80,'\n');
         cout << "\n\nPress a key to continue...";
         getch();
         system("cls");
         
         while(1) //main loop; looks like infinate but is later broken using break;
         {
         while(z>=2) //internal loop
         {
          //prints out array
          for (k = 0; k < MAXROW; k++)
          {
                for (j = 0; j < MAXCOL; j++)
                {
                      cout << arr[k][j] << " ";
                }
                cout << endl;
          }
          
          if(z=1) //if its player 1's turn
          {
                 cout << p1 << ", enter your choice:" << endl << endl;
                 cout << "Row: ";
                 cin >> row; //input for row
                 cout << endl << "Coulumn: ";
                 cin >> col; //input for column
                 arr[row+1][col+1]='X'; //puts an X in the spot the user wants
          }
          else //if player 2's turn
          {
                 cout << p1 << ", enter your choice:" << endl << endl;
                 cout << "Row: ";
                 cin >> row; //input for row
                 cout << endl << "Coulumn: ";
                 cin >> col; //input for column
                 arr[row+1][col+1]='O'; //puts an O in the spot the user wants
          }
          
          z++; //increments variable which controld which persons turn it is
          
          
          /*         decides if player 1 wins          */
          
          if(arr[0][0]=='X' && arr[0][1]=='X' && arr[0][2]=='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[1][0]=='X' && arr[1][1]=='X' && arr[1][2]=='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[2][0]=='X' && arr[2][1]=='X' && arr[2][2]=='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[0][0]=='X' && arr[1][1]=='X' && arr[2][2]=='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[0][2]=='X' && arr[1][1]=='X' && arr[2][0]=='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          
          
          /*         decides if player 2 wins          */
          
          
          if(arr[0][0]=='O' && arr[0][1]=='O' && arr[0][2]=='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[1][0]=='O' && arr[1][1]=='O' && arr[1][2]=='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[2][0]=='O' && arr[2][1]=='O' && arr[2][2]=='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[0][0]=='O' && arr[1][1]=='O' && arr[2][2]=='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[0][2]=='O' && arr[1][1]=='O' && arr[2][0]=='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          
          
          
          }
          if(z=-1)
          {
                  break;
          }
          
          z=1;
          }
          
          system("cls");
          cout << "Press ESC to exit...";
          
          while(input!=27)
          {
                          input=getch();
          }
          
          return 0;
    }
    thanks for your help.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    More PIAs:
    Code:
          if(z=1) //if its player 1's turn
    Code:
          if(z=-1)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 4
    Last Post: 03-18-2009, 05:01 PM
  3. Using the debugger to see array contents
    By JohnnyCat in forum C++ Programming
    Replies: 2
    Last Post: 06-23-2005, 02:17 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM