将几种类型的数据读入数组

Reading several types of data into arrays

本文关键字:数据 数组 类型 几种      更新时间:2023-10-16

我很难弄清楚将文本文件读取到程序中的过程。在get_answer_key_array中,我试图读取并仅将文本文件的顶行放入数组中。

文本文件看起来像这样:

ABDBCBDBABABCBABCB

Bob Bobby adcad abcbd bacb

在这个程序中测试的每个文本文件将在第一行有答案键。第一行之后的每一行都有人名,空格,姓氏,空格,成绩,当我找到它时,缺失的成绩将被"-"替换。

我目前正在努力获得第一行并将其放入答案键数组。我知道,一旦我完成了第一行,我就可以将人名、姓氏和答案放入三个独立的并行数组中。我很难想出正确的方法来检查第一行,这样我就可以得到答案键。

好了,现在我已经改变了我的get_answer_key_array来获得我需要的所有数组。目前,我正试图将顶行(答案键)放入answer_key[]的第一个数组。我试图实现getline函数,但我试图弄清楚如何只得到顶行。是否有可能保持我的eof()循环,但在第一个结束行停止,将第一行的数据传输到数组中?还我Answer_key [i] = key;我打赌需要换别的东西!

我还应该提到,一旦我弄清楚如何将顶行放入数组,我想使用这个过程通过以下工作流程将其余的数据(姓名和答案)放入它们自己的单独数组中:

in_stream >> first_name[i] >> last_name[i] >> answers[i];
while(!in_stream.eof() ) {
    i++;
    in_stream >> first_name[i] >> last_name[i] >> answers[i];
}
in_stream.close();

程序开始

void get_input_file(ifstream &in_stream);  //gets the text file name from the user
void get_arrays(ifstream &in_stream, int answer_key[], string first_name[], string last_name[], int answers[], int &count);  //brings the data from text file into all of the parallel arrays
//void score_grader(int &target, string first_name[], string last_name[], int answers[], int &count, int &score);
//void letter_grade(int &score, int &letter_grade);
//void student_report(int &target, string first_name[], string last_name[], int answers []); 
int main()
{
    ifstream in_stream;
    int answer_key[30], count = 0, score = 0;   //initializing the answer key array up to 30 answers
    string first_name[20];     //initializing the first name array
    string last_name[20];      //initializing the last name array
    int answers[30];          //initializing the answers array
    cout << "Welcome to the Test Grader." << endl;   //welcome message
    get_input_file(in_stream);   //function call to get the file name
    get_arrays(in_stream, answer_key, first_name, last_name, answers, count);  //function call to create the arrays
}

void get_input_file(ifstream &in_stream) {
    string file_name;   //initializing the file name string
    do {
        cout << "Enter the file name you would like to import the data from: " << endl;  //asks user to input name of file
        cin >> file_name;           //user types name of file
        in_stream.open(file_name.c_str());      //program opens the stream
        if(in_stream.fail()) {
            cout << "Error finding file, try again.n";  //if failed, asks user for file name again
            continue;   //continues back to the do loop
        }
        break;
    } while(true);
    cout << "File Obtained: " << file_name << endl;  //alerts user of file success with printed name

}
void get_arrays(ifstream &in_stream, int answer_key[], string first_name[], string last_name[], int answers[], 
int &count) {
    int i = 0;
    string key;  //This will be the storage variable for the first line of text file
    if (in_stream.is_open() ) {     //if the stream is open
        getline(in_stream, key);
        cout << "Testing: " << key << endl;
        while(!in_stream.eof() ) {
            i++;
            in_stream >> first_name[i] >> last_name[i] >> answers[i];
        }
    }
    cout << first_name[1] << " " << last_name[1] << " " << answers[1] << endl;
    in_stream.close();
}

你可以简单地这样读第一行

void get_answer_key_array(ifstream &in_stream, char *answer_key, int &count) {
     in_stream >> answer_key;
     count = strlen(answer_key);
}

answer_key数组必须是char类型的。

结尾字符是'n'而不是'/n'