c++ - 我的以下代码有错误

标签 c++ compiler-errors

我的以下代码有错误,我不确定自己在做什么错。我的编译器是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;
}

最佳答案

您应该说什么是编译器错误。我在Visual Studio中尝试过,事实证明您需要

#include <string>

在文件的顶部。

关于c++ - 我的以下代码有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22624234/

相关文章:

c++ - 在定义一个充满成员的结构时,是否为该结构类型的每个变量创建了这些成员?

c++ - CAIRO_OPERATOR_CLEAR 未按预期工作

c++ - 模板类型推导

java - 此 token 错误后将引发

c++ - 垃圾收集的概念与非 OOP 语言有何关系

c++ - 链表 C++ 删除节点

java - 奇怪的编译问题

c++ - 您选择的 CPU 不支持 x86-64 指令集

c# - C#泛型: 'x' is a 'type' but used like a 'variable'

c++ - 使元组包含unique_ptr时出错