gpt4 book ai didi

c++ - 为什么我的程序会无限循环?

转载 作者:行者123 更新时间:2023-11-30 02:26:17 26 4
gpt4 key购买 nike

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

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

最佳答案

如果在这段代码中,会发生什么

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
}

strIniWidth 长?

您将尝试递增 i 直到它达到负数。这可能是您的问题。

关于c++ - 为什么我的程序会无限循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42968606/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com