C++ 从文件中读取,直到行结束

C++ 从文件中读取,直到行结束,c++,input,line,C++,Input,Line,我在阅读文件输入,确定成绩,打印成绩,ID和4名学生的正确答案。我遇到的问题是,答案后面的第四行有空格,这意味着学生没有回答这些问题。当我填充并加载到一个由20个字符组成的数组中时,它将从下一行中提取接下来的2个字符,这是学生ID的第一个数字,我不希望发生这种情况。我想知道如何在输入文件的每一行停止输入。第一行是正确答案 TTFTFTTTFTFTFFTTFTTF 54102 T FTFTFTTTFTTFTTF TF 56278 TTFTFTTTFTFTFFTTFTTF 42366 TTF

我在阅读文件输入,确定成绩,打印成绩,ID和4名学生的正确答案。我遇到的问题是,答案后面的第四行有空格,这意味着学生没有回答这些问题。当我填充并加载到一个由20个字符组成的数组中时,它将从下一行中提取接下来的2个字符,这是学生ID的第一个数字,我不希望发生这种情况。我想知道如何在输入文件的每一行停止输入。第一行是正确答案

 TTFTFTTTFTFTFFTTFTTF
 54102 T FTFTFTTTFTTFTTF TF
 56278 TTFTFTTTFTFTFFTTFTTF
 42366 TTFTFTTTFTFTFFTTF/
 42586 TTTTFTTT TFTFFFTF




    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    #include<cmath>
    using namespace std;
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */

    int main(int argc, char** argv) {
    char answers[20];
    double points = 0;

    struct student1{

        char student[20];
        int id;
        char grade;
        char correct[20];
    };

    struct student2{

        char student[20];
        int id;
        char grade;
        char correct[20];

    };
    struct student3{

        char student[20];
        int id;
        char grade;
        char correct[20];

    };

    struct student4{

        char student[20];
        int id;
        char grade;
        char correct[20];

    };


    student1 s1;
    student2 s2;
    student3 s3;
    student4 s4;


    ifstream inFile;
    inFile.open("Project9_Input.txt");
    if(!inFile)
        {
           cout << "unable to open input file. "<< endl;
           return 1;
        }



    for(int i=0;i<20;i++)
    {

        inFile>>answers[i];
    }


    inFile>>s1.id;


    char u;
    inFile>>noskipws>>u;

    for(int i=0;i<20;i++)
    {

        inFile>>noskipws>>s1.student[i];
        if(cin.peek()== '\n')
        {
            break;
        }

    }

    inFile>>noskipws>>u;
    inFile>>s2.id;
    inFile>>noskipws>>u;

    for(int i=0;i<20;i++)
    {

        inFile>>noskipws>>s2.student[i];
    }



    inFile>>noskipws>>u;
    inFile>>s3.id;
    inFile>>noskipws>>u;

    for(int i=0;i<20;i++)
    {

        inFile>>noskipws>>s3.student[i];

    }


    inFile>>noskipws>>u;
    inFile>>s4.id;
    inFile>>noskipws>>u;
    cout<<s4.id<<endl;


    for(int i=0;i<20;i++)
    {

        inFile>>noskipws>>s4.student[i];

    }





    for(int i=0;i<20;i++)
    {



        if(answers[i]==s1.student[i])
        {
            points=points+2;
            s1.correct[i] = answers[i];
        }
        else
            if(s1.student[i]=' ')
        {
            points=points+1;      //undo previous if condition to negate the -1.
        }
        else
        if(answers[i]!=s1.student[i])
        {
            points=points-1;

        }


    }

    double percent = 0;
    percent = points/40;

    if(percent>=0.9 && percent<=1.0)
    {
        s1.grade = 'A';
    }

    if(percent>=0.8 && percent<0.9)
    {

        s1.grade = 'B';
    }
    if(percent>=0.7 && percent<0.8)
    {
        s1.grade = 'C';

    }
    if(percent>=0.6 && percent<0.7)
    {
        s1.grade = 'D';

    }
    if(percent<0.6)
    {
        s1.grade = 'F';
    }


    for(int i=0;i<20;i++)
    {
        if((s1.correct[i]!= 'T') && (s1.correct[i]!= 'F') )
        {
            s1.correct[i] = ' ';
        }
    }

    cout<<s1.id<<endl;



    for(int i=0;i<20;i++)
    {

        cout<<s1.correct[i];
    }
    cout<<endl;
    cout<<s1.grade<<endl;
    cout<<endl;




    points = 0;
    for(int i=0;i<20;i++)
    {



        if(answers[i]==s2.student[i])
        {
            points=points+2;
            s2.correct[i] = answers[i];
        }
        else
            if(s2.student[i]=' ')
        {
            points=points+1;      //undo previous if condition to negate the -1.
        }
        else
        if(answers[i]!=s2.student[i])
        {
            points=points-1;

        }


    }


    percent = points/40;

    if(percent>=0.9 && percent<=1.0)
    {
        s2.grade = 'A';
    }

    if(percent>=0.8 && percent<0.9)
    {

        s2.grade = 'B';
    }
    if(percent>=0.7 && percent<0.8)
    {
        s2.grade = 'C';

    }
    if(percent>=0.6 && percent<0.7)
    {
        s2.grade = 'D';

    }
    if(percent<0.6)
    {
        s2.grade = 'F';
    }


    for(int i=0;i<20;i++)
    {
        if((s2.correct[i]!= 'T') && (s2.correct[i]!= 'F') )
        {
            s2.correct[i] = ' ';
        }
    }

    cout<<s2.id<<endl;



    for(int i=0;i<20;i++)
    {

        cout<<s2.correct[i];
    }
    cout<<endl;
    cout<<s2.grade<<endl;
    cout<<endl;






    points = 0;
    for(int i=0;i<20;i++)
    {



        if(answers[i]==s3.student[i])
        {
            points=points+2;
            s3.correct[i] = answers[i];
        }
        else
            if(s3.student[i]=' ')
        {
            points=points+1;      //undo previous if condition to negate the -1.
        }
        else
        if(answers[i]!=s3.student[i])
        {
            points=points-1;

        }


    }


    percent = points/40;

    if(percent>=0.9 && percent<=1.0)
    {
        s3.grade = 'A';
    }

    if(percent>=0.8 && percent<0.9)
    {

        s3.grade = 'B';
    }
    if(percent>=0.7 && percent<0.8)
    {
        s3.grade = 'C';

    }
    if(percent>=0.6 && percent<0.7)
    {
        s3.grade = 'D';

    }
    if(percent<0.6)
    {
        s3.grade = 'F';
    }


    for(int i=0;i<20;i++)
    {
        if((s3.correct[i]!= 'T') && (s3.correct[i]!= 'F') )
        {
            s3.correct[i] = ' ';
        }
    }

    cout<<s3.id<<endl;



    for(int i=0;i<20;i++)
    {

        cout<<s3.correct[i];
    }
    cout<<endl;
    cout<<s3.grade<<endl;
    cout<<endl;



    points = 0;
    for(int i=0;i<20;i++)
    {



        if(answers[i]==s4.student[i])
        {
            points=points+2;
            s4.correct[i] = answers[i];
        }
        else
            if(s4.student[i]=' ')
        {
            points=points+1;      //undo previous if condition to negate the -1.
        }
        else
        if(answers[i]!=s4.student[i])
        {
            points=points-1;

        }


    }


    percent = points/40;

    if(percent>=0.9 && percent<=1.0)
    {
        s4.grade = 'A';
    }

    if(percent>=0.8 && percent<0.9)
    {

        s4.grade = 'B';
    }
    if(percent>=0.7 && percent<0.8)
    {
        s4.grade = 'C';

    }
    if(percent>=0.6 && percent<0.7)
    {
        s4.grade = 'D';

    }
    if(percent<0.6)
    {
        s4.grade = 'F';
    }


    for(int i=0;i<20;i++)
    {
        if((s4.correct[i]!= 'T') && (s4.correct[i]!= 'F') )
        {
            s4.correct[i] = ' ';
        }
    }

    cout<<s4.id<<endl;



    for(int i=0;i<20;i++)
    {

        cout<<s4.correct[i];
    }
    cout<<endl;
    cout<<s4.grade;


    return 0;
    }
