所以这里是我写的一个void函数,但是<<
显示为红色并且不会运行程序。这是我第一次遇到这个问题,我该怎么办?这是在c++中完成的,我使用visual studio作为我的编译器。
void readData() //reading the function
{
ifstream inputfile;
inputfile.open("student.txt"); //open file for the students grades and names
for (int count = 0; count<20; count++)
{
inputfile **>>** st[count].studentFName >> st[count].studentLName; //reading each field
for (int i = 0; i<5; i++)
inputfile >> st[count].test_score[i]; //reading each score
}
inputfile.close(); //close the file
cout << " Reading of the file was completed!" <<endl;
}
是**>>**
的部分是我的错误发生的地方。它说它不是弦的一部分。
你可能少了一个#include
。我怀疑是<istream>
。
#include <iostream>
#include <fstream>
#include <string>
#include <istream>
using namespace std;
struct StudentType
{
string studentFName;
string studentLName;
int test_score[5];
char score; //naming my variables
};
struct StudentType stu[20]; //you cannot enter more than 20 students with grades
void data() //reading the void function
{
ifstream inputfile;
ifstream inputfile("student.txt"); //open file for the students grades and names
for (int count = 0; count<20; count++)
{
inputfile >> stu[count].studentFName >> stu[count].studentLName; //reading each field
for (int i = 0; i<5; i++)
inputfile >> stu[count].test_score[i]; //reading each score
}
ifstream inputfile("student.txt"); //close the file
cout << " Reading of the file was completed!" << endl;
}
void studentGrade()
{
ofstream outputfile;
outputfile.open("output.txt"); //Open file for writing
int total[20];
for (int i = 0; i<20; i++) //20 records
{
total[i] = 0;
for (int j = 0; j<5; j++) //calculating scores
total[i] += stu[i].test_score[j];
cout << " Totals : " << total[i] << endl;
if ((total[i] / 5) >= 90) //finding of grades
stu[i].score = 'A';
else if ((total[i] / 5) >= 80)
stu[i].score = 'B';
else if ((total[i] / 5) >= 70)
stu[i].score = 'C';
else
stu[i].score = 'D';
}
for (int i = 0; i<20; i++) //This will be for entering in 20 different students and grades
{
outputfile << stu[i].studentFName << " " << stu[i].studentLName << " "; //the data for the file
for (int j = 0; j<5; j++) //writing of 05 scores
outputfile << " " << stu[i].test_score[j];
outputfile << " " << stu[i].score << endl; //this will be the grades for the students
}
outputfile.close(); //closing the file
cout << " File content is stored in output.txt" << endl;
}
int main()
{
cout << " Welcome to my file!" << endl;
data();
studentGrade();
cout << "Thank you for using my program! " << endl;
system("pause");
return 0;
}