简体   繁体   中英

C++ ignoring the first character

I am trying to read a text file (text.txt) that contains

1. One
2. Two
3. Three
4. Four
5. Five
6. Six

However it's ignoring the first character and giving me the following output.

1. One
 Three
 Five
 Two
 Six
 Four

It ignores the numbers. My program reads the input file to an array and randomly spits out the items in the array. It works but it doesn't print out the numbers. How should I fix this?

Below is my code.

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

int main()
{
    const int arraySize = 6;
    //random seeding
    srand(time(0));
    std::string myArray[arraySize];
    std::ifstream inFile("text.txt");

    if (inFile.is_open())
    {
        int i = 0;
        while (i < arraySize && std::getline(inFile, myArray[i++]))
        {
            inFile >> myArray[i];
        }
    }
    for (int i = 0; i < arraySize; i++)
    {
        int index = rand() % arraySize;
        //swap myArray[i] with myArray[index]
        std::string temp = myArray[i];
        myArray[i] = myArray[index];
        myArray[index] = temp;
    }

    for (int i = 0; i < arraySize; i++)
    {
        std::cout << myArray[i] << std::endl;
    }
    return 0;
}

At the first iteration ( i = 0 ), you get the first line using getline and assign it to myArray[0] , and immediately increnent the variable i . Now i is equal to 1 . Then you enter the while loop, and read the second line using >> operator, until reaching to the first whitespace, and assign it to myArray[1] .

In the next iteration on the while loop ( i is still 1 ), you read the remaining characters of the second line and assign it to myArray[1] ; This is the reason that the number is ommited. Then you increase i to 2 and the pattern will be occurred until the end of the loop.

You don't need the inFile >> myArray[i]; line; Using getline will do what you want.

You're doing more than you think you are in your while loop.

std::getline(inFile, myArray[i++]) actually does everything you need. getline() reads a complete line of text from the input stream, up to a newline character (the newline is excluded from the output).

The >> operator reads data from a stream up to any whitespace character, newlines included . So, this line:

inFile >> myArray[i];

reads one word from your file (in this case the number at the beginning of the line) and leaves the rest behind, whitespace included. When the loop continues, getline() reads the rest of the line. Because i is not incremented until after this, the rest of the line is copied to the same array element that the number was in, overwriting it as a completely new string.

The first line, '1. One', is correct because when getline() first reads from the file, the input stream is at the very beginning. The body of your loop hasn't executed yet. Then, i increments and your program reads the first "word" on the second line ( 2. ), which is subsequently overwritten with Two when the loop runs again.

The solution, then, is to leave your while loop with an empty body, like this:

while (i < arraySize && std::getline(inFile, myArray[i++]))
{
    ;
}

Or move the call to getline() into the body of the loop and out of the conditional statement:

while (i < arraySize)
{
    std::getline(inFile, myArray[i++]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM