文件读取不正确,显示此...
该文件已创建,并且与.cpp文件位于同一目录中,但它无法打开。查看文件扩展名已启用(在Windows资源管理器中),因此不是问题...为什么它无法正确读取?
我想添加" ifstream inputFile;"在函数范围内会有所帮助,但因为它是全局声明的,所以它不应该是。该文件的拼写完全符合预期,因为我知道它区分大小写。
这只是我的一段代码
完整代码 https://hastebin.com/pomahoyefi.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
char getUserChoice();
int calcNumber();
double calcSum();
double calcAvg();
ifstream inputFile;
int main()
{
ifstream inputFile;
char choice;
int num;
double sum;
double average;
do
{
choice = getUserChoice();
if (choice == 'a')
{
num = calcNumber();
cout << "Number of scores is " << num << endl;
}
if (choice == 'b')
{
sum = calcSum();
cout << "Sum of scores is " << sum << endl;
}
if (choice == 'c')
{
average = calcAvg();
cout << "Average of scores is " << average << endl;
}
}
while(choice == 'a' || choice == 'b' || choice == 'c');
return 0;
}
char getUserChoice()
{
char choice;
do
{
cout << "Make your choice" << endl;
cout << "================" << endl;
cout << "a) Calculate the number of scores in the le" << endl;
cout << "b) Calculate the sum of all the scores in the le" << endl;
cout << "c) Calculate the average of all scores in the le" << endl;
cout << "d) Quit" << endl;
cin >> choice;
if (!(choice == 'a' || choice == 'b' || choice =='c' || choice =='d'))
{
cout << "Invalid input" << endl;
}
}
while(!(choice == 'a' || choice == 'b' || choice =='c' || choice =='d'));
return choice;
}
int calcNumber()
{
ifstream inputFile;
double number;
int total = 0;
inputFile.open("studentData.txt");
if(! inputFile)
{
exit(0);
}
else
{
while (inputFile >> number)
{
++total;
}
inputFile.close();
}
return total;
}