I am trying to read and then output the contents of a text file with three lines, as shown below:
Bob Dylan 10 9
John Lennon 8 7
David Bowie 6 5
For each line, I just want to output a line, i.e. firstName LastName number1 number2.
I use the following code for this:
int num1;
int num2;
string firstName;
string lastName;
string fullName;
ifstream inFile;
inFile.open("inputFile.txt");
while (getline(inFile, firstName))
{
inFile >> firstName >> lastName >> num1 >> num2;
fullName = firstName + " " + lastName;
cout << fullName << " " << num1 << " " << num2 << endl;
}
inFile.close();
There are two problems with getting out of this. Firstly, the first line is not displayed, although I know from experiments that she reads it. Secondly, after the last 2 lines are read and displayed (optional), the program displays everything in the last line EXCLUDES the first name (in this case, the last thing it prints is Bowie 6 5).
- , , getline ? ( , , , ). .
-, getline (inFile, firstName) while boolean? , (.. ), firstName ? , , - , while, , ?
-, firstName , ( "" ), ? , , .
-, , , , , ? firstName? , ""? , while ? "", ?
Btw, ( ), getline, . , .