2014-03-25 34 views
0

我有以下代码的错误,我不知道我做错了什么。我的编译器是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; 
} 
+0

'strerror(errno)'是你的朋友;当文件操作或其他系统调用失败时总是打印它。它可能不会澄清这种情况,但它会对其他人。 – zwol

+0

另外,你说你有MSVC,但你正在尝试运行GCC。你也有吗? – zwol

+0

另外,请注意某人已经为您重新设置了您的代码格式,并在将来首先编写这样的代码。当你实际阅读代码时,发现错误远远更容易。 – zwol

回答

0

你应该说你的编译器错误是什么。我在Visual Studio中试过它,结果你需要

#include <string> 

在你的文件的顶部。

+0

非常感谢!另一个问题,如果你能帮助我。当我运行代码时,它会提示我输入sourcefile,inputfile和resultfile。当我输入正确的信息时,我收到以下消息:“'gcc不被识别为内部或外部命令,可操作程序或批处理文件。” – user3453745

0

system用命令参数调用主机环境的命令处理器,它在linux和windows上有不同的行为。 假设你们所有的输入都是有效的。

  • 在当你运行你的程序的Linux,它会调用通过 system功能,通常作为默认的Linux中安装GCC“GCC”的命令,所以它 运行正常。
  • 在Windows上,当你运行你的程序,它也将通过命令工具称之为“海湾合作委员会”的命令 ,让你有问题,如:“GCC不 识别为一个内部或外部命令......因为你不你的windows没有安装gcc。

Visual C++提供了命令行工具,请参考帮助页面:http://msdn.microsoft.com/en-us/library/f35ctcxw.aspx了解详情。

简单地说,你可以repalce与 'CL' 代码 'GCC',以便它可以在Windows上编译(使用VC++)。

+0

感谢您的帮助,但是当我用Cl替换gcc时,以下错误信息:“该程序无法启动,因为您的计算机中缺少mspdb100.dll。请尝试重新安装程序来解决此问题。” – user3453745

+1

@ user3453745,嗯,无论如何,这表明在Windows上工作。对于mspdb100.dll问题,我建议你可以谷歌它。通常,原因是dll不在你的环境中,你可以改变你的“路径”环境以包含dll或任何其他方法的路径,例如在“CL”之前运行“vsvars32.bat”。 –