How can I convert C code to C++ in Visual Studio 2003 .NET?

  • Context: C/C++ 
  • Thread starter Thread starter UNIQNESS
  • Start date Start date
  • Tags Tags
    C++
Click For Summary
SUMMARY

The discussion focuses on converting C code to C++ using Visual Studio 2003 .NET. The original poster faced multiple errors during the conversion process and sought assistance in adapting the code for compatibility with C++ standards. Key insights include that C++ is a superset of C, meaning ANSI C code should compile with a C++ compiler, and that specific Borland extensions like clrscr and getch are not standard and should be removed or replaced. The recommended approach is to use Visual Studio 2003 .NET for the conversion while avoiding Visual C++ 6.0 due to its incompatibility with modern C++ standards.

PREREQUISITES
  • Familiarity with C and C++ programming languages
  • Understanding of Visual Studio 2003 .NET IDE
  • Knowledge of memory management in C/C++
  • Basic understanding of matrix operations in programming
NEXT STEPS
  • Learn how to replace non-standard functions like getch and clrscr in C++
  • Explore memory management techniques in C++ for dynamic arrays
  • Study the differences between C and C++ syntax and constructs
  • Investigate modern C++ compilers such as GCC and IDEs like Code::Blocks or Dev-C++
USEFUL FOR

This discussion is beneficial for programmers transitioning from C to C++, particularly those using Visual Studio 2003 .NET, as well as anyone interested in understanding matrix operations and memory management in C/C++.

UNIQNESS
Messages
18
Reaction score
0
I tried to convert from C++ to visual C++ .NET but got many errors. (I haven't used C in years) If anyone can do this for me, I'd really appreciate it.

Please convert the following to visual C++ .NET code (to run on microsoft visual studio 2003 .NET):

http://www.cplusplus.happycodings.com/Algorithms/code17.html

thanks in advance

[edit]Sorry, I meant to mention that I need to convert this to c++ visual .NET. I only learned c++ visual .NET and I'm not very familiar with some of the commands in c++ (even though they are very similar). The way arrays are arranged in that code is very different from how i learned it. Oh, and this is not for an assignment. I'm doing this to impress my girlfriend since she's learning matrices in her math class. Sorry, I meant to mention that I need to convert this to c++ visual .NET. I only learned c++ visual .NET and I'm not very familiar with some of the commands in c++ (even though they are very similar). The way arrays are arranged in that code is very different from how i learned it. Oh, and this is not for an assignment. I'm doing this to impress my girlfriend since she's learning matrices in her math class. (I also want to learn how to do this myself) Help would be greatly appreciated.
 
Last edited:
Technology news on Phys.org
1) If this is some kind of homework assignment, we won't do it for you.

2) It appears to be pretty atrocious code to begin with.

3) C++ is a superset of C, so anything written in ANSI C should compile just fine with a C++ compiler.

- Warren
 
