求各位大神帮忙看看我这程序哪里有问题。
每当我按1,输入了学生信息后,再按4,按道理程序会把我输入的学生信息输入到文件中,然后再输出“Input num:”到屏幕上,然后我再输入num。可是现在,程序输出信息到文件里后,它并没有输出“Input num:”到屏幕上,也没输出到文件中(当然不会输出到文件中),不知道是怎么回事。
而且当我按5之后,程序输出了数据,又输出了“Input num:”,然后却直接关闭程序了。
下面附上代码:
#include<bits/stdc++.h>
#include<cstring>
using namespace std;
struct student
{
string name;//姓名
char sex;//性别
float score;//学分
int birthday;//生日
}a[100];
int main()
{
int num,i=0,j=0;//i表示第几个学生 ,j在第4中用,存结构体长度
cout<<"Please input the following num to run the program."<<endl;
cout<<"1.add student"<<endl;//新添加学生信息
cout<<"2.delete last student"<<endl;//删除最后一个学生信息
cout<<"3.display student list"<<endl;//打印输出当前系统含有的学生信息
cout<<"4.save the student list to the the database"<<endl;//存储学生信息至数据库 覆盖(可以使用database.txt模拟)
cout<<"5.read the student list from the database"<<endl;//从数据库读取学生信息更新当前学生信息内容
cout<<"6.exit the program"<<endl<<endl;//退出程序
cout<<"Input num:";
while(cin>>num)
{
switch(num)
{
case 1:
if(i==100)
{
cout<<"学生信息已满!"<<endl;
cout<<"Input num:";
break;
}
else
{
cout<<"Enter name>";
cin>>a[i].name >>a[i].sex >>a[i].score >>a[i].birthday ;
i++;
cout<<"Input num:";
break;
}
case 2:
if(i==0)
{
cout<<"当前还未有学生信息!请按1输入学生信息!"<<endl;
cout<<"Input num:";
break;
}
else
{
a[i-1].name="";
a[i-1].sex =' ';
a[i-1].score =NULL;
a[i-1].birthday =NULL;
i--;
cout<<"Input num:";
break;
}
case 3:
if(i==0)
{
cout<<"当前没有任何学生信息!请按1添加学生信息!"<<endl;
cout<<"Input num:";
}
else
{
for(int n=0;n<i;n++)
{
cout<<a[n].name<<" ";
cout<<a[n].sex <<" ";
cout<<a[n].score <<" ";
cout<<a[n].birthday <<endl;
}
cout<<"Input num:";
}
break;
case 4:
freopen("学校学生管理软件.out","w",stdout);
for(int n=0;n<i;n++)
{
cout<<a[n].name<<" ";
cout<<a[n].sex<<" ";
cout<<a[n].score<<" ";
cout<<a[n].birthday<<endl;
}
freopen("学校学生管理软件.in","w",stdout);
for(int n=0;n<i;n++)
{
cout<<a[n].name<<" ";
cout<<a[n].sex<<" ";
cout<<a[n].score<<" ";
cout<<a[n].birthday<<endl;
}
fclose(stdout);
cout<<"Input num:";
break;
case 5:
freopen("学校学生管理软件.in","r",stdin);
for(int n=0;n<2;n++)
{
cin>>a[n].name;
cin>>a[n].sex;
cin>>a[n].score;
cin>>a[n].birthday;
}
fclose(stdin);
for(int n=0;n<2;n++)
{
cout<<a[n].name<<" ";
cout<<a[n].sex<<" ";
cout<<a[n].score<<" ";
cout<<a[n].birthday<<endl;
}
cout<<"Input num:";
break;
case 6:
return 0;
}
}
}
各位大神帮忙看一下,在下感激不尽!!!!