-2

I'm quite new to C++, having started learning over the summer.

For an assignment I am supposed to write a program that will extract records from an input text file.

However, the for loop I am using to display the contents of a vector never executes. I believe this is because the loop condition determines that the vector has zero elements, but I do not know why.

The program is designed to start off with a vector of length zero and populate it with "flightRecord" class objects as the program reads through the text file.

The vector is declared outside of main, and should thus be global, right? My impression is that any elements placed inside a global vector would remain until explicitly removed, or the vector goes out of scope.

If anyone can help me resolve this, it would be much appreciated!

Summarised code below:

    fileOpener >> word;
    while (fileOpener.good())   //As long as end of file not reached and the file is open
    {
        //std::cout << word << " "; //Put spaces between each word
        fileOpener >> word; //Read the contents of the stream into char array word

        if (fileOpener.get() != '\n')   //if the current char is not equal to a newline
        {
            infoString.append(word);
            std::cout << "infoString contains: " << infoString << std::endl;
        }

        else
        {
            std::cout << "Current char is a endline char." << std::endl;    //For debug
        }
    }

    //EOF Notification

    if (fileOpener.eof() == true)   //If the end of file has been reached
    {
        std::cout << "End of file reached." << std::endl;   //Tell the user the EOF has been reached
    }

    // --------------------------------------------
    // Text Processing

    //while the end of file has not been reached
    unsigned int i = 0; //Used for traversing the input string - unsigned to make compiler happy
    while (i < infoString.size())   //While less than the total size of string - ERROR 
    {
        if (infoString.at(i) == ',' || infoString.at(i) == '\n')    //If the individual element of the string is a comma OR a newline (to delimit the last piece of info)
        {
            commaCounter += 1;  //Add one to the current value when a comma is located
            switch (commaCounter)   //Test the value of the comma counter - corresponds to the different string variables in class
            {
            case 1:
                tempFlightNum = infoString.substr(commaLocation, (i - commaLocation));  //Comma location is the starting point for the search --- (i-(commaLocation)) is the length of the substring
                commaLocation = i;  //Store the location of the comma while i continues to increase
                break;
            case 2:
                //Subtracting one from i makes the substr shorter, thus exluding the last comma -- Also begin the substr at one past the comma location to exlude the first comma
                tempOriginAirport = infoString.substr(commaLocation + 1, ((i - 1) - commaLocation));
                commaLocation = i;  //Update the comma location
                break;
            case 3:
                tempDestAirport = infoString.substr(commaLocation + 1, ((i - 1) - commaLocation));  
                commaLocation = i;  //Update the comma location
                break;
            case 4:
                tempNumPassengers = infoString.substr(commaLocation + 1, ((i - 1) - commaLocation));   
                commaLocation = i;  //Update the comma location
                break;
            case 5:
                std::cout << "Comma counter currently 5" << std::endl; //Debugging notice
                tempAvgPrice = infoString.substr(commaLocation + 1, ((i - 1) - commaLocation)); 
                commaLocation = i;  //Update the comma location
                commaCounter = 0;   //reset the comma counter to the starting position
                break;
            }

            // When the end of the line is reached, append the temp variables to the appropriate member functions in the class instance
            if (infoString.at(i) == '\n')   //If a newline is encountered...
            {
                //Append the data to a new flightRecord instance in a vector 
                //Issue is that populating the vector is dependent on 

                // Problem - vector is out of scope after while loop is exited ****
                flightVector.push_back(new flightRecord(tempFlightNum, tempOriginAirport, tempDestAirport, tempNumPassengers, tempAvgPrice)); //Need to delete at end to clear memory 
                std::cout << "testing - i is currently == to \n" << std::endl;
            }

        }

        i++;    //Increase the search index, whether comma or not
    }


    //Display
    // --------------------------------------------
char continue_record = NULL;    //If the user wants to display the next record

    for (unsigned int i = 0; i < flightVector.size(); i++)
    {
        // Processing here - This loop does not execute
    }

    system("PAUSE");

    return 0;
}
t0000m
  • 791
  • 1
  • 5
  • 5
  • 1
    TLDR. Try to reduce your example to a **minimum**. But still complete. – Cheers and hth. - Alf Dec 10 '15 at 06:32
  • Hint: What do you do with `word` when you read a newline from `fileOpener`? – 1201ProgramAlarm Dec 10 '15 at 06:40
  • Alf, thanks for letting me know, I cut out some stuff not directly relevant to the problem. – t0000m Dec 10 '15 at 06:44
  • 1201ProgramAlarm - When a newline is encountered, I believe `word` should stop appending its contents to `infoString` and the second condition executes. Which confuses me slightly because removing all text from the read-in file except for the first line produces an empty `infoString`... – t0000m Dec 10 '15 at 07:13
  • [Your read loop structure is incorrect](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). Instead, the test condition should be whether each read succeeded. Also, you never do anything with the word you read before the loop. – M.M Dec 10 '15 at 07:15

1 Answers1

0

The vector code is fine, only the push_back is not getting called. When you are appending the values to infostring, '\n' is skipped.

Deepak K Gupta
  • 6,416
  • 1
  • 18
  • 31