从文件读取到行尾

时间:2014-11-20 22:37:41

标签: c++ input line

我正在阅读文件中的输入,确定成绩,打印成绩,ID和4名学生的正确答案。我遇到的问题是答案结尾处第四行有空格,这意味着学生没有回答这些问题。当我在inFile中加载并加载到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;
    }

0 个答案:

没有答案