Hello Battlefire,
I have not actually tested this yet, but I believe this is what you are looking for.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
while (inFile >> a >> b >> t)
{
ori_a = a;
ori_b = b;
a = a - b;
while (sum < t)
{
a = a + b;
sum += a;
counter++;
}
outFile << ori_a << " " << ori_b << " " << t << " " << counter << endl;
}
|
The variable name "file_Dir" is a bit misleading. The first thing I thought of is that this should be a path not a file name. Over time I settled on using "inFileName" as there is no mistake to what it is used for.
While I am on the subject give your variables a proper name. This would be a noun or sometimes a verb that describes what it is or what it does. This is much better than "a", "b" and "t" which only you know what they are for.
Next I see that you have opened a file stream for input, but how do you know it is open? I like to use this code when opening a file for input:
1 2 3 4 5 6 7 8 9 10
|
const std::string inFileName{ "" }; // <--- Put file name here.
std::ifstream inFile(inFileName);
if (!inFile)
{
std::cout << "\n File " << std::quoted(inFileName) << " did not open" << std::endl;
//std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Needs header files chrono" and "thread". This is optional as is the header files.
return 1; //exit(1); // If not in "main".
}
|
In your case remove the "const" from the "std::string" and put lines 3 - 10 after the "cin" statement. Otherwise it may not open the file stream, but the program will continue. It just will not be able to read anything from the file.
For an output file stream this is not as necessary because if the file name does not exist it will be created. Should you have a path of where to put the output file then it is useful as the path is usually where the problem lies. It is still a good idea to check if the output stream is open and usable.
One thing I would consider adding is to print out a list of the file names available for use. You could do this with a "std::string" array or a vector. Although you would have to keep this updated. Starting with C++14 there is the header file "experimental/filesystem" which I believe will print the files in a given directory. In C++17 this left the "experimental/" directory and became part of the standard include files. As I understand it.
In a little while I will load up the program and make the changes and give it a test. If I find anything else I will let you know.
Hope that helps,
Andy