#include<iostream>
#include<stdio.h>
using namespace std;
void student_array();
void query();
void show_arr();
void student_entry();
struct student
{
char name[80];
char f_name[80];
char the_class[3];
char grade[2];
};
student std_arr[10];
char action;
int count;
int main()
{
cout<<"add 1st student"<<endl;
student_entry();
}
void student_entry()
{
if (count == 10)
{
cout<<"Memory Full!";
//break;
}
cout<<"enter name of student"<<endl;
cin>>std_arr[count].name;
//cout<<std_arr[count].name;
cout<<"enter student's father's name"<<endl;
cin>>std_arr[count].f_name;
cout<<"enter the class of student"<<endl;
cin>>std_arr[count].the_class;
cout<<"enter the grade of student"<<endl;
cin>>std_arr[count].grade;
query();
count++;
}
void query()
{
cout<<"what do you want to do?"<<endl;
cout<<"press a to add"<<endl;
cout<<"press s to show"<<endl;
cout<<"press q to quit"<<endl;
cin>>action;
//cout<<action;
switch (action)
{
case 'a':
{
student_entry();
break;
}
case 's':
{
show_arr();
break;
}
default:
{
cout<<"wrong entry";
query();
break;
}
}
}
void show_arr()
{
for (int i = 0; i < count; i++)
{
cout<<endl<<"Student No."<<count<<endl;
cout<<"Name: "<<std_arr[i].name<<endl;
cout<<"Father's Name: "<<std_arr[i].f_name<<endl;
cout<<"Class: "<<std_arr[i].the_class<<endl;
cout<<"Grade Achieved: "<<std_arr[i].grade<<endl;
}
}
我的开关结构没有在s的情况下调用show_arr()函数。
最佳答案
您需要在调用count
之前增加变量query
,否则将不执行for
循环。由于已经将一个学生添加到数组中,因此在执行查询之前增加此变量是有意义的。