Need help for code. I'm stuck

string fileName[] = {"t0.txt", "t1.txt", "t2.txt", "t3.txt", "t4.txt", "t5.txt", "t6.txt", ""};
int trg[MAXTBLSIZE][MAXTBLSIZE] = {0};
int trgSize; // the number of rows and columns
int trgConstant; // -1 if it is not a perfect triangle or the triangle's constant otherwise
int choice = 1; // to stop the program to allow the user to see the results one table at a time
int maxSum;
ifstream inFile; // input of input file
ofstream outFile; // open output file
for (int i = 0; choice == 1 && fileName[i] != ""; i++)
{
if (readtrg(fileName[i], trg, trgSize))
{
trgConstant = testtrg(trg, trgSize);
printtrg(trg, trgSize);
printResults(trgConstant);
}
else
{
cout << "Error: Input file \"" << fileName[i] << "\" not found!" << endl;
}

cout << "Please enter 1 to continue 0 to stop" << endl;
cin >> choice;
cout<<"Highest Sum:"<<maxSum<<endl;
}

return 0;
}
bool readtrg(string &m, int trg[][MAXTBLSIZE], int &trgSize)
{
ifstream inFile; // input of input file
ofstream outFile; // open output file
inFile.open("m");
}

I'm stuck in here. Can someone explain how I can open the text file from the filename array one by one?
Last edited on
It's a little messy, but it ought to work; well, open the file at least.

Are the files in the application's "current directory"? Maybe you should print it at the start, just to be sure.
Posix: https://pubs.opengroup.org/onlinepubs/9699919799/functions/getcwd.html
Windows: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcurrentdirectory

[EDIT]
Just noticed:
 
inFile.open("m");

should be:
 
inFile.open(m);

Or better yet:
 
ifstream inFile(m);
Last edited on
it gives me an error like this when i changed it to

inFile.open(m)

D:\22B_H2\22B_H_2C.cpp|51|error: no matching function for call to 'std::basic_ifstream<char>::open(std::__cxx11::string&)'|
Last edited on
as you can see std::basic_ifstream<char> expects char


inFile.open(m.c_str());
thank you!
The better thing to do here would be to compile with C++11 (8 years old at this point), so that you can use std::strings there instead of char-pointers.

In fact, C++11 or later has been the default on all mainstream compilers for quite some time now, so you should update your compiler.
Last edited on
Topic archived. No new replies allowed.