I am having problems with figure out the problems with MATRIX program...i am havin few errors that i cant fix in the main function where : print_matrix
This is where i am gettin errors my compiler tells me error conversion from std::ofstream to non-scalar type 'MATRIX' requested
also in line 131 in the void print_matrix error 'matrix' was not declered in this scope
and I am always gettin this errors cause I am using geany {linux} compiler where inside the function file_open inside the if statement 'strcpy' was not declered in this scope..
Question
mzawieska
I am having problems with figure out the problems with MATRIX program...i am havin few errors that i cant fix in the main function where : print_matrix
#include<iostream> #include<fstream> #include<string> #include<iomanip> #include<cmath> using namespace std; struct MATRIX { double a11, a12, a21, a22; }; void file_open(ofstream &Out); int file_close(ofstream &Out); void print_matrix(MATRIX m, char word[], ofstream &out); void get_matrix(MATRIX &matrix, char name[]); void get_scalar(double &k); void calc_sum(MATRIX m1, MATRIX m2, MATRIX ∑); void calc_diff(MATRIX m1, MATRIX m2, MATRIX &diff); void scalar_mult(double k, MATRIX m, MATRIX &km); void calc_prod (MATRIX m1, MATRIX m2, MATRIX ∏); void calc_inverse(MATRIX m, MATRIX &inverse); int main() { MATRIX matrix1, matrix2, sum, difference, product, sProduct,inverse, sMult; double k=4.5; char name1[20], name2[20]; ofstream outfile; file_open(outfile); get_matrix(matrix1, name1); get_matrix(matrix2, name2); get_scalar(k); calc_sum(matrix1, matrix2, sum); calc_diff(matrix1,matrix2,difference); scalar_mult (k, matrix1, sMult); calc_prod(matrix1, matrix2, product); calc_inverse(matrix1, inverse); print_matrix(matrix1, name1,outfile); print_matrix(matrix2, name2,outfile); print_matrix(outfile, "Sum =",sum); print_matrix(outfile, "Difference = ", difference); print_matrix(outfile, "Scalar Product = ", sMult); print_matrix(outfile, "Matrix Product = ", product); print_matrix(outfile, "Inverse = ", inverse); file_close(outfile); return 0; } void get_matrix(MATRIX &matrix, char name[]) { cout<<"Enter the name of the Matrix :"; cin>>ws; cin.getline(name,20); cout<<"Enter m1: "; cin>>matrix.a11; cout<<"Enter m2: "; cin>>matrix.a12; cout << "Enter m3: "; cin >> matrix.a21; cout<<"Enter m4: "; cin>>matrix.a22; } void get_scalar(double &k) { cout<<endl<<"Enter scalar value: "; cin>>k; } void calc_sum(MATRIX m1, MATRIX m2, MATRIX ∑) { sum.a11 = m1.a11 + m2.a11; sum.a12 = m1.a12 + m2.a12; sum.a21 = m1.a21 + m2.a21; sum.a22 = m1.a22 + m2.a22; } void calc_diff(MATRIX m1, MATRIX m2, MATRIX &diff) { diff.a11 = m1.a11 - m2.a11; diff.a12 = m1.a12 - m2.a12; diff.a21 = m1.a21 - m2.a21; diff.a22 = m1.a22 - m2.a22; } void scalar_mult(double k, MATRIX m, MATRIX &km) { km.a11 = m.a11 * k; km.a12 = m.a12 * k; km.a21 = m.a21 * k; km.a22 = m.a11 * k; } void calc_prod (MATRIX m1, MATRIX m2, MATRIX ∏) { prod.a11= m1.a11 * m2.a11 + m1.a12 * m2.a21; prod.a12= m1.a11 * m2.a11 + m1.a12 * m2.a21; prod.a21= m1.a11 * m2.a11 + m1.a12 * m2.a21; prod.a22= m1.a11 * m2.a11 + m1.a12 * m2.a21; } void calc_inverse(MATRIX m, MATRIX &inverse) { double det = m.a11 * m.a22 - m.a21 * m.a12; inverse.a11 = m.a22/det; inverse.a12 = -(m.a12/det); inverse.a21 = -(m.a21/det); inverse.a22 = m.a11/det; } void print_matrix(MATRIX m, char word[], ofstream &out) { out << word << " = " << matrix.a11 << " " << matrix.a12 << "\n" << " " << matrix.a21 << " " << matrix.a22 << "\n"; } void file_open(ofstream &Out) { int choice; char drive[3]; char disk_f[20]; char file[15]; cout<< "Output to console [1] to disk file [2]: "; cin >> choice; if(choice == 1) { Out.open("con"); } else { cout<< "Which drive would you want to save your data?"; cin >> drive; strcpy(disk_f, drive); strcat(disk_f, ":"); cout<< "Please enter result file name: "; cin >>file; strcat(disk_f, file); strcat(disk_f, ".dta"); Out.open(disk_f); } Out<<setiosflags(ios::showpoint|ios::fixed)<<setprecision(2); } int file_close(ofstream &Out) { Out.close(); return 0; }print_matrix(matrix1, name1,outfile);
print_matrix(matrix2, name2,outfile);
print_matrix(outfile, "Sum =",sum);
print_matrix(outfile, "Difference = ", difference);
print_matrix(outfile, "Scalar Product = ", sMult);
print_matrix(outfile, "Matrix Product = ", product);
print_matrix(outfile, "Inverse = ", inverse);
This is where i am gettin errors my compiler tells me error conversion from std::ofstream to non-scalar type 'MATRIX' requested
also in line 131 in the void print_matrix error 'matrix' was not declered in this scope
and I am always gettin this errors cause I am using geany {linux} compiler where inside the function file_open inside the if statement 'strcpy' was not declered in this scope..
Thanks for help :)
Link to comment
Share on other sites
6 answers to this question
Recommended Posts