what is the different about C++ n C..
i have a program below which my friends say that it is on C.. how do i change to C++..

#include <stdio.h>
#include <stdlib.h>

char matrix[3][3];  /* the tic tac toe matrix */

char check(void);
void init_matrix(void);
void get_player_move(void);
void get_computer_move(void);
void disp_matrix(void);

int main(void)
{
  char done;

  printf("This is the game of Tic Tac Toe.\n");
  printf("You will be playing against the computer.\n");

  done =  ' ';
  init_matrix();

  do {
    disp_matrix();
    get_player_move();
    done = check(); /* see if winner */
    if(done!= ' ') break; /* winner!*/
    get_computer_move();
    done = check(); /* see if winner */
  } while(done== ' ');

  if(done=='X') printf("You won!\n");
  else printf("I won!!!!\n");
  disp_matrix(); /* show final positions */

  return 0;
}

/* Initialize the matrix. */
void init_matrix(void)
{
  int i, j;

  for(i=0; i<3; i++)
    for(j=0; j<3; j++) matrix[i][j] =  ' ';
}

/* Get a player's move. */
void get_player_move(void)
{
  int x, y;

  printf("Enter X,Y coordinates for your move: ");
  scanf("%d%*c%d", &x, &y);

  x--; y--;

  if(matrix[x][y]!= ' '){
    printf("Invalid move, try again.\n");
    get_player_move();
  }
  else matrix[x][y] = 'X';
}

/* Get a move from the computer. */
void get_computer_move(void)
{
  int i, j;
  for(i=0; i<3; i++){
    for(j=0; j<3; j++)
      if(matrix[i][j]==' ') break;
    if(matrix[i][j]==' ') break;
  }

  if(i*j==9)  {
    printf("draw\n");
    exit(0);
  }
  else
    matrix[i][j] = 'O';
}

/* Display the matrix on the screen. */
void disp_matrix(void)
{
  int t;

  for(t=0; t<3; t++) {
    printf(" %c | %c | %c ",matrix[t][0],
            matrix[t][1], matrix [t][2]);
    if(t!=2) printf("\n---|---|---\n");
  }
  printf("\n");
}

/* See if there is a winner. */
char check(void)
{
  int i;

  for(i=0; i<3; i++)  /* check rows */
    if(matrix[i][0]==matrix[i][1] &&
       matrix[i][0]==matrix[i][2]) return matrix[i][0];

  for(i=0; i<3; i++)  /* check columns */
    if(matrix[0][i]==matrix[1][i] &&
       matrix[0][i]==matrix[2][i]) return matrix[0][i];

  /* test diagonals */
  if(matrix[0][0]==matrix[1][1] &&
     matrix[1][1]==matrix[2][2])
       return matrix[0][0];

  if(matrix[0][2]==matrix[1][1] &&
     matrix[1][1]==matrix[2][0])
       return matrix[0][2];

  return ' ';
}

Depends on what your friend means.. does he mean use C++ language features to solve the problem that is solved using C..
Or does he mean just make this code compile using a C++ compiler.. ?

what he mean is using C++ language n code.. what is the dif.. how can i change it

what he mean is using C++ language n code.. what is the dif.. how can i change it

from what i understand C++ is a subset of C so all C code should compile in a C++ compiler.

without going through your code line by line i guess it would be a matter of changing how data is cast and how pointers are handled (slight differences)

from what i understand C++ is a subset of C so all C code should compile in a C++ compiler.

Next you'll say Earth is the center of Universe.. :D

C++ is superset of C.

what he mean is using C++ language n code.. what is the dif.. how can i change it

There is a subtle difference between using C++ language to code and using C++ language's features (that are NOT available in C) to code.
In simplest terms C is procedural and C++ is Object Oriented. So to really use the features of C++ you should have some classes and objects and so on..

so all C code should compile in a C++ compiler.

No it won't. C++ made several syntax changes such as c++ requires type casts where C does not. Here is a more complete list of differences.

One way to make your code more c++'ish is to replace all printf() function calls with cout that is declared in <iostream> header file and remove <stdio.h> c++ standard header files to not have the .h extension.

No it won't. C++ made several syntax changes such as c++ requires type casts where C does not. Here is a more complete list of differences.

One way to make your code more c++'ish is to replace all printf() function calls with cout that is declared in <iostream> header file and remove <stdio.h> c++ standard header files to not have the .h extension.

In THIS particualar case, the code may indeed compile under C++.
It's definately C. I am embarassed to say that I don't even understand what the arguments in a printf() do! I'm comming in late in the game, so I'm more or less C++ only. I've been steering clear of C for now for fear of confusing the two. In reality I will need to know both.

> from what i understand C++ is a subset of C
C++ is the functional superset of C.

> In reality I will need to know both.
Learning C++ should do the job. Like I said, since C++ is the functional superset of C, knowing C++ should as such make you proficient in C, though I am sure many people here would disagree with it. ;-)

sub / super pshhh it was a typo :P

look at what time i posted it. LOL

sub / super pshhh it was a typo

You just lived up to your name.. :)

> In reality I will need to know both.
Learning C++ should do the job. Like I said, since C++ is the functional superset of C, knowing C++ should as such make you proficient in C, though I am sure many people here would disagree with it. ;-)

You're right, there will be disagreement.

C and C++ have different mindsets. Knowing C++ you may know the syntax of C, but because one is a procedural language and the other an OO language, knowing one does not necessarily give you the skills to write the other. At least not efficiently.

Thats why I specifically highlighted the term 'functional'. If you know C its just a matter of developing the OO mentality. Plus a good beginner book doesn't dwell on the OO part of the language but what can be achieved with the language.

Come to think of it, C++ is IMO not pure OO. It gives you the impression of OO features being added to an imperative language to make it a mainstream one (OO was the buzzword in those days).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.