samedi 30 janvier 2021

How can I change a global variable's value inside an if-else statement

'''
#include <iostream>
#include<string>
using namespace std;

int main()
{
  cout << "-------------\n|   |   |   |\n-------------\n|   |   |   |\n-------------\n|   |   |   |\n-------------" << endl; //signals the start of program
  int row;
  int col;
  string board[3][3];
  bool win = false; // set the intial state to not be winning

  for (int i =0;i<3;i++)
  {
    for(int j = 0; j<3 ; j++)
    {
      board[i][j]=" ";
    }
  }
  // The above code has intialized a matrix waiting to be filled in

  for (int i = 0;i<9;i++)
  {
    string player_input = "O";
    string * s_pointer = & player_input;
    if (i%2 == 0){
    string player_input = "X";
    }

    cout << "Player " << * s_pointer <<": Enter your desired row (1-3):" << endl;
    cin >> row;
    cout << "Player " << * s_pointer <<": Enter your desired column (1-3):" << endl;
    cin >> col;

    board[row-1][col-1] = * s_pointer;

    // The above code asks for input and then assign it to the row,col we've intialized before

    for (int i =0;i<3;i++) // this for loop basically prints out the whole grid beside the very last line of it
      {
        cout <<"-------------" << endl;
        for(int j = 0; j<3; j++)
          {
            cout << "| " << board[i][j] << " ";
          }
          cout<<"|"<<endl;
      }
      cout <<"-------------" << endl;}

      // The idea here is to use a for loop and switch between X and O input for each for loop, concantenate the string
      // Also the board's input switches between X and O depends on what turn it is, we can control this using if else
      // Check for the result of the match at 5-9 rounds as only till those rounds are results to be determined
      // Try utilize external function for cleaner format
  }
'''

Hi guys, I'm wondering why this code wouldn't work as it doesn't seem like the value of player_input changes from the if-else statement.

Also, why do I not need "return 0" and closing parentheses at the end? As it seems like If I include it, it warns 2 errors: " expected unqualified-id ", "extraneous closing brace ('}')"

Sorry if this question is too much of a newbie question, thank you so much, guys!

Aucun commentaire:

Enregistrer un commentaire