Hello Donatas123,
I have been working on your program for a while. It would be a big help if you would post your input file along with the output that you expect.
This way everyone will be working with the same information.
As it is my guess for an input file may not be working correctly.
In your program line 5 is OK at the global level.
Line 7 should be put in "main" and you should avoid this type of global variable. The next problem with this is that in "main" you are passing a global variable to your function. This is not needed because it is a global variable. By defining a new variable in the function, even if it has the same name as the global variable, you are creating a local variable in the function that will not see the global variable. Any changes that you make to the local variable are lost when the function ends and nothing will change with the global variable.
When you open an input file it is a good idea to check and make sure that the file is open.
Something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
int main()
{
int n;
char letters[CMAX]{}; // <--- Moved to here.
std::string inFileName{ "duom.txt" }; // <--- Added this for use in the if statement and in line 8.
ifstream fd(inFileName);
ofstream fr("rez.txt");
if (!fd)
{
std::cout << "\n File \"" << inFileName << "\" did not open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Needs header files chrono" and "thread".
return 1;
}
|
Checking if the output file opened properly is not always necessary because if the file does not exist it will be created.
I am wondering if you have to use a character array or if you could use a "std::string"? The "std::string" would be much easier.
Note; the for loop to input from the file will skip any spaces in the file and put all the letters one after the other. Not sure if that is what you want.
Hope that helps,
Andy