gpt4 book ai didi

c++ - C++中的开关结构不起作用

转载 作者:行者123 更新时间:2023-11-28 03:56:52 25 4
gpt4 key购买 nike

#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;
}
}

我的 switch 结构没有在 s 的情况下调用 show_arr() 函数。

最佳答案

您需要在调用query 之前增加变量count,否则不会执行for 循环。由于已经将一名学生添加到数组中,因此在执行查询之前增加此变量是有意义的。

关于c++ - C++中的开关结构不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3218081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com