Hello Trixypu,
That alright I replaced it with "number" and it worked fine.
When I was first learning about using files I found this code useful:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
std::string iFileName{ "" }; // <--- Insert file name between the "".
std::ifstream inFile;
inFile.open(iFileName);
if (inFile.is_open())
{
std::cout << "\n File " << iFileName << " is open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2)); // <--- Needs header files chrono" and "thread".
}
else
{
std::cout << "\n File " << iFileName << " did not open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Needs header files chrono" and "thread".
exit(1);
}
|
This can be shortened when you have a better understanding of using files like what you have done with
std::ofstream flow("numbers.txt", std::ios::app);
. This creates the file stream object and opens the file at the same time and is the preferred way of doing this. The if/else statement checks to make sure the files is open which should be done for input files. This i not as necessary for output files because if the file does not exist it is created, so there is less of a chance the an output will not be opened.
As noted in my code the use of
using namespace std;
is a bad idea and
WILL get you in trouble some day. It is better to learn early what is in the "std" namespace and to qualify these names with "std::" like "std::cout", "std::cin" and "std::endl". A good IDE will help you with this.
Your program opens two output files in two different ways. Each way will work, but one of the file streams is never used.
For variable names associated "ifstream"s and "ofstream"s I like to use inFile" and "outFile". Some day when you have a program that requires more than one input or output file stream you could just add to inFile" and "outFile" something that is descriptive for the file it is using. This will make the variable name longer, but it is better than just adding a 2 or 3 to the end of the variable name.
One last note, when you write
for (int i = 0; i<10; i++) {
this is fine because the compiler does not care where the opening { is placed or about white space. But for reading the code this is much easier to read:
1 2 3 4
|
for (int i = 0; i<10; i++)
{
// Some code here.
}
|
When the {}s line up in the same column it is not only easier to read, but easier to find when when a closing } is missing.
I need to give you a commendation on the first for loop as it is well written and presents the input to the user in a good way.
Hope that helps,
Andy