【问题标题】:Create an array with external file in C++在 C++ 中使用外部文件创建数组
【发布时间】:2013-08-08 07:23:57
【问题描述】:

我有 4 天的 C++ 培训,请耐心等待。

评估多项选择题需要两个数据文件。第一个文件 (booklet.dat) 包含正确答案。问题总数为 50。示例 文件如下:

ACBAADDBCBDDAACDBACCABDCABCCBDDABCACABABABCBDBAABD

第二个文件 (answer.dat) 包含学生的答案。每行有一个学生 包含以下信息的记录:

学生的答案(共 50 个答案)格式同上(* 表示没有答案),后跟学生 ID 和学生姓名。示例:

AACCBDBC*DBCBDAAABDBCBDBAA*BCBDD*BABDBCDAABDCBDBDA 6555 MAHMUT
CBBDBC*BDBDBDBABABABBBBBABBABBBBD*BBBCBBDBABBBDC** 6448 SINAN
ACB*ADDBCBDDAACDBACCABDCABCCBDDABCACABABABCBDBAABD 6559 CAGIL

我有一个家庭作业,要编写一个 C++ 程序,该程序计算每个学生正确答案的总数,并将此信息输出到另一个名为 report.dat 的文件中。在此文件中,必须提供学生的 ID、姓名和分数。每个正确答案得 1 分。对于上面给出的示例文件,输出应该如下:

6555 MAHMUT 10
6448 SINAN 12
6550 CAGIL 49 

这是我目前所拥有的:

include <iostream>
include <fstream>

using namespace std;

int main()
{
    char booklet[50] answers[50]
    int counter

    // Link answers with booklet.dat
    booklet = ifstream
    input_file("booklet.dat");
    return 0;

    // Link answers with answers.dat
    answers = ifstream
    input_file("answer.dat");
    return 0;


    while (booklet==answers)
    {
        counter++
        cout << "The student had">>counter>> "answers right";
    }
}

我什至不确定我的方向是否正确。我知道我需要从文件 booklet.dat 中创建一个数组,并从文件 answer.dat 中创建另一个数组。然后必须进行比较,并计算两者之间的匹配。

我不希望任何人为我完成任务,我只需要朝着正确的方向轻推。

【问题讨论】:

  • booklet == answers 将始终为假。打开你的警告(虽然这首先是相当不可编译的)。
  • 您已经列举了解决问题所需采取的步骤,所以我认为您已经在正确的方向上。请注意一般 C++ 知识(不要在没有充分理由的情况下将 return 放在函数中间,如何比较数组...)
  • 四天后,您会希望在行尾添加分号。
  • 哈哈是的对不起,我一直想念那些!

标签: c++ arrays if-statement count


【解决方案1】:

1.) 在您的语法上:

a) C++ 中的每一行都必须以“;”结尾。您的示例中有一些行没有。 (通常你的编译应该指向这一行或以下行并出现错误)

b) 多个变量定义在两个不同的变量之间需要一个“,”。

2.) 我建议你使用类似的东西: (看看C++ Reference fstream) 编辑:只是一个小大纲,这种形式并不完整,只是为了给你和想法;-)

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{

int nr_of_students = 1000;   /* Or any number you'd like to analyze */

int stud_nr[nr_of_students];
string stud_name[nr_of_students];
int stud_count[nr_of_students];

fstream in_out;
in_out.open("filename.dat",fstream::in); // fstream::in for reading from file
                                         // fstream::out for writing to this file
if(in_out.is_open())
{
    for(lines=0;(in_out>>answers && lines<nr_of_students);lines++)
    {
         in_out >> stud_nr[lines];   /* EDIT: sorry hat some index confusions here... */
         in_out >> stud_name[lines];
         stud_count[lines]=0;
         for(int i=0;i<50;i++)
         {
              /* comparison between the booklet_array and the answers_array */
              /* Count up the stud_count[lines] for each right comparison */
         }
    }

    /* some simmilar code for the output-file */
}
else cout << "Error reading " << "filename.dat" << endl;

return 1;
}

3.) 您的代码还可以通过向量获得更高的性能。 一个好的教程是:Tutorial part I 然后你会在 cmets 中找到第 2 部分

4.) 您可以使用 argc 和 argv** 实现更动态的代码,只需 google 即可

我希望这些 cmets 可以帮助你继续前进;)

【讨论】:

  • 非常感谢所有的输入,我现在可以继续。我一直忘记分号,但编译器让我想起了那些!
  • 如果我的回答对您有帮助,能否请您标记我的回答和您要解决的问题? :)
【解决方案2】:

你已经在正确的方向。基本上,您想将答案键加载到数组中以进行快速比较,然后您需要检查每个学生的答案,每次他们得到正确答案时,您都会增加一个计数器并为每个学生写下 ID、姓名和分数。您的代码存在问题,例如缺少分号。 另请注意,返回会退出函数,并且在无条件返回后不会执行任何语句,从 main 返回会终止您的程序。

打开文件进行读取的正常方法是:

#include<fstream>
#include<string>
int main()
{
   std::ifstream input_file("inputfilename");
   // since the answer key is one line
   // and each students answer , id and name are also one line 
   // getting that line using std::getline() would be sufficient
   std::string line;
   std::getline(input_file, line);
   // line would now contain the entire first line except the newline character

   std::getline(input_file, line);
   //now line would now contain the second line in the file
   return 0;
}

写入文件类似于我们使用ofstream打开文件进行写入。

像这样:

#include<fstream>

int main()
{
   std::ofstream output_file("outputfilename");
   // lets say we have a string and an int that we want to write
   std::string line_to_write("Hello File");
   int number = 42;
   output_file << line_to_write << number; // writes the string and then 42 on the same line
   output_file << '\n'; // writes the newline character so that next writes would appear on another line
   return 0;
}

对于标准库和 C++ 的一般参考,当您需要知道可用的函数来做某事时,我推荐 cppreference 这里是 ifstreamofstream 上的特定页面。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 2013-07-02
    相关资源
    最近更新 更多