#include
#include
#include
int main()
{
ifstream infile;
ofstream outfile;
int text1,text2,text3,text4,text5;
double average;
char name;
infile.open("D:myfile.txt");
outfile.open("D:myfile1.txt");
outfile< outfile< if(!infile)
{
cout<<"Can't open the file!"< return 1;
}
cout<<"Processing date"< infile>>name;
outfile< infile>>text1>>text2>>text3>>text4>>text5;
outfile<<"The scores :"< average=static_cast(text1+text2+text3+text4+text5)/5.0;
outfile<<"Average test score is:"<
infile.close();
outfile.close();
return 0;
}
为什么这个程序不好用???????????????????????????????????????????????????
--------------------next---------------------
我大概看了一下你的程序好象是完成从myfile。txt中读取姓名及5门课程的成绩然后再算出平均成绩保存在myfile1.txt中
首现name不应定义成char型的改为char name[10];
把infile.open("D:myfile.txt");改为infile.open("D:myfile.txt",ios;;nocreate);否则当文件不存在时系统会自动创建空文件
该程序只能计算一个同学的成绩可以用循环完成所有同学成绩的统计
可将程序改为
#include
#include
#include
int main()
{ ifstream infile;
ofstream outfile;
int text1,text2,text3,text4,text5;
double average;
char name[10];
infile.open("D:myfile.txt",ios::nocreate);
outfile.open("D:myfile1.txt");
outfile< outfile< if(!infile)
{
cout<<"Can't open the file!"< return 1;
}
cout<<"Processing date"< while(!infile.eof())
{
infile>>name;
outfile< infile>>text1>>text2>>text3>>text4>>text5;
outfile<<"The scores :"< average=static_cast(text1+text2+text3+text4+text5)/5.0;
outfile<<"Average test score is:"< }
infile.close();
outfile.close();
return 0;
}
在D盘新建mymyfile。txt文件并输入一下内容:
张三 67 32 42 42 42
老汉 89 78 56 98 67
余波 89 67 32 89 32
运行后可在D盘生成mymyfile1。txt文件打开文件可见运行结果为:
张三
The scores : 67 32 42 42 42
Average test score is: 45.00
老汉
The scores : 89 78 56 98 67
Average test score is: 77.60
余波
The scores : 89 67 32 89 32
Average test score is: 61.80
--------------------next---------------------
阅读(1329) | 评论(0) | 转发(0) |