Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.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++ 从txt文件中分离并行数组_C++_Arrays_File Io - Fatal编程技术网

C++ 从txt文件中分离并行数组

C++ 从txt文件中分离并行数组,c++,arrays,file-io,C++,Arrays,File Io,我是一个非常新手的程序员,我正在尝试制作一个程序来读取一个txt文件,其中包含5个学生的名字(仅限于名字)以及每个学生的四个考试分数。我试图将这些名字读入一个名为students的数组,然后将分数读入名为test1、test2、test3、test4的4个独立数组,然后在监视器上显示。该文件如下所示: 史蒂夫78 65 82 73 肖恩87907982 安妮92908996 卡罗尔72 65 60 凯西34504520 我在分解阵列并组织它们方面经历了一段非常艰难的时间。有人能帮我吗?请记住我是

我是一个非常新手的程序员,我正在尝试制作一个程序来读取一个txt文件,其中包含5个学生的名字(仅限于名字)以及每个学生的四个考试分数。我试图将这些名字读入一个名为students的数组,然后将分数读入名为test1、test2、test3、test4的4个独立数组,然后在监视器上显示。该文件如下所示:

史蒂夫78 65 82 73

肖恩87907982

安妮92908996

卡罗尔72 65 60

凯西34504520

我在分解阵列并组织它们方面经历了一段非常艰难的时间。有人能帮我吗?请记住我是个新手,所以我对编程了解不多

这是我迄今为止的代码:

#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <ctime>
#define over2 "\t\t"
#define over3 "\t\t\t"
#define over4 "\t\t\t\t"
#define down5 "\n\n\n\n\n"


using namespace std;



int main(int argc, char *argv[])
{

    
    ifstream inputFile;
    
    
    //Constant for max string size
    const int SIZE = 5;
     

    //Constant for the scores
    string names[SIZE], fileName, line, test1[SIZE], test2[SIZE], test3[SIZE], test4[SIZE];


    //input section, user enters their file name
    cout << down5 << down5 << over2 << "Please enter your file name: ";
    cin >> fileName;
    system("CLS");
    

    

    //open the file containing the responses
    inputFile.open(fileName.c_str());
     cout << endl;

    //kicks you out if file isn't found
    if (inputFile)
    {
        for(int i = 0; i < SIZE; i++)
        {  
           getline(inputFile, line);
            names[i] = line;
            getline(inputFile, line);
            test1[i] = line;
            getline(inputFile, line);
            test2[i] = line;
            getline(inputFile, line);
            test3[i] = line;
            getline(inputFile, line);
            test4[i] = line;
        }   
            inputFile.close();  
    }
    cout << down5 << over3 << "Student\tTest1\tTest2\tTest3\tTest4\n";
    cout << over3 << "-------\t-----\t-----\t-----\t-----\n";   
    for(int i = 0; i < SIZE; i++)
    {
        cout << over3 << names[i] << endl;
        cout << over3 << test1[i] << endl;
        cout << over3 << test2[i] << endl;
        cout << over3 << test3[i] << endl;
        cout << over3 << test4[i] << endl;
        
    }
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#定义超过2“\t\t”
#定义超过3“\t\t\t”
#定义超过4“\t\t\t\t”
#定义down5“\n\n\n\n”
使用名称空间std;
int main(int argc,char*argv[])
{
ifstream输入文件;
//最大字符串大小的常量
常数int SIZE=5;
//分数不变
字符串名称[大小]、文件名、行、test1[大小]、test2[大小]、test3[大小]、test4[大小];
//输入部分,用户输入其文件名

cout让我们看看您试图读取的文件的结构:

Steve 78 65 82 73
Shawn 87 90 79 82
Annie 92 90 89 96
Carol 72 65 65 60
Kathy 34 50 45 20
数据的格式可以描述如下:

  • 每行代表一条“记录”
  • 每个“记录”包含多个列
  • 列之间用空格分隔
您当前正在为每个列使用
getline()

for(int i = 0; i < SIZE; i++)
{  
  getline(inputFile, line);
  names[i] = line;
  getline(inputFile, line);
  test1[i] = line;
  getline(inputFile, line);
  test2[i] = line;
  getline(inputFile, line);
  test3[i] = line;
  getline(inputFile, line);
  test4[i] = line;
}   
请注意,
get\u column()
函数处理多个空格或太短的行,因此此文件:

Steve 78     65 82 73
Shawn   87 90 
Annie 92 90 89 96
Carol 72 
Kathy 34 50   45 20
…生成以下输出:

Please enter your file name: textfile.txt
Student Test1 Test2 Test3 Test4
------- ----- ----- ----- -----
Steve   78    65    82    73
Shawn   87    90    79    82
Annie   92    90    89    96
Carol   72    65    65    60
Kathy   34    50    45    20
Please enter your file name: textfile.txt
Student Test1 Test2 Test3 Test4
------- ----- ----- ----- -----
Steve   78    65    82    73
Shawn   87    90
Annie   92    90    89    96
Carol   72
Kathy   34    50    45    20

前面的答案是对问题的过度思考

您可以使用输入文件流通过>>运算符检索“格式化输入”(即,您知道的输入格式为“字符串”,然后是“int”、“int”、“int”、“int”)

string name;

int score[4];

ifstream mf;

mf.open("score.txt");

// Get name string.
mf >> name;

// Get four score values into an array.
mf >> score[0] >> score[1] >> score[2] >> score[3];
然后:

cout << name;

cout << score[0];
cout << score[1];
cout << score[2];
cout << score[3];

cout当我运行这个函数时,它只输出随机数…为什么会这样?另外,我是一个非常新手,所以你的get\u column函数对我来说完全没有意义。:/The
get\u column()
函数接受一个字符串和一个“起点”,并从第一组(非空白)创建一个新字符串在“开始”点之后找到的字符。如果看到奇怪的结果,请确保设置了“开始”在第一次调用之前,先将位置设置为
0
;然后通过
pos+=len
行更新起始位置。要了解其工作原理,请阅读函数和方法。此外,如果我想计算每个分数的平均值并显示它,最简单的方法是什么?使用或转换str将数据分成数:
double average=(atof(test1[i])+atof(test2[i])+atof(test3[i])+atof(test4[i])/4;
注意这里使用的是
double
而不是
int
,整数除法在某些情况下可能会导致奇数结果。例如,比较
3/code>(integer)与
3.0/2.0
(浮点)。我将在哪里将其插入代码?如何声明它?
string name;

int score[4];

ifstream mf;

mf.open("score.txt");

// Get name string.
mf >> name;

// Get four score values into an array.
mf >> score[0] >> score[1] >> score[2] >> score[3];
cout << name;

cout << score[0];
cout << score[1];
cout << score[2];
cout << score[3];