2

サブスクリプト演算子を使用してかなり簡潔なC++を記述した後、プログラムに小さなエラーが発生しました。出力がありません。

これを入力します(Linux)

54 73 89 43 38 90

次に、Cntrl+Dを押してEOFを取得します。プログラムは何も出力せず、実行を停止します。

ソース:

#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::vector;
using std::cout;
using std::endl;

int main() {
vector<unsigned> scores(11, 0); //11 buckets, all initially 0
unsigned grade;
while(cin >> grade) //read the grades
{
   if(grade <=100) //handles only valid inputs
  {
   ++scores[grade/10]; //increment counter for the current cluster
  }
 }
}

VIMの設定を変更していないので、コーディングスタイルが少しずれています。何が悪いのか想像できませんが、whileループはかなり標準的です。ストリームが無効であることがわかるまで、成績を読み込みます。次に、入力が100(両端を含む)未満であるかどうかを確認します。コードの最後の部分(かなり簡潔です)は、カウンターをインクリメントするためにベクトル内の正しい要素を見つけます。

プログラムが出力されない原因は、おそらく私の入力である可能性があると感じています。

編集1:出力ステートメントを追加しました。これは、常に参照である間接参照を使用して行いました。

#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::vector;
using std::cout;
using std::endl;

int main() {
vector<unsigned> scores(11, 0); //11 buckets, all initially 0
unsigned grade;
while(cin >> grade) //read the grades
{
   if(grade <=100) //handles only valid inputs
  {
   ++scores[grade/10]; //increment counter for the current cluster
  }
 }
for(auto it = scores.begin(); it != scores.end(); ++it) {
cout << *it << endl;
 }
}
4

1 に答える 1

6

プログラムが出力されない原因は、おそらく私の入力である可能性があると感じています。

完全ではありません。プログラムに出力ステートメントがないために、プログラムが出力されません。

于 2013-01-05T11:48:26.247 に答える