Aleph Null
Aleph Null

Reputation: 21

Why does my program infinitely loop?

The program that I'm working on reads an input file's contents (.csv), creates an output file (.txt), and outputs the input file's content in the output file in a formatted fashion. Here's how it looks:

#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
}

However, whenever I run it, it loops infinitely here:

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
}

The input file contains 1119 rows of information, so I'll give you the first row:

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

I let my program sit for a minute and nothing happened. Even adding this code at the beginning of the for loop only outputs the first row of information:

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;

Why does my program infinitely loop?

Upvotes: 1

Views: 139

Answers (1)

Telokis
Telokis

Reputation: 3389

What happens if, in this code,

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 is longer than iWidth ?

You will attempt to increment i until it reaches a negative number. Here is probably your issue.

Upvotes: 3

Related Questions