Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将几种类型的数据读入数组_C++ - Fatal编程技术网

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

C++ 将几种类型的数据读入数组,c++,C++,我很难弄清楚将文本文件读入程序的过程。在get_answer_key_数组中,我试图读取文本文件的顶行并将其放入数组中 文本文件如下所示: abdbcbdbbabbcbcbcb 鲍勃·鲍比·阿德卡 在本程序中测试的每个文本文件的第一行都有答案键。第一行之后的每一行都会有人的名字、空格、姓氏、空格、等级和缺少的等级,当我到达时,会用“-”替换 我目前正在获取第一行并将其放入应答键数组中。 我知道,一旦我完成了第一行,我就可以把人的名字、姓氏和答案放入三个独立的平行数组中。我很难想出正确的方法来检查

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

文本文件如下所示:

abdbcbdbbabbcbcbcb

鲍勃·鲍比·阿德卡

在本程序中测试的每个文本文件的第一行都有答案键。第一行之后的每一行都会有人的名字、空格、姓氏、空格、等级和缺少的等级,当我到达时,会用“-”替换

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

好的,现在我已经更改了我的get\u answer\u key\u数组,以获得我需要的所有数组。目前,我正在尝试将顶行(应答键)放入应答键[]的第一个数组中。我已经尝试过实现getline函数,但是我正在尝试找出如何只获取最上面的一行。是否可以让我的eof()循环在第一行停止,以便将第一行的数据传输到数组中?也是我的 应答键[i]=键;我敢打赌,需要换成别的东西

我还应该提到,一旦我弄清楚如何将顶行放入数组,我希望使用此过程通过以下工作流将其余数据(名称和答案)放入各自的单独数组中:

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_input_文件(ifstream&in_stream)//从用户处获取文本文件名
void get_数组(ifstream&in_stream,int-answer_键[],字符串名[],字符串姓[],int-answers[],int&count)//将文本文件中的数据放入所有并行数组
//无效分数评分器(整数和目标、字符串名[]、字符串姓[]、整数答案[]、整数和计数、整数和分数);
//无效字母等级(整数和分数、整数和字母等级);
//作废学生报告(int&target,字符串first_name[],字符串last_name[],int answers[]);
int main()
{
ifstream in_stream;
int answer_key[30],count=0,score=0;//初始化最多30个答案的答案键数组
字符串first_name[20];//初始化first name数组
字符串last_name[20];//初始化last name数组
int answers[30];//初始化answers数组

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

void get_answer_key_array(ifstream &in_stream, char *answer_key, int &count) {

     in_stream >> answer_key;
     count = strlen(answer_key);
}
应答键数组必须是char类型


如果您需要读取一行数据,那么应该使用
getline
在\u-stream>>应答键[i]中读取
尝试从文件中读取数字。这真的是故意的吗?
getline
用于获取整行内容。之后,您可以迭代它以提取答案键的所有字符。不是真正的主题,但您是否被迫使用文本文件?使用某种结构化格式(例如:JSON、XML、CSV等)可能会让你的生活更轻松是的,它必须是一个文本文件。我现在正在查看getline!谢谢你,我会在找到它后更新。“答案键数组必须是char类型。”我宁愿建议使用
std::string
。我在评论之前更改了这部分的结构,希望是最好的?