Hello Awesomeness,
Your posted code is good, but could be improved upon.
As is the code poses more questions because of what is missing like the include files, "main". Is this code in a function or part of "main"?
It is always best to post enough code that can be compiled and tested because others may see something that you may have missed.
The program uses an input file. You need to provide the input file. If it is large then a good sample that will work with your program. If possible maybe a link to the file.
I see the point of the first while loop and would suggest something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
//while (std::cout << "What size word do you want? " && !std::cin || size > 30 || size < 1) // <--- An alternative.
while (!std::cin || size > 30 || size < 1)
{
if (!std::cin)
{
std::cerr << "\n Invalid Input!. Must be a number.\n\n";
std::cin.clear(); // <--- Resets the state bits on the stream.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>. Clears the input buffer.
}
else if (size > 30 || size < 1)
{
std::cerr <<
"Please enter a number between 1 and 30\n"
"Try again: ";
cin >> size; // <--- Not needed if you use the alternative while loop.
}
}
|
This should work, but is untested until I get all the errors worked out of your code.
The next problem I see is with lines 22 and 23 of your code. How do you know if the files are open? You do not because you never check them. Your program would continue as if nothing is wrong until you try to read the file and get nothing.
When dealing with files and some type of "fstream" I found this the be helpful. It may be a little to much, but in the beginning it helps.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
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::cout << "\n File \"" << inFileName << "\" did not open." << std::endl;
return 1;
}
std::string outFileName{ "" }; // <--- Put File name here.
std::ofstream outFile(outFileName);
if (!outFile)
{
std::cout << "\n File " << std::quoted(outFileName) << " did not open" << std::endl;
//std::cout << "\n File \"" << inFileName << "\" did not open." << std::endl;
return 2;
}
|
Something to note here: Do not be afraid to use long variable names, e.g., "inFileName", as long as it describes what it is. Also you use the variable name "count". "wordCount" would be more descriptive and the code would be easier to understand.
The next while loop:
1 2 3
|
while (getline(inputFile, arrPtr[j]))
{
inputFile >> arrPtr[j];
|
The while condition is good, but the next line overwrites what you just read, so you miss processing the first read and everything will be off.
I believe the if statement is premature. You say the the output file should be in reverse order, but you are writing to the file in forward order.
I am think that instead of writing to the output file you would need to write to a second array the words that fit then later write that second array to the output file , but in reverse order. That would be starting at the end of the array and working backwards.
Without knowing what the input file looks like it is hard to say if what you have started with actually works.
Andy