nishisheiaasfa 2021-05-21 19:12 采纳率: 100%
浏览 34
已结题

为什么输完姓名后,程序直接结束运行了呢? 如果不定义姓名,可以正常运行。

#include<iostream>
#include<string>
#include <windows.h>
int MAX;
using namespace std;
//光标定位在屏幕中间
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
void locate(int x, int y)
{
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(hout, coord);
};
void Menu()
{
    locate(45, 7);
    cout << "■■■■■■■■■■■" << endl;
    locate(45, 8);
    cout << "■  " << "1.添加学生信息" << "  ■" << endl;
    locate(45, 9);
    cout << "■  " << "2.显示学生信息" << "  ■" << endl;
    locate(45, 10);
    cout << "■  " << "3.查找学生信息" << "  ■" << endl;
    locate(45, 11);
    cout << "■  " << "0.退出管理系统" << "  ■" << endl;
    locate(45, 12);
    cout << "■■■■■■■■■■■" << endl;
}
//学生结构体
struct StudentIformation
{
    std::string m_Name;//姓名
    int m_Score;  //  分数
    double m_Studentnumber;//学号
};
StudentIformation* p = new StudentIformation[MAX];
//添加学生
void addStudent(StudentIformation* p)
{
    std::string name;
    for (int i = 0; i < MAX; i++)
        
    {
        cout << "请输入第" << i + 1 << "学生的姓名:" << endl;
        string M_name;
        cin >> M_name;
        p[i].m_Name = M_name;
        ////姓名
        //
        //cin >> name;
        //p[i].m_Name = name;
        //分数
        int score;
        cout << "请输入第" << i + 1 << "学生的分数:" << endl;
        cin >> score;
        p[i].m_Score = score;
        //学号
        double number;
        cout << "请输入第" << i + 1 << "学生的学号:" << endl;
        cin >> number;
        p[i].m_Studentnumber = number;
    }
}
//显示学生
void  showStudent(StudentIformation* p)
{
    for (int i = 0; i < MAX; i++)
    {
        cout << "姓名:" << p[i].m_Name << "\t";
        cout << "成绩:" << p[i].m_Score << "\t";
        cout << "学号:" << p[i].m_Studentnumber << "\t";
    }
    system("pause");//缓冲
    system("cls");//清屏
}
//查找学生
int isEsist(StudentIformation* p, int num)
{
    for (int i = 0; i < MAX; i++)
    {
        if (p[i].m_Studentnumber == num)
            return i;
    }
    return -1;
}
void findStudent(StudentIformation* p)
{
    cout << "请输入要查找的学生的学号" << endl;
    int number;
    cin >> number;
    isEsist(p, number);
    int ret = isEsist(p, number);
    if (ret != 1)
    {
        cout << "姓名:" << p[ret].m_Name << "\t";
        cout << "学号:" << p[ret].m_Studentnumber << "\t";
        cout << "成绩:" << p[ret].m_Score << endl;
        system("pause");
    }
    else
        cout << "查无此人" << endl;
    }
    int main()
    {
        cout << "请输入要录入学生人数" << endl;
        int max;
        cin >> max;
        MAX = max;
        while (true)
        {
            Menu();
            int n;
            cin >> n;
            switch (n)
            {
            case 1:
                system("cls");
                addStudent(p);
                system("cls");
                break;
            case 2:
                system("cls");
                showStudent(p);
                system("cls");
                break;
            case 3:
                system("cls");
                findStudent(p);
                system("cls");
                break;
            case 0:
                system("pause");
                return 0;
                break;
            default:
                break;
            }
        }
        delete[]p;
    }
 

  • 写回答

2条回答 默认 最新

  • 关注

     cin >> M_name;这个代码后面加一行代码

    fflush(stdio);//清空缓存。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月26日
  • 已采纳回答 10月18日

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等