我正在用C ++编写一个简单的程序,它从文本文件中读取如下的示例输入: 样本输入
7 10
William Horde AA AA AA AA AA AA AA AA AA AA ;
John Doe AA AA AA FF AA AA AA AA AA AA ;
Hermann Straus AA AA AA AA AA BB NA AA AA AA ;
Patrick Doe CC CC DC CC CB CC DC CC CC CC ;
Xhulia Abazi AA BB BB BA CB BB CB CC BB AA;
John Trki CC BB DC DD DD DD BB AA BB CC ;
Julien Torfelt NA FD FD DD DC DC DC DD DD ;
在第一行给出学生数0
以下是我的源代码:
#include <iostream>
#include <ofstream>
using namespace std;
void getData(char fnames[], char lnames[], string grades[], int &n, int &m);
int main()
{
char fnames[100];
char lnames[100];
string grades[100]; int n,m;
getData(fnames,lnames, grades , n, m);
return 0;
}
void getData(char fnames[],char lnames[], string grades[], int &n, int &m)
{
ifstream fin("input.txt");
fin>>n; // read number of students
fin>>m; // read number of courses
double sum = 0.0;
for (int i=0; i<n; i++)
{
fin>>fnames;
fin>>lnames;
for (int j=0; j<m; j++)
{
fin>>grades[j];
if(grades[j]=="AA") sum = sum + 4.0;
else if(grades[j]=="BA") sum = sum + 3.5;
else if(grades[j]=="BB") sum = sum + 3.0;
else if(grades[j]=="CB") sum = sum + 2.5;
else if(grades[j]=="CC") sum = sum + 2.0;
else if(grades[j]=="DC") sum = sum + 1.5;
else if(grades[j]=="DD") sum = sum + 1.0;
else if(grades[j]=="FD") sum = sum + 0.5;
else sum = sum + 0.0; //FF , NA
}
}
}
如何让我的程序正常运行?
答案 0 :(得分:0)
首先,有一个终止分号,你没有考虑到它。在阅读完所有成绩后,最好放弃一行中的所有内容。只需使用istream::ignore
// .............
float avg = sum / m;
cout<<"CGPA for "<<fnames<<" "<<lnames<<" is = "<<avg<<endl;;
fin.ignore(10, '\n'); // <------------------------------------ add this line
}
}
其次,您必须为每位新学生重置sum
。所以将sum
初始化移到学生循环中:
fin>>n; // read number of students
fin>>m; // read number of courses
//double sum = 0.0; // <------------------------------------ remove from here
for (int i=0; i<n; i++)
{
double sum = 0.0; // <------------------------------------ put here
fin>>fnames;
fin>>lnames;
答案 1 :(得分:0)
代码问题是';'在文件中的行尾没有读取。
该程序未能获得';'在fin中,这样当循环继续到fin>&gt; fnames **读取冒号,然后下一个fin继续第二行中的firstname作为lnames。
getData函数的更正代码是,
void getData(char fnames[],char lnames[], string grades[], int &n, int &m)
{
ifstream fin("file.txt");
char getColon[2] ; //added
fin>>n; // read number of students
fin>>m; // read number of courses
for (int i=0; i<n; i++)
{
double sum = 0.0;
fin>>fnames;
fin>>lnames;
for (int j=0; j<m; j++)
{
fin>>grades[j];
if(grades[j]=="AA") sum = sum + 4.0;
else if(grades[j]=="BA") sum = sum + 3.5;
else if(grades[j]=="BB") sum = sum + 3.0;
else if(grades[j]=="CB") sum = sum + 2.5;
else if(grades[j]=="CC") sum = sum + 2.0;
else if(grades[j]=="DC") sum = sum + 1.5;
else if(grades[j]=="DD") sum = sum + 1.0;
else if(grades[j]=="FD") sum = sum + 0.5;
else sum = sum + 0.0; //FF , NA
}
fin >> getColon ; //Added
float avg = sum / m;
cout<<"CGPA for "<<fnames<<" "<<lnames<<" is = "<<avg<<endl;
}
}
上面代码中的行fin >> getColon ; //Added
将冒号作为调整,代码流继续。
上述代码的Exact输出为,
CGPA for William Horde is = 4
CGPA for John Doe is = 3.6
CGPA for Hermann Straus is = 3.5
CGPA for Patrick Doe is = 1.95
CGPA for Xhulia Abazi is = 2.65
CGPA for Trki CC is = 1.95
CGPA for Torfelt NA is = 0.85
同样初始化第一个for循环中的sum
变量,以便在计算之前为每个学生重置它。