ttftftftftftftftftf
54102吨英尺英尺英尺英尺英尺英尺英尺英尺英尺英尺英尺英尺英尺英尺英尺英尺英尺
56278 TTFTFTTTFTFFTTFTTF
42366 TTFTFTTTFTFFTTF/
42586 TTTTFTTTTFFFFTF
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
/*使用控制台暂停器运行此程序,或添加您自己的getch、system(“暂停”)或输入循环*/
int main(int argc,字符**argv){
char回答[20];
双点=0;
结构学生1{
char学生[20];
int-id;
煤焦品位;
字符正确[20];
};
结构学生2{
char学生[20];
int-id;
煤焦品位;
字符正确[20];
};
结构学生3{
char学生[20];
int-id;
煤焦品位;
字符正确[20];
};
结构学生4{
char学生[20];
int-id;
煤焦品位;
字符正确[20];
};
学生1 s1;
学生2 s2;
学生3 s3;
学生4名;
河流充填;
infle.open(“Project9_Input.txt”);
如果(!infle)
{
cout>s1.id;
char u;
填充>>noskipws>>u;
对于(int i=0;i>noskipws>>s1.student[i];
如果(cin.peek()='\n')
{
打破
}
}
填充>>noskipws>>u;
infle>>s2.id;
填充>>noskipws>>u;
对于(int i=0;i>noskipws>>s2.student[i];
}
填充>>noskipws>>u;
infle>>s3.id;
填充>>noskipws>>u;
对于(int i=0;i>noskipws>>s3.student[i];
}
填充>>noskipws>>u;
infle>>s4.id;
填充>>noskipws>>u;

你为什么要创建四个相同的结构定义呢?
student s[4]有什么问题吗
或者更好的
std::vector
这样可以有任意数量的学生。如果学生的名字超过19个字符怎么办?使用
std::string
来支持任何名字长度。什么是
correct
意思?使用更好的变量名并用注释记录它们。如果一天有超过20个问题怎么办至少使用一个命名常量来表示问题的数量,这样你就不必在代码中替换大量的20个。
std::vector
更好,这样你就可以有任意数量的问题了。idk什么向量是我的新手。我只想知道如何在行的末尾停下来,然后加载到下一个学生信息
std::string行;而(std::getline(infle,line)){..}
将一次解析文件中的每一行。这是唯一的方法吗?否,但这是最简单的方法。如果使用
>
一次读取一个字符,它将自动跳过空白。