Dljcali
Dljcali

Reputation: 81

I can't identify the endl character \n in my if statement

I am creating ASCII art for a game and my fstream is not receiving the '\n' (using char) in my if statement to delete from the stream. Seems simple, but I am puzzled at it right now..Any clue?

void pic_maker(ifstream &Pic_file, string P_Name, int x, int y, int t)
{
Pic_file.open(P_Name);
int margin = 0;
coordinate(x, y);
char pic_info;
Pic_File >> pic_info;

while (!Pic_File.eof())
{
    while (pic_info == '?')
    {
        Pic_File >> pic_info; //ADDS SPACES TO IMAGE BY REPLACING ?
        cout << " ";
    }
    if (pic_info == '0') //RETURNS TO NEW LINE
    {
        Pic_File >> pic_info;
        y++;
        coordinate(x, y);
    }
    if (pic_info == '\n') //MY PROBLEM! DOESN'T RECOGNIZE \n
    {
        Pic_File >> pic_info;
    }
    cout << pic_info;
    Pic_file >> pic_info;
}

Sleep(t);
}

Upvotes: 0

Views: 103

Answers (2)

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153810

The formatted input operator skip leading whitespace. Clearing the format flag std::ios_base::skipws for the stream, e.g., using the std::noskipws manipulator, avoids this skipping of whitespace.

I would rather us unformatted input operations, e.g., one of the std::istream::get() members or std::getline().

Upvotes: 0

Abhishek Bansal
Abhishek Bansal

Reputation: 12715

AFAIR the >> operator by default skips the newline character. If you want to read the whitespaces also, then you should add noskipws before reading the characters.

Pic_File >> std::noskipws;

Upvotes: 6

Related Questions