2017-03-23 19 views
1

私が作業しているプログラムは、入力ファイルの内容(.csv)を読み込み、出力ファイル(.txt)を作成し、入力ファイルの内容を出力ファイルに出力します。ここではそれがどのように見えるかです:なぜ私のプログラムは無限にループしますか?

#include "stdafx.h" 
#include <iostream>          // standard input/output library 
#include <string>          // string data type and its associated functions 
#include <fstream>          // file input/output 

using namespace std;          // use standard namespaces 

const int iRows = 1119;         // input file contains 1,119 rows 
const int iColumns = 11;         // input file contains 11 columns 

string strData[iRows][iColumns];       // 2-dimensional array that holds input file contents 

// pads strings to make them the same wide, for fixed width output 
string Align(string strIn, int iWidth) 
{ 
    string strOut;          // padding 

    // add padding 
    for (int i = 0; i < iWidth - strIn.length(); i++) 
     strOut += " "; 

    return strOut;          // return padding 
} 

// main program entry point 
int main() 
{ 
    ifstream inFile;          // handle for input file 
    string strSourcePath =        // input file path 
     "C:\\Users\\Logan\\Documents\\CIS022_S2017_Lab8b.csv"; 

    ofstream outFile;         // handle for output file 
    string strDestPath =         // output file path 
     "C:\\Users\\Logan\\Documents\\out.txt"; 

    inFile.open(strSourcePath);       // open input file for read (ifstream) 

    for (int i = 0; i < iRows; i++)      // loop for rows 
     for (int j = 0; j < iColumns; j++)    // embedded loop for column 
     { 
      if (j == iColumns - 1)      // the last element in the row is newline delimited 
       getline(inFile, strData[i][j], '\n'); 
      else           // all other elements are comma delimited 
       getline(inFile, strData[i][j], ','); 

      /*cout << "i = " << i << " j = " << j << " " << strData[i][j] << endl;*/ // console dump for error checking 
     } 

    inFile.close();          // done with input file, close it 

    outFile.open(strDestPath);       // open output file for write (ofstream) 

    for (int i = 0; i < iRows; i++)      // loop through each input row 
    { 
     outFile << 
      strData[i][0] << Align(strData[i][0], 7) << // CRN 
      strData[i][1] << Align(strData[i][1], 6) << // Subject 
      strData[i][2] << Align(strData[i][2], 6) << // Number 
      strData[i][3] << Align(strData[i][3], 20) << // Title 
      strData[i][4] << Align(strData[i][4], 7) << // Days 
      strData[i][5] << Align(strData[i][5], 13) << // Meetdates 
      strData[i][6] << Align(strData[i][6], 17) << // Times 
      strData[i][7] << Align(strData[i][7], 6) << // Credits 
      strData[i][8] << Align(strData[i][8], 13) << // Instructor 
      strData[i][9] << Align(strData[i][9], 6) << // Room 
      strData[i][10] << endl;      // Max Enroll 
    } 

    outFile.close();          // close output file 

    system("Pause");          // wait for user input 
    return 0;           // exit program 
} 

しかし、私はそれを実行するたびに、それはここで無限ループ:

for (int i = 0; i < iRows; i++)      // loop through each input row 
{ 
    outFile << 
     strData[i][0] << Align(strData[i][0], 7) << // CRN 
     strData[i][1] << Align(strData[i][1], 6) << // Subject 
     strData[i][2] << Align(strData[i][2], 6) << // Number 
     strData[i][3] << Align(strData[i][3], 20) << // Title 
     strData[i][4] << Align(strData[i][4], 7) << // Days 
     strData[i][5] << Align(strData[i][5], 13) << // Meetdates 
     strData[i][6] << Align(strData[i][6], 17) << // Times 
     strData[i][7] << Align(strData[i][7], 6) << // Credits 
     strData[i][8] << Align(strData[i][8], 13) << // Instructor 
     strData[i][9] << Align(strData[i][9], 6) << // Room 
     strData[i][10] << endl;      // Max Enroll 
} 

入力ファイルは、情報の1119行を含むので、私はあなたの最初の行をあげます:

CRN,Subj,Num,Title,Days,Meetdates,Times,Credits,Instructor,Room,Max Enroll 

私のプログラムは1分間座り、何も起こらなかった。 forループの先頭にこのコードを追加するだけで、最初の情報行が出力されます。

cout << 
    strData[i][0] << " " << 
    strData[i][1] << " " << 
    strData[i][2] << " " << 
    strData[i][3] << " " << 
    strData[i][4] << " " << 
    strData[i][5] << " " << 
    strData[i][6] << " " << 
    strData[i][7] << " " << 
    strData[i][8] << " " << 
    strData[i][9] << " " << 
    strData[i][10] << endl; 

なぜ私のプログラムは無限にループしますか?

string Align(string strIn, int iWidth) 
{ 
    string strOut;          // padding 

    // add padding 
    for (int i = 0; i < iWidth - strIn.length(); i++) 
     strOut += " "; 

    return strOut;          // return padding 
} 

strInが長いiWidthよりも、このコードでは、どうなります

+3

このような問題にアプローチするには、プログラムをデバッグするのが一般的です。なぜそれをしないのですか? –

+0

1119回の反復処理中に* slow - *に反して、*無限にループすると思いますか? – AnT

+0

ところで、あなたは 'endl'を使うべきではありません。代わりに '\ n'を入れてください。 – Telokis

答えて

3

iは、負の数になるまで増分します。 これはおそらくあなたの問題です。

+0

ああ、そうですね、感謝しませんでした! – Telokis

+0

それが問題でした。ありがとう! –

関連する問題