translate this c languange into c++

so im only learning c since im fairly new and i would like to translate this code to c++ so that i could study it for future reference, all your help is highly appreciated

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 #include<stdio.h>
#include<conio.h>

void main(){
               
                int poly[100], m, r, i, q[100];
                printf("\t\tSYNTHETIC DIVISION");
                printf("\n Enter the highest degree of the equation (max 5): ");
                scanf("%d",&m);

                for(i=0;i<=m;i++){
                                printf("\n Coefficient x[%d] = ", m-i);
                                scanf("%d",&poly[i]);
                }

                printf("\n Enter the value of constant in (x-r) : ");
                scanf("%d",&r);
                q[0] = poly[0];
                for(i=1;i<=m;i++){
                                q[i] = (q[i-1]*r)+poly[i];
                }

                printf("\n Coefficients of quotient are: \n");
                for(i=0;i<m;i++){
                                printf("\t%d",q[i]);
                }
                printf("\n Remainder is: %d", q[m]);
getch();

}
The above code is not valid C in the first place:
http://ideone.com/gbTyKd
http://ideone.com/gvrXP7

How are we to convert an invalid program?
weird ive been using MVC++ 6.0 and im not getting any error..
First, conio.h is not standard C; it is propietary.

You have input (printf) and output (scanf). C++ has them too, but offers an alternative: streams. See http://www.cplusplus.com/doc/tutorial/basic_io/

Replace <stdio.h> with <cstdio> if you keep using the printf/scanf. The new header essentially includes the old header, and then some.
basically this part i dont understand


1
2
printf("\n Coefficient x[%d] = ", m-i);
scanf("%d",&poly[i]);
i dont think i made any mistakes but i did have to rush
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <vector>

int main(int argc, char *argv[])
{
	vector<int> poly, q;
	int m, r, i;

	std::cout<<"\t\tSYNTHETIC DIVISION"<<"\n Enter the highest degree of the equation (max 5): ";
	std::cin>> m;

	for(i=0;i<=m;i++){
		std::cout<<"\n Coefficient x["<< m-i <<"] = ";
		int temp;
		std::cin>> temp;
		poly.push_back(temp);
	}

	std::cout<<"\n Enter the value of constant in (x-r) : ";
	std::cin>> r;
	q.push_back(poly[0]);
	for(i=1;i<=m;i++){
		q.push_back((q[i-1]*r)+poly[i]);
	}

	std::cout<<"\n Coefficients of quotient are: \n";
	for(i=0;i<m;i++){
		std::cout<<"\t"<< q[i];
	}
	std::cout<<"\nRemainder is: "<< q[m];

	getch();
}

also its not very clean so dont use this as a guide to writing good c++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Cpp1.cpp
c:\users\plsprt\desktop\craps\cpp1.cpp(6) : error C2065: 'vector' : undeclared identifier
c:\users\plsprt\desktop\craps\cpp1.cpp(6) : error C2062: type 'int' unexpected
c:\users\plsprt\desktop\craps\cpp1.cpp(16) : error C2065: 'poly' : undeclared identifier
c:\users\plsprt\desktop\craps\cpp1.cpp(16) : error C2228: left of '.push_back' must have class/struct/union type
c:\users\plsprt\desktop\craps\cpp1.cpp(21) : error C2065: 'q' : undeclared identifier
c:\users\plsprt\desktop\craps\cpp1.cpp(21) : error C2228: left of '.push_back' must have class/struct/union type
c:\users\plsprt\desktop\craps\cpp1.cpp(21) : error C2109: subscript requires array or pointer type
c:\users\plsprt\desktop\craps\cpp1.cpp(23) : error C2228: left of '.push_back' must have class/struct/union type
c:\users\plsprt\desktop\craps\cpp1.cpp(23) : error C2109: subscript requires array or pointer type
c:\users\plsprt\desktop\craps\cpp1.cpp(23) : error C2109: subscript requires array or pointer type
c:\users\plsprt\desktop\craps\cpp1.cpp(28) : error C2109: subscript requires array or pointer type
c:\users\plsprt\desktop\craps\cpp1.cpp(30) : error C2109: subscript requires array or pointer type
c:\users\plsprt\desktop\craps\cpp1.cpp(32) : error C2065: 'getch' : undeclared identifier
c:\users\plsprt\desktop\craps\cpp1.cpp(33) : warning C4508: 'main' : function should return a value; 'void' return type assumed
Error executing cl.exe.

Cpp1.obj - 13 error(s), 1 warning(s)



ok it gave me tons of error
sorry my bad... make it std::vector<int>
also, dont use void main, even in c
if possible can @little bobby's code can function without using vector since im still not very fond in using it atm..
you can use an array instead of a vector, you'll need to understand the difference though and how to manipulate it.
@pepstein
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;

main(){
               
                int poly[100], m, r, i, q[100];
               cout<<"\t\tSYNTHETIC DIVISION";
                cout<<"\n Enter the highest degree of the equation (max 5):";
                cin>>m;

                for(i=0;i<=m;i++){
                                cout<<"\n Coefficient x["<<m-i<<"] = ";
                                cin>>poly[i];
                }

                cout<<"\n Enter the value of constant in (x-r) : ";
                cin>>r;
                q[0] = poly[0];
                for(i=1;i<=m;i++){
                                q[i] = (q[i-1]*r)+poly[i];
                }

                cout<<"\n Coefficients of quotient are: \n";
                for(i=0;i<m;i++){
                                cout<<"\t"<<q[i];
                }
                cout<<"\n Remainder is: "<<q[m];
getch();
return 0;

}

EDIT: GOT IT TO WORKED PROLERPLY JUST NEED SOME CLEANING ^^ thanks all
Last edited on
The first loop does not store the temp anywhere.

The second loop values from q and poly, even though those values are uninitialized, undefined, unknown. The result of the equation is not stored anywhere.

Lines 32 and 34: q still has no values.
Topic archived. No new replies allowed.