我有以下代码的错误,我不知道我做错了什么。我的编译器是microsoft visual C++ 2010.此代码通过system()函数编译C++源文件,然后使用给定的输入文件运行生成的程序。程序然后将该程序生成的输出文件与预期的结果文件进行比较,以确定该程序是否正确。我的下面的代码是:我有以下代码错误
#include <iostream>
#include <fstream>
using namespace std;
string getfile(string);
int execute(string,string);
void checkit(ifstream&,ifstream&);
int main() {
string command;
string input,output,source,expected;
ifstream in,exp;
int code;
source=getfile("source");
input=getfile("input");
expected=getfile("expected result");
code=execute(source,input);
if(code!=0) {
cout<<"Execution error,program aborted!\n";
system("pause");
return 0;
}
in.open("output.txt"); //open file
if(in.fail()) { //is it ok?
cout<<"created output file did not openplease check it\n";
system("pause");
return 1;
}
exp.open(expected.c_str()); //open file
if(exp.fail()) { //is it ok?
cout<<"expected output file did not openplease check it\n";
system("pause");
return 1;
}
checkit(in,exp);
in.close();
exp.close();
system("pause");
return 0;
}
void checkit(ifstream& act,ifstream& exp) {
int i,error=0,j;
int n,m,total=0;
act>>n;
exp>>m;
while(act&&exp) {
total++;
if(n!=m)
error++;
act>>n;
exp>>m;
}
if(act || exp)
error++;
if(error==0)
cout<<"The output of the program iscorrect.\n";
else
cout<<"The output of theprogram is not correct.\n";
cout<<"Your grade is"<<(total-error)/(double)total*100.<<"%\n";
}
int execute(string source,string input) {
string command,minusc;
int c,pos;
pos=source.find('.',0);
minusc=source.substr(0,pos);
command="gcc -o "+minusc+" "+source;
c=system(command.c_str());
if(c!=0) {
cout<<"compilation error\n";
return c;
}
command=minusc+" "+input+" > output.txt";
c=system(command.c_str());
if(c!=0)
cout<<"execution error\n";
return c;
}
string getfile(string mess) {
string file;
cout<<"Please enter the name of the "<<mess<<"file: ";
cin>>file;
return file;
}
'strerror(errno)'是你的朋友;当文件操作或其他系统调用失败时总是打印它。它可能不会澄清这种情况,但它会对其他人。 – zwol
另外,你说你有MSVC,但你正在尝试运行GCC。你也有吗? – zwol
另外,请注意某人已经为您重新设置了您的代码格式,并在将来首先编写这样的代码。当你实际阅读代码时,发现错误远远更容易。 – zwol