0

次のようなファイルからデータを読み込もうとしています。

1 1 2007 12 31 2006
12 31 2007 1 1 2008
1 3 2006 12 15 2006
2 29 1900 3 1 1900
9 31 2007 10 28 2009

int値は日付であると想定されているため、1行に2つの日付があるため、関数を使用して3つのグループで読み取ります。ペアの最初の日付でエラーが返された場合は、次の行にスキップしてそこからループを繰り返す必要があります。エラーがなければ、単に次のグループを評価します。エラーのファイルとbool変数は参照パラメーターとして関数に渡されるため、Get_Date()関数の外部でそれに応じて変更されます。私は次のようなステートメントを使用ignore()してみました:break

while (infile) {
   Get_Date(infile, inmonth1, inday1, inyear1, is_error);
   if (is_error == true){
      infile.ignore(100, '\n');
      break;
   } 
   if (is_error == false) {
      Get_Date(infile, inmonth2, inday2, inyear2, is_error);
      if (is_error == false) {
         Get_Date(infile, inmonth2, inday2, inyear2, is_error);
         other stuff; 
      } 
   }    
}

これにより、うるう年で 2 月は 29 日しかないため、4 行目で 1 つのエラーが発生した後、プログラム全体が終了します。ブレークは制御を最も近いループに戻すと思っていましたが、そうではないようです。

4

1 に答える 1

0

continueエラーが見つかったら、サイクルの残りの実行をスキップするために使用できます。

while (infile) 
{
    Get_Date(infile, inmonth1, inday1, inyear1, is_error);
    if (is_error)
    {
        //Skip the rest of the line  (we do not know how long it is)
        infile.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
        continue; //Skip the rest of the cycle
    } 

    Get_Date(infile, inmonth2, inday2, inyear2, is_error);
    if (is_error) { continue; } //Skip the other stuff if we have error

    otherStuff(); 
}

ここのJumpステートメントの下にいくつかの詳細情報があります:http://www.cplusplus.com/doc/tutorial/control/

于 2013-10-23T10:12:20.877 に答える