Hi CN.L!
I have been writing C++ code for close to 10 years. During that time I have learnt how to use C++ in a way that minimizes the number of errors (bugs) I create. Probably some will disagree with me, but I would recommend you to only use for and while to do looping. Never do-while. Learn these two well and you will be able to loop successfully any time you want.
To illustrate my technique, I have taken the liberty to rewrite your code using my style. It has complete error checking, uses a while loop with read-ahead, some C++0x, and simplified stream handling:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
int main(int argc, char** argv)
{
// check program arguments
if (argc<2) {
std::cerr << "Usage: " << argv[0] << " file" << std::endl;
return EXIT_FAILURE;
}
// check file can be opened
std::ifstream infile(argv[1]);
if (!infile) {
std::cerr << "Failed to read " << argv[1] << std::endl;
return EXIT_FAILURE;
}
std::string input;
// read-ahead
std::getline(std::cin, input);
while (input!="q" && input!="quit" && input!="exit") {
//system("cls");
// print contents of file by streaming its read buffer
std::cout << infile.rdbuf();
// read file again
infile = std::ifstream(argv[1]);
// finally, read again to match read-ahead
std::getline(std::cin, input);
}
}
Save to main.cpp, compile to print.exe and run with print.exe main.cpp.
Good luck with learning C++!