#include <iostream>
#include <fstream>
#include <string>
using std :: cout ;
using std :: ifstream ;
using std :: string ;
using std :: endl ;
int main ()
{
ifstream myfile ;
string line ;
myfile.open ("student.txt") ;
while (! myfile.eof ())
{
myfile >> line ;
cout << "first name : " << line << endl ;
myfile >> line ;
cout << "last name : " << line << endl ;
myfile >> line ;
cout << "ID : " << line << endl ;
myfile >> line ;
cout << "last degree : " << line << endl ;
myfile >> line ;
cout << "major : " << line << endl ;
myfile >> line ;
cout << "date registered : " << line << endl ;
}
return 0 ;
}
What I expected at the first was that for the second and third person the program Ignores their names , because there is a new line before them and the program will count it as the "line" for the name .
Why is that?
What if I wanted my program to count blank lines in a file?
I even made the blank lines more (for example 10 blank lines between every field of information) and still I had problem.
THank you!
The way >> works is that it ignores all whitespace characters up until the first non-whitespace character and then it tries to read whatever it is you are reading. The newline character is a whitespace character so it will be treated the same way as a normal space or any other whitespace character.