Both of the while loops work individually. However, the second while loop in my code won't work when the while loop above it is present. I need both while loops to work together.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
string line, line1, line2;
int count = 0;
int track = 0;
int stop;
ifstream file ("ai.txt");
while (getline(file, line2)){
count++;
}
file.seekg (0L, ios :: beg);
stop = count - 10;
while (getline(file, line)){
track++;
if (track >= stop){
for (int num = 1; num <=10; num++){
getline(file,line1);
cout << line1 << endl;
}
}
}
}
The code should output the last ten lines of any text file.