为什么我的程序无限循环?

时间:2017-03-23 06:19:35

标签: c++

我正在处理的程序读取输入文件的内容(.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

我让我的节目坐了一会儿,什么都没发生。即使在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;

为什么我的程序无限循环?

1 个答案:

答案 0 :(得分:3)

如果在此代码中

,会发生什么
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

您将尝试增加i,直到达到负数。 这可能是你的问题。