I'm supposed to read a text file one character at a time and every time there is a new line the line
should be increased by one.
So here is the relevant part of code:
ifstream textFile.open("PATHWAY::HERE//RRER.txt");
int line = 1;
char letter;
while (textFile)
{
//Read in letter
textFile >> letter;
// If you reach the end of the line
if (letter == '\n')
{
line++;
cout << line;
}
}
The if statement is completely ignored for some reason and doesn't ever print out line.
Although the answers (till now) have mentioned the problem about "\n" correctly, the approach mentioned may not work. Reason being >>
is formatted input operator which will skip whitespaces. You will have to read the file using std::ifstream::get
The code will look something like:
while (textfile.get(letter))
{
// If you reach the end of the line
if (letter == '\n')
{
line++;
cout << line;
}
}