And, incidentally, visual studio has a vanilla C compiler. (At least the version with which I'm familiar)
 
I don't think this is an assignment since only a few minor changes were needed to compile under gcc. The errors are mainly caused by the use of Borland extensions that are not part of the standard language (notably clrscr and getch). I just replaced getch and commented out clrscr to get it to compile. I can't say if this code actually works but the new version is below.

Note: do not use Visual C++ 6.0, it pre-dates the standard and is therefore not compatible with a number of standard C++ expressions. Plus it has some nasty bugs. If this is what you are stuck with then at least get the service packs. Better yet, move on to Code::Block or Dev-C++ which come packaged with gcc, a much better compiler. Both are free.

Technical note: C++ is not a strict superset of C, it has a few constructs that differ from C so an occasional valid C program may not compile under C++. But it's uncommon and required changes are often minor.

Code:
// Matrix Multiplication
#include <cstdio>
#include <cstdlib>

/* to initialize matrix */
int** init(int** arr,int row,int col) {
    int i=0,
        j=0;

    arr=(int**)malloc(sizeof(int)*row*col);

    for(i=0;i<row;i++) {
        for(j=0;j<col;j++) {
            *((arr+i)+j)=(int*)malloc(sizeof(int));
            *(*(arr+i)+j)=0;
        }
    }
    return arr;
}

/* to set value in matrix */
int** set(int** arr,int row,int col) {
    int i=0,
        j=0,
        val=0;

    for(i=0;i<row;i++) {
        for(j=0;j<col;j++) {
            printf("Enter value for row %d col %d  :",(i+1),(j+1));
            scanf("%d",&val);
            *(*(arr+i)+j)=val;
        }
    }
    return arr;
}


/* print values of the passed matrix */
void get(int** arr,int row,int col) {
    int i=0,
        j=0;

    for(i=0;i<row;i++) {
        for(j=0;j<col;j++) {
            printf("%d\t",*(*(arr+i)+j));
        }
        printf("\n");
    }
}

/* mutiply two matrices and return the resultant matrix */
int** mul(int** arr1,int** arr2,int row,int col,int col1) {
    int **result,
        i=0,
        j=0,
        k=0;

    result=init(result,row,col);

    for(i=0;i<row;i++) {
        for(j=0;j<col;j++) {
            for(k=0;k<col1;k++) {
                printf("%dX%d(%d)",*(*(arr1+i)+k),*(*(arr2+k)+j),
                       (*(*(arr1+i)+k))*(*(*(arr2+k)+j)));
                *(*(result+i)+j)+=(*(*(arr1+i)+k))*(*(*(arr2+k)+j));

                if (k!=(col1-1))
                    printf("+");
            }
            printf("\t");
        }
        printf("\n");
    }
    return result;
}

int main() {
    int row1=0,
        col1=1,
        row2=0,
        col2=0,
        **matrix1,
        **matrix2,
        **result;

//    clrscr();
    printf(" Enter number of row for first matrix ");
    scanf("%d",&row1);

    while (col1!=row2) {
        printf(" Enter number of column for first matrix ");
        scanf("%d",&col1);

        printf(" Enter number of row for second matrix ");
        scanf("%d",&row2);

        if (col1!=row2) {
//            clrscr();
            printf("Column number of first matrix must be "
                   "same as the row number of second matrix");
        }
    }

    printf(" Enter number of column for second matrix ");
    scanf("%d",&col2);

    matrix1=init(matrix1,row1,col1);
    matrix2=init(matrix2,row2,col2);
    /* setting values in matrix */
    printf("First matrix \n");
    set(matrix1,row1,col1)
    ;
    printf("Second matrix \n");
    set(matrix2,row2,col2)
    ;
    /* printint matrix */
//    clrscr();
    printf("       [ First matrix ]\n");
    get(matrix1,row1,col1)
    ;
    printf("       [ Second matrix ]\n");
    get(matrix2,row2,col2)
    ;

    printf("       [ Multiplication Result ]\n");
    result=mul(matrix1,matrix2,row1,col2,col1);
    get(result,row1,col2)
    ;
    printf("\n\t\t Thanks  from debmalya jash");

//    getch();
system("pause");

    free(matrix1);
    free(matrix2);
    free(result);

} /* end main */
 
Sorry, I meant to mention that I need to convert this to c++ visual .NET. I only learned c++ visual .NET and I'm not very familiar with some of the commands in c++ (even though they are very similar). The way arrays are arranged in that code is very different from how i learned it. Oh, and this is not for an assignment. I'm doing this to impress my girlfriend since she's learning matrices in her math class. (I also want to learn how it works myself) Help would be greatly appreciated.
 
if the problem is just getch and clrscr just dump the conio include in there.
 

Similar threads

  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 25 ·
Replies
25
Views
15K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
Replies
47
Views
5K
  • · Replies 16 ·
Replies
16
Views
5K