기본적으로 간단한 수학 프로그램 인 프로그램에 문제가 있습니다. 몇 가지 벡터 곱셈과 벡터 행렬 곱셈을 수행하고 텍스트에서 데이터를 읽으 려 할 때까지 제대로 작동합니다. 프로그램을 컴파일 할 수는 있지만 실행하려고하면 "Dataname.exe가 작동하지 않습니다"라는 오류가 발생합니다. 여기 내 코드가 있습니다. 수족을 고려하지 마십시오ifstream 사용 방법
#include<iostream>
#include<cmath>
#include<vector>
#include"header.hpp"
#include<fstream>
using namespace std;
int main()
{
ifstream einlesen ("Zahlen.dat"); //function "einlesen" opens the file "Zahlen.dat".
if (einlesen) // Message if the file has been opend
cout<<"Daten wurden eingelesen"<<endl;
else {
cout<<"Konnte Daten nicht einlesen"<<endl;
return 99;
}
double a;
int n{0};
while (einlesen >>a>>a>>a>>a>>a>>a>>a) n++;//Loop copys datas on a
einlesen.clear();//stops the loop after the last data is copied.
einlesen.seekg(0, ios_base::beg);//??
vector<double> vecein(n), vecein1(n),Matrixein(n);
for (a = 0;a<n;a++);//copys datas to a vector further calculations
{
einlesen>>vecein.at(a);
einlesen>>vecein1.at(a);
einlesen>>Matrixein.at(a);
}
double Matrix1[9]; //defining my Matrix and the coordinates of my vectors.
double x,y,z,x_1,x_2,x_3;
vector<double> vec(3);
vector<double> vec1(3);
// Old code where I read the data with `cin`:
/*cout<<"Geben Sie die x Koordinaten ein:"<<endl;
cin>>vec[0];
cin>>vec1[0];
cout<<"Geben Sie die y Koordinaten ein:"<<endl;
cin>>vec[1];
cin>>vec1[1];
cout<<"Geben Sie die z Koordinaten ein:"<<endl;
cin>>vec[2];
cin>>vec1[2];
cout<<"Geben Sie eine Matrix ein"<<endl;
cin>>Matrix1[0]>>Matrix1[1]>>Matrix1[2]>>Matrix1[3]>>Matrix1[4]>>Matrix1[5]>>Matrix1[6]>>Matrix1[7]>>Matrix1[8];
cout<<"Vektor1:<"<<vec[0]<<","<<vec[1]<<","<<vec[2]<<">"<<endl;
cout<<"Vektor2:<"<<vec1[0]<<","<<vec1[1]<<","<<vec1[2]<<">"<<endl;
vector<double> Addition(3);
Addition = Vektoraddition(vec,vec1);
cout<<"Addition:"<<"<"<<Addition[0]<<","<<Addition[1]<<","<<Addition[2]<<">"<<endl;
double Skalarprodukt;
Skalarprodukt = Skalarpr(vec,vec1);
cout<<"Skalarprodukt:"<<Skalarprodukt<<endl;
vector<double> kreuzprodukt(3);
kreuzprodukt = Kreuzprodukt (vec,vec1);
cout<<"Kreuzprodukt:"<<"<"<<kreuzprodukt[0]<<","<<kreuzprodukt[1]<<","<<kreuzprodukt[2]<<">"<<endl;
vector<double> MatrixVektor(3);
MatrixVektor = Matrix_vektor (Matrix1, vec);
cout<<"Matrix*Vektor:"<<"<"<<MatrixVektor[0]<<","<<MatrixVektor[1]<<","<<MatrixVektor[2]<<">"<<endl;*/
ofstream ausgabe ("Ausgabe.dat");//write the data on Ausgabe.dat
for (int i = 0; i < a; i++)
{
ausgabe << "(" << vecein[i] << "," << vecein1[i] << "," << Matrixein[i]<<")" << endl;
}
return 0;
}
나는 변수 이름에 대해 궁금하지 않습니다. 내 문제는 내가 while 루프에서 무엇을하는지 이해하지 못한다는 것이다. Zahlen.dat는 1 2 3 4 5 6 7과 유사한 파일이며 Ausgabe.dat는 빈 파일입니다. 사실 Ausgabe.dat에 내용이 변경되었다고 알려주기 때문에 쓰기는하지만 파일을 새로 고침하면 여전히 비어 있습니다.
들여 쓰기를 시도하고 코드를 더 읽기 쉽게 포맷 해 주실 수 있습니까? 소스 코드에 표시되는 것과 동일한 언어로 오류 메시지를보고하면 오류를 던지고있는 행을 알 수 있습니다. – Davislor
입력 파일이 열리지 않고 그 이유를 알 수없는 경우 한 가지 방법은 프로그램 상단에'#include'과'#include '을 쓰고 오류 메시지는'cerr << __FILE__ << ':'<< __LINE__ << "": "<< strerror (errno) << endl; 대부분의 프로그램에는 비슷한 것을하는 매크로가 있습니다. –
Davislor