我是第二个OOP课程的编程学生,我的第一堂课是用C#教授的,这门课是用C ++教授的。为了让我们学习C ++,我们的教授要求我们用File I / O编写一个相当大的程序。问题是,我的程序中有一小部分不起作用,至少不适合我。该项目要求我们编写一个循环来测试文件是否可以成功打开,并且我写的循环似乎不起作用。
我没有得到任何编译器错误,但是当我输入文件的路径时,无论是相对还是绝对,它都说它无效。我觉得这与我do-while
循环中的条件有关,但我无法确定它。
我不是故意要把我的作业带到现场,但是我已经摸不着头两个小时了,我似乎无法弄清楚这一点。
你介意帮我修理我的循环吗?也许解释一下我做错了什么?我想学习。
谢谢!的
代码:
Rainfall rData;
ifstream actualReader;
ifstream averageReader;
string aRDataLoc;
char response = 'a';
const int KILL_VALUE = 1;
double actualRainfallD;
double actualRainfallPassedArray[ARRAY_CAPACITY];
double averageRainfallPassedArray[ARRAY_CAPACITY];
int i = 0;
do
{
actualReader.clear();
cout << "\nPlease enter in the path to the file containing the actual rainfall data." << endl;
cout << "Path to file: ";
cin >> aRDataLoc;
actualReader.open(aRDataLoc.c_str());
if (!actualReader.is_open())
{
cout << "Invalid file path! Would you like to enter in a new file path?(y/n): ";
cin >> response;
if (response == 'n') { exit(KILL_VALUE); }
}
}while (!actualReader.is_open() && response == 'y');