我试图将我的outfile流设为全局,但结果是" ' OUTFILE'没有命名类型"错误弹出。我试过做一些谷歌搜索,但没有一个与fstream有关..
我的代码:
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
#include<cstdlib>
using namespace std;
void determinepointer(string[],int,float[]);
void printresults(string[],float[],int,char[][9],int[]);
float calcGPA(float[],int[],int);
ofstream outfile;
outfile.open("StudentsTranscript.txt");//the problematic part
int main()
{
ifstream infile;
int coursenum;
infile.open("StudentsFile.txt");
infile.ignore(40,'\t');
infile>>coursenum;
char coursecode[coursenum][9];
int credithr[coursenum];
infile.ignore(13);
for (int i =0;i<coursenum;i++)
{
infile>>coursecode[i];
infile.ignore(1);
if(coursecode[i][7]=='1')
credithr[i]=1;
else if(coursecode[i][7]=='2')
credithr[i]=2;
else if(coursecode[i][7]=='3')
credithr[i]=3;
else if(coursecode[i][7]=='4')
credithr[i]=4;
else
{
cout<<"invalid course code, please re-check & run this program again";
exit(0);
}
}
infile.ignore(10,'\t');
outfile<<"STUDENT'S INDIVIDUAL TRANSCRIPT\n\n";
while(!infile.eof())
{
char name[40];
string grade[coursenum];
float coursepointer[coursenum];
infile.getline(name,40,'\t');
outfile<<name<<endl;
for (int i =0;i<coursenum;i++)
{
infile>>grade[i];
infile.ignore(2);
}
determinepointer(grade,coursenum,coursepointer);
printresults(grade,coursepointer,coursenum,coursecode,credithr);
}
infile.close();
cout<<"done";
return 0;
}
**编码太长,完整代码在这里:https://drive.google.com/file/d/0B_ir83gzFmIBQ0dXaGhPNXBJZkE/view?usp=sharing
答案 0 :(得分:1)
发表声明
outfile.open("StudentsTranscript.txt");//the problematic part
主要功能内部。您可以在函数外部声明变量,而不是使用它。
答案 1 :(得分:1)
您可以像@oLen建议或(如果您要求outfile
全局藐视,您可以执行以下操作:
ofstream outfile("StudentsTranscript.txt");