ここで取り組んできた宿題の問題があります。説明は次のとおりです。
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;
}
そして、このプロジェクトと同じフォルダーにあるファイルは、次のように呼ばれます。
data.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 ステートメントを実行しようとしましたが、それが問題のようです。
助けていただければ幸いです。