C++ C+中的开关结构+;不起作用 #包括 #包括 使用名称空间std; void student_数组(); void query(); void show_arr(); 无效学生_条目(); 结构学生 { 字符名[80]; 字符f_名称[80]; char_类[3]; 炭级[2]; }; 学生标准[10]; 炭作用; 整数计数; int main() { cout
在调用C++ C+中的开关结构+;不起作用 #包括 #包括 使用名称空间std; void student_数组(); void query(); void show_arr(); 无效学生_条目(); 结构学生 { 字符名[80]; 字符f_名称[80]; char_类[3]; 炭级[2]; }; 学生标准[10]; 炭作用; 整数计数; int main() { cout,c++,visual-c++,C++,Visual C++,在调用query之前,您需要增加变量count,否则将不执行for循环。由于已将一个学生添加到数组中,因此在执行查询之前增加此变量是有意义的。count始终为零 第一次从main调用student\u entry时,在增加count的值之前,您正在调用query。现在如果您输入a,下一个学生的数据将在stru arr[0]本身输入,并且调用query而不更新count 因此,每当您输入's'并调用函数show\u arr时,count的值将为零 不要从student\u entry方法调用查询,
query
之前,您需要增加变量count
,否则将不执行for
循环。由于已将一个学生添加到数组中,因此在执行查询之前增加此变量是有意义的。count
始终为零
第一次从main
调用student\u entry
时,在增加count
的值之前,您正在调用query
。现在如果您输入a
,下一个学生的数据将在stru arr[0]
本身输入,并且调用query
而不更新count
因此,每当您输入's'
并调用函数show\u arr
时,count
的值将为零
不要从student\u entry方法调用查询,只需增加计数并从中返回。在主函数中的
while(true)
循环中进行查询,并调用student\u entry
或show\u data
或根据输入的数据中断即可。处理q的开关部分在哪里?
#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;
}
}