【问题标题】:Cin loop never terminatingCin 循环永不终止
【发布时间】:2012-10-23 13:22:37
【问题描述】:

我无法让我的 cin 循环在我的程序中终止。我的程序使用 Linux 重定向从文件 hw07data 中读取输入,数据文件如下所示:

100 20 50 100 40 -1 
A34F 90 15 50 99 32 -1
N12O 80 15 34 90 22 -1

第一部分是班级总分,下一行是学生ID号,后面是他们的分数,都以-1结尾。

我的问题:当我运行命令 ./a.out 时,我的 while 循环永远不会终止,有人可以查看我的代码并给我一些提示吗?我不想要答案,因为这是作业,我只需要一些指导。谢谢!!

#include <iostream>
#include <iomanip>
using namespace std;

const int SENTINEL = -1;           //signal to end part of file
const int LTAB = 8;                //left tab
const int RTAB = 13;               //right tab

int main()
{
    cout << "Grant Mercer Assignment 7 Section 1002\n\n";
    cout << setprecision(2) << fixed << showpoint;
    cout << left << setw(LTAB) << "ID CODE" <<
                right << setw(RTAB) << "POINTS" <<
                setw(RTAB) << "PCT" << setw(RTAB) <<
                 "GRADE" << endl;
    double Percentage,              //holds students percentage
           AvgPercentage;
    int Earnedpoints,               //earned points for specific student
        Totalpoints,                //total points possible for all students
        AvgPoints,                  //average points for all students
        NumClass;                   //counts the number of students
    Totalpoints = Earnedpoints =    //set all vals equal to zero
    AvgPoints = AvgPercentage = Percentage =  NumClass = 0;
                                    //first and last char for studentID
    char Fchar,Lchar, Num1, Num2, Grade;

    int TmpVal = 0;                 //temporary value
    cin >> TmpVal;
    while(TmpVal != -1)             //reading in TOTAL POINTS
    {
        Totalpoints += TmpVal;      //add scores onto each other
        cin >> TmpVal;              //read in next value
    }

    while(cin)                       //WHILE LOOP ISSUE HERE!
    {
                                     //read in student initials
            cin >> Fchar >> Num1 >> Num2 >> Lchar >> TmpVal;
            while(TmpVal != -1)
            {
                Earnedpoints += TmpVal; //read in points earned
                cin >> TmpVal;
            }
                                        //calculate percentage
            Percentage = ((double)Earnedpoints / Totalpoints) * 100;
            AvgPercentage += Percentage;
            NumClass++;
            if(Percentage >= 90)        //determine grade for student
                Grade = 'A';
            else if(Percentage >= 80 && Percentage < 90)
                Grade = 'B';
            else if(Percentage >= 70 && Percentage < 80)
                Grade = 'C';
            else if(Percentage >= 60 && Percentage < 70)
                Grade = 'D';
            else if(Percentage < 60)
                Grade = 'F';
                                        //display information on student


            cout << left << Fchar << Num1 << Num2 << setw(LTAB) << Lchar << right << setw(RTAB-3) << Earnedpoints <<
            setw(RTAB) << Percentage <<  setw(RTAB) << Grade << endl;
            TmpVal = Earnedpoints = 0;
    }
    AvgPercentage /= NumClass;
    cout << endl << left << setw(LTAB+20) << "Class size: " << right << setw(RTAB) << NumClass << endl;
    cout << left << setw(LTAB+20) << "Total points possible: " << right << setw(RTAB) << Totalpoints << endl;
    cout << left << setw(LTAB+20) << "Average point total: " << right << setw(RTAB) << AvgPoints << endl;
    cout << left << setw(LTAB+20) << "Average percentage: " << right << setw(RTAB) << AvgPercentage << endl;
}

输出继续要求新的输入。

【问题讨论】:

  • 您希望它在收到标准输入的 EOF 时终止?见这里:stackoverflow.com/questions/3197025/…
  • 我建议使用struct 来稍微清理一下代码。
  • 我们必须严格遵守课堂作业的指导方针,我也想过使用结构,但老师会因为使用任何高级技术而让我失望。

标签: c++ linux while-loop cin io-redirection


【解决方案1】:

你可以在那里找到答案How to read until EOF from cin in C++

因此您可以使用getline 逐行读取cin 并解析结果行,如下所示:

#include <iostream>
#include <sstream>
#include <string>

int main() {
    int a, b;

    std::string line;
    while (std::getline(std::cin, line)) {
        std::stringstream stream(line);

        stream >> a >> b;
        std::cout << "a: " << a << " - b: " << b << std::endl;
    }

    return 0;
}

编辑:不要忘记检查解析结果和流状态是否有任何失败!

【讨论】:

    【解决方案2】:

    您应该始终检查您的输入是否成功:

    if (std::cin >> TmpVal) {
        // do simething with the read value
    }
    else {
        // deal with failed input
    }
    

    如果发生故障,您晚上想检查eof(),说明故障是由于已到达输入末尾。

    要处理错误,请查看std::istream::clear()std::istream::ignore()

    【讨论】:

      猜你喜欢
      • 2015-12-02
      • 2014-03-16
      • 2016-09-02
      • 2015-04-21
      • 1970-01-01
      • 2015-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多