【问题标题】:C++ File I/O - Program suspendsC++ 文件 I/O - 程序挂起
【发布时间】:2012-07-01 22:41:35
【问题描述】:

我一直在处理一个家庭作业问题;这是描述:

编写一个程序,读取包含学生考试成绩的文件 范围 0–200。然后它应该确定拥有 以下每个范围内的分数:0-24、25-49、50-74、75-99、 100–124、125–149、150–174 和 175–200。输出分数范围 和学生人数。 (使用以下输入运行您的程序 数据:76、89、150、135、200、76、12、100、150、28、178、189、167、200、 175、150、87、99、129、149、176、200、87、35、157、189)

以下是我制作的程序:

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

int main() 
{
//declaring variables
ifstream myfile("data.txt");
double grades[26];
int i, studentNumber=0;
int score0_24=0, score25_49=0, score50_74=0,
    score75_99=0, score100_124=0, score125_149=0,
    score150_174=0, score175_200=0;

cout << score150_174 << endl;

//initializing grades array
for(i=0; i<26; i++)
{
    grades[i] = 0;
    cout << grades[i] << " ";
}

//getting data from text file
    for(i=0; i<26; i++)
        {
            myfile >> grades[i];
            cin.ignore(2);
            studentNumber++;
        }

    myfile.close();

//finding number of people for each score range
for(i=0; i<26; i++)
{
    if(grades[i] <= 24)
        score0_24++;
    if(grades[i] >= 25 && grades[i] <= 49)
        score25_49++;
    if(grades[i] >= 50 && grades[i] <= 74)
        score50_74++;
    if(grades[i] >= 75 && grades[i] <= 99)
        score75_99++;
    if(grades[i] >= 100 && grades[i] <= 124)
        score100_124++;
    if(grades[i] >= 125 && grades[i] <= 149)
        score125_149++;
    if(grades[i] >= 150 && grades[i] <= 174)
        score150_174++;
    if(grades[i] >= 175 && grades[i] <= 200)
        score175_200++;
}

//outputing results
cout << "Number of students: " << studentNumber << endl;
cout << "0-24: " << score0_24 << endl;
cout << "25-49: " << score25_49 << endl;
cout << "50-74: " << score50_74 << endl;
cout << "75-99: " << score75_99 << endl;
cout << "100-124: " << score100_124 << endl;
cout << "125-149: " << score125_149 << endl;
cout << "150-174: " << score150_174 << endl;
cout << "175-200: " << score175_200 << endl;

return 0;
}

我拥有的文件,它与这个项目在同一个文件夹中,被称为:

数据.txt

并包含以下内容:

76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35 , 157, 189

当我运行程序时,它会在我从文件中读取数据的 for 循环中暂停。我已经尝试在之前和之后做一些 cout 语句,这似乎是问题所在。

我们将不胜感激。

【问题讨论】:

  • 你试过没有cin.ignore(2);吗?请记住,您正在读取文件,而不是 cin。
  • 你的意思是myfile.ignore(2);

标签: c++ input


【解决方案1】:

我有一些建议给你,虽然你没有问:)...

1) 不要先收集成绩然后再处理它们,而是将循环组合成 1,以便在读取值后立即对其进行处理。这消除了拥有 grades 数组和大约一半代码的需要。

2) 不要硬编码 26 个等级,而是从文件中读取直到最后:

for(int grade = 0; myFile >> grade; )
{
    if(myFile.peek() == ',')
         myFile.ignore(); // <--- this is why your version wasn't working (you were calling cin.ignore instead)

    // do something with grade here
}

3) 不要为 0-24、25-49 等创建 8 个计数器,而是创建一个 8 的数组并使用整数数学计算要访问的索引。

通过这些更改,您应该能够将其缩减为 15-20 行清晰可读的代码。

【讨论】:

    【解决方案2】:
    **File to read in named test.txt located @ c:\\users\\desktop\\test.txt**
    
    76 89 150 135 200 76 12 100 150 28 178 189 167 
    200 175 150 87 99 129 149 176 
    200 87 35 157 189
    ----------------------------------------------------------
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    int main()
    {
        ifstream infile;
        int scores[26] = { 0 };  
        int scoreCount[8] = { 0 };
        
    
       infile.open("c:\\users\\desktop\\test.txt");
       if (infile)
            for (int i= 0; i < 26; i++) {    //read file txt into scores array
                infile >> scores[i];
                if (!infile)
                    break; }
    
       
       for (int i = 0; i <= scores[i]; i++) {  //index score count into scoreCount array
           int index = scores[i] / 25;
           if (index >= 0 && index < 8)
                scoreCount[index]++;
                    else if (scores[i] == 200)
                        scoreCount[7]++; }
       
       int min = 0;
       int max = 24;
       
       for (int i = 0; i < 8; i++) {              //print ranges & score count
           cout << min << " - " << max << " ";
           cout << scoreCount[i];
           cout << endl;
           min += 25;
           max += 25;
           if (max == 199) max = 200; }
        
    infile.close();
    
    return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-10-28
      • 2017-02-27
      • 2013-12-18
      • 2012-11-05
      • 2023-03-08
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 2015-05-24
      相关资源
      最近更新 